In reality, an array of pointers pointed to by a pointer is still an array of integral data types or numbers to hold the memory addresses. You should use delete[]
for both.
Also, yes, a new[]
implies a delete[]
.
When you create an array of arrays, you’re actually creating an array of numbers that happen to hold the memory address for another array of numbers. Regardless, they’re both arrays of numbers, so delete both with delete[]
.
http://coliru.stacked-crooked.com/a/8a625b672b66f6ce
#include <iostream> int main() { //Hey, pointers have a finite size, no matter the indirection level! std::cout << "sizeof(int*): " << sizeof(int*) << std::endl; std::cout << "sizeof(int**): " << sizeof(int**) << std::endl; std::cout << "sizeof(int***): " << sizeof(int***) << std::endl; //Create an array of pointers that points to more arrays int** matrix = new int*[5]; for (int i = 0; i < 5; ++i) { matrix[i] = new int[5]; for (int j = 0; j < 5; ++j) { matrix[i][j] = i*5 + j; } } //Print out the matrix to verify we have created the matrix for (int j = 0; j < 5; ++j) { for (int i = 0; i < 5; ++i) { std::cout << matrix[j][i] << std::endl; } } //Free each sub-array for(int i = 0; i < 5; ++i) { delete[] matrix[i]; } //Free the array of pointers delete[] matrix; return 0; }