[RESOLVED] 2d Array problem
Hi Folks,
I've got a problem with the code below:
Code:
char** map;
int iRows = 40;
int iColumns = 50;
//map = (char**)GlobalAlloc(GMEM_FIXED, iRows+1);
map = (char**)new char[iRows];
for(int i=0;i<iRows;i++)
//map[i] = (char*)GlobalAlloc(GMEM_FIXED, iColumns+1);
map[i] = (char*)new char(iColumns);
for(int i=0;i<iRows;i++){
for(int j=0;j<iColumns;j++){
map[i][j] = 'X';
}
}
The problem is, that the last for-loop crashes on writting to the array after X loops.
There is no problem in allocation, globalalloc and new both work fine it seems.
what is wrong in that code?
Re: [RESOLVED] 2d Array problem
one additional question...
why does
Code:
for(int i=0;i<iRows;i++)
delete[] map[i];
delete[] map;
crashes ?
how should I cleanup the memory insteat?
Re: [RESOLVED] 2d Array problem
Release code is OK, but there is one more problem:
map[i] = (char*)new char(iColumns);
Should be:
map[i] = new char[iColumns];
Don't use casting when this is not absolutely necessaty, this hides errors.