I am trying to create a 2D dynamic array using pointers:
int** Grid;
Grid = new int*[5];
for (int i=0;i<5;i++) { Grid[i] = new int[5]; }
std::cout << Grid[4][300] << std::endl; // output: -17891602
std::cout << Grid[5][0] << std::endl; // access violation
I need the array to be of a specific size, which is why I am not using a static array. As you can see, although I am making a 5x5 array, debugger did not give error when trying to read Grid[4][300]. Can someone please enlighten me as to why is this so?
This only appears to be so for the 2nd dimension. (Trying to read Grid[5][0] would give the access violation error) Correct me if I am wrong, but technically I am really creating an array of pointers?
I expect an exception for Grid[5][0]. What I really don't get is why no error for Grid[4][300]. Can someone advise me how do I have a dynamic array with, say dimension of 5x5 ?