Accessing members of an array using members of a second array

I've been working on a c++ graphics program, and I've run into an interesting problem.

the basic idea is this:

1
2
 
        mesh.normal[i] = Normal[ normalIndex[i] ];



didn't work, so I tried this:

1
2
3
4
5
6
       
       unsigned int a = normalIndex[i];
         
       float x = Normal[a];
        
       mesh.Normal[i] = x;


still didn't work.

is there a problem with using an array member to index a second array?
Could your elaborate what means "did not work"?!
The program returns "This program has encountered a problem and needs to close. Has to be some sort of memory problem. I haven't tried this in visual studio. maybe that will help the debugging, but i know the dissassembly will be cryptic. If someone could tell me if the first line of code is possible, or what the specifications are on using members of arrays as indices it would be a great help.
It's perfectly legal code. There's nothing special about an array member - as long as it's an integer, it's legal to use it as an array index.

If you're sure that the crash is happening because of this, then my guess would be that whatever value is stored in normalIndex[i] is bigger than the number of elements you've allocated for Normal. Use a debugger to see what the value of normalIndex[i] is at the point where the crash occurs.
It's perfectly legal code. There's nothing special about an array member - as long as it's an integer, it's legal to use it as an array index.

If you're sure that the crash is happening because of this, then my guess would be that whatever value is stored in normalIndex[i] is bigger than the number of elements you've allocated for Normal. Use a debugger to see what the value of normalIndex[i] is at the point where the crash occurs.
Out of range. I'm sure.
Last edited on
If you had crash with array, I claim it is "Access Violation".

Operating System will assign certain size of space on RAM to your program. If you do this:

1
2
int a[10];
a[99999] = 1;


The chance is high that your program try to assign value to space which not belong to the space the program is assigned.

If your program got enough space, I don't know. That is not error at all, which is bad because you don't know what's gonna happen.

That is so called "Out of range". STL throws exception to let user know if it went out of range so the program crash itself, but array has no check for that.

Make sure your program does not give any weird index to subscript operator[].
Last edited on
int a[10];
a[-99999] = 1;

This also always causes a crash. :)
crash could be because of

either 'mesh.normal[i]'
or 'normalIndex[i] '
or 'Normal[ normalIndex[i] ]'

is trying to access the array element which is out of array range.
Last edited on
Topic archived. No new replies allowed.