What in the world is going on?

I don't know whats happening here. I've been having a lit of issues with arrays lately. Other examples to come. I have array [4][9], array[4][0], and array [9][0] copying values from array[5][0], array[3][9], and array[8][9] seemingly without reason. This problem seems to go away inexplicably when I change the size of int array to [10][10].


#include <iostream>
using namespace std;
int main(){
int array [9][9];
array[4][9] = 0;
array[4][0] = 0;
array[9][0] = 0;

array [5][0] = 2;
array[3][9] = 7;
array[8][9] = 10;
cout << "connection: " <<4<< ":" <<9<< " strength: " << array[4][9] << endl;
cout << "connection: " <<4<< ":" <<0<< " strength: " << array[4][0] << endl;
cout << "connection: " <<9<< ":" <<0<< " strength: " << array[9][0] << endl;

int array2 [10][10];
array2[4][9] = 0;
array2[4][0] = 0;
array2[9][0] = 0;

array2 [5][0] = 2;
array2[3][9] = 7;
array2[8][9] = 10;
cout << "connection2: " <<4<< ":" <<9<< " strength: " << array2[4][9] << endl;
cout << "connection2: " <<4<< ":" <<0<< " strength: " << array2[4][0] << endl;
cout << "connection2: " <<9<< ":" <<0<< " strength: " << array2[9][0] << endl;
}
---------------------produces the following results--------------------------
connection: 4:9 strength: 2
connection: 4:0 strength: 7
connection: 9:0 strength: 10
connection2: 4:9 strength: 0
connection2: 4:0 strength: 0
connection2: 9:0 strength: 0

The last index of an array is size-1
int array [9][9]; You can use array[0-8][0-8]
To really beat the dead horse, and reiterate what Thomas says:

If you make an array of size "n", the elements are numbered from zero to n-1.

If you make an array of size 3, the elements are numbered 0, 1, 2.

If you make an array of size 3, there IS NO element 3.

1
2
3
4
5
int array[3];  // a new array, of size three
array[0]; // This exists
array[1]; // This exists
array[2]; // This exists
array[3]; // This does NOT exist 


By the nature of C++ trusting you to know what you're doing, if you try to read or write array elements that don't exist, it will instead read/write memory next to the array. That memory could contain other variables that you're trashing, or that will just overwrite whatever you just wrote. That memory could be outside your allotted memory space and the program could crash.

In a 2D array, that other memory could be in use by other parts of the 2D array, so you'll be trashing your own array.

So let's look at your code.

int array [9][9];
Right, so array[0][0] to array[8][8] exist.

array[4][9] = 0; DOES NOT EXIST
array2[9][0] = 0; DOES NOT EXIST
and so on.
Last edited on
In a multi-D array

The elements are contiguous.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
constexpr size_t COLS = 9;
constexpr size_t COLS = 9;

int array[ROWS][COLS]; // block of 81 integers

size_t row = ...
size_t col = ...

array[row][col]
// means same as
*(array + row*COLS + col)

// therefore
array[4][9]
// means
*(array + 4*COLS + 9)
// means
*(array + 45)
// means
*(array + 5*COLS)
// means
array[5][0]
// which is a valid array element 


1
2
3
4
5
6
7
8
9
10
int array [9][9];
array[4][9] = 0;
array[5][0] = 2;
// does same as
int array [9][9];
array[5][0] = 0;
array[5][0] = 2;
// does same as
int array [9][9];
array[5][0] = 2;
Topic archived. No new replies allowed.