Doubt with delete[]

Hi there,
I'm writing a C++ app which deals with dynamic allocated memory, a matrix to be more specific.
My real doubt is: when I use delete[], it does dealloc a column/line of the matrix, or it does dealloc the whole matrix?

[]'s
It will delete the first "row" only, e.g.:

1
2
3
4
5
6
7
8
9
10
11
int** matrix = new int*[5];
for(unsigned int i = 0; i < 5; ++i) {
    matrix[i] = new int[5];
}
//matrix is a 5x5 matrix
//if we call delete[] matrix; here, we will *only* delete the first element of each array,
//and miss the other 4, so we need to call it like this:
for(unsigned int i = 0; i < 5; ++i) {
    delete[] matrix[i];
}
delete[] matrix;
Last edited on
Topic archived. No new replies allowed.