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.

This question has already been answered. Start a new discussion instead.