So I'm having trouble deleting a 3D array. It seems to cause a segmentation fault in my code. This is what I have. The segmentation fault is caused by the deletion of the 3D array, but I don't know why.
int main( int argc, char** argv )
{
//variables
ifstream inputFileStream[ argc - 2 ];
int ***dimensions;
//open inputFileStream and read in values...
//creating the dynamic 3D array and reading in grey values
dimensions = new int**[ argc - 2 ];
for( int i = 0; i < argc - 2; i++ )
{
dimensions[ i ] = new int*[ width ];
for( int j = 0; j < width; j++ )
{
dimensions[ i ][ j ] = new int[ height ];
for( int k = 0; k < height; k++ )
{
inputFileStream[ i ] >> dimensions[ i ][ j ][ k ];
}
}
}
//do something with array
//delete array
for( int i = 0; i < width; i++ )
{
for( int j = 0; j < height; j++ )
{
delete[] dimensions[ i ][ j ];
}
delete[] dimensions[ i ];
}
delete[] dimensions;
}
When allocating, your 'i' value goes from 0 to 'argc-2', your 'j' value goes from 0 to 'width', and your 'k' value corresponds to 'height'. When deleting, your 'i' value goes from 0 to 'width', and your 'j' value goes from 0 to 'height'. Yet you index with [i][j] in both cases. This is not consistent.