Hi guys,
Following this nice example I found, I was trying to create a function that dynamically generates a 2D grid (two dimensional array) of int values in C++.
It works fairly well the first couple of times you change the values but if crashes after that. I guess the part where memory is freed doesn't work as it should.
Any ideas why? Thank you in advance.
void testApp::generate2DGrid(){
int i, j = 0;
// Delete previous 2D array
// (happens when previous value for cols and rows is 0)
if((numRowsPrev != 0) && (numColumnsPrev != 0)){
for (i=0; i<numRowsPrev; i++){
delete [ ] Arr2D[i];
}
}
// Create a 2D array
Arr2D = new int * [numColumns];
for (i=0; i<numColumns; i++){
Arr2D[i] = new int[numRows];
}
// Assign a random values
for (i=0; i<numRows; i++){
for (j=0; j<numColumns; j++){
Arr2D[i][j] = ofRandom(0, 10);
}
}
// Update previous value with new one
numRowsPrev = numRows;
numColumnsPrev = numColumns;
}