Deleting 3d dynamic array C++ -
i make 3d dynamic array using code
//layer = 2 //levelsize.x = 100 //levelsize.y = 100 level_array = new int**[layer]; for(int = 0; < layer; ++i) { level_array[i] = new int*[(int)levelsize.x]; for(int j = 0; j < levelsize.x; ++j) level_array[i][j] = new int[(int)levelsize.y]; }
but when want delete it, program crashes
for(int = 0; != levelsize.x; ++i) { for(int j = 0; j != levelsize.y; ++j) { delete[] level_array[i][j]; } delete[] level_array[i]; } delete[] level_array;
i don't know wrong in code of deleting array.
please me check code, thanks
you allocate memory array dimensions [layer][levelsize.x][levelsize.y]
, while deleting operate array dimensions [levelsize.x][levelsize.y][somenting]
.
for(int = 0; != layer; ++i) // ^^^^^ not levelsize.x { for(int j = 0; j != levelsize.x; ++j) // ^^^^^^^^^^^ not levelsize.y { delete[] level_array[i][j]; } delete[] level_array[i]; } delete[] level_array;
Comments
Post a Comment