Arrays referencing same memory space

Hi,

I'm having an issue where an array is not being initialized and it is referencing the memory space of another array.

I have a 3d array and a 2d array with two different names. I have nested for loops to set their values 0 and -1 initially. After the first 3d array is set to 0, when i go to initialize the second 2d array values to -1 it replaces the first array's values.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
    int a [n][n];
    int b [n][n][m];
    for (int i= 1; i < (n+1); i++) {
        for (int j= 1; j < (n+1); j++) {
    	    for (int k= 0; k < m; k++) {
		a[i][j][k] = 0;
	    }
	}
    }
    ...
    <some code not relating to either a or b or n or m>
    ...
    for (int i = 1; i < (n+1); i++) {
        for (int j = 1; j < (n+1); j++) {
            b[i][j] = -1;
        }
    }


is this a compiler issue?
The array indexing is from 0 to n-1 and not from 1 to n.

If you try to access array[n] element of size n array, then it will go out of bounds and access memory not meant for that array.

Change your loops to:

for ( int i = 0; i < n; i++ )
Last edited on
awsome. thanks for the help!
Topic archived. No new replies allowed.