Hello everyone:

I'm having a problem with vector indexing. The following code results in a segmentation fault:

for (int i = 0; i < (rows * rows); i++)
{
   temperature[(i * rows) + (rows - 1)] = 0;                                            
}

And:

for (int i = 0; i < (rows * rows); i++)
{
   temperature[i * rows] =  3;                                           
}

According to my reading, it is possible to use expressions involving calculation as vector indices. I would very much appreciate it if someone could suggest what I'm doing wrong.

Thank you for any help.

It is legal, as long as the expression evaluates to a value within the acceptable range of indexes.

The range of i is zero (0) thru (rows^2)-1. When you multiply i by rows, you dramatically increase its value to a point where it is outside the limits of the vector. For your example situation, you are better off not using an expression as an index.

Thanks a lot. I'm an idiot!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.