Proper way to absolutely delete a pointer to pointer

Hi everyone,
I defined a pointer to pointer (p2p) as:
int **my_list = NULL;
After assigning values to this (p2p).
I want to delete it, but I am confusing several ways as follows:

The first way:
delete[] my_list;
my_list = NULL;

The second way:
delete[] my_list[0];
my_list[0] = NULL;
delete[] my_list[1];
my_list[1] = NULL;
delete[] my_list[2];
my_list[2] = NULL;
.
.
.
delete[] my_list[i];
my_list[i] = NULL;

So my question is which is the best way to absolutely delete a p2p ?. And how I can verify that the p2p is already deleted?

Thank in advance!
Last edited on
delete what you have created with new, nothing else.
Assigning it back to NULL is the way to go.
int **my_list = NULL;

pointers to pointers like all other datatypes are static, you can't get rid of them. There's no need to get rid of them.

When you do need to worry about having to use delete is when you dynamically allocate memory with new. So something like this: int **my_list = new int*[10];

To delete,
delete[] my_list;

Note that if the indexes of the pointer to pointer i.e my_list[0], my_list[1] etc are pointing to something, they need to be separately deleted with regular delete delete my_list[0];
How can I verify that the pointer is deleted or not?
I used:
if(my_list == NULL) but it does not work.
The pointer will continue to point at the location where it was pointing when you had allocated memory.

1
2
3
4
	char* foo = new char[1];
	cout << (int)foo;
	delete foo;
	cout <<  '\n' << (int)foo;


If you're done using the pointer then it's a good idea to write: my_list = NULL
Note that delete ptr does not delete the pointer ptr. Instead it deletes whatever ptr is pointing to.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// If my_list points to an array that has been created with new, and
// each element is pointing to an int that has been created with new ...
const int my_list_length = 5;
int **my_list = new int*[my_list_length];
for (int i = 0; i < my_list_length; ++i)
{
	my_list[i] = new int;
}

// ... then you you need to loop through the array and use
// delete on each of the elements before deleting the array.
for (int i = 0; i < my_list_length; ++i)
{
	delete my_list[i];
}
delete[] my_list;


1
2
3
4
5
6
7
8
9
10
11
12
13
14
// If my_list points to an array that has been created with new, 
// and each element is pointing to a local variable ...
const int my_list_length = 5;
int **my_list = new int*[my_list_length];
int local_var;
for (int i = 0; i < my_list_length; ++i)
{
	my_list[i] = &local_var;
}

// ... then you don't need to do anything to delete the local_var because 
// it will get destroyed automatically when it goes out of scope, but 
// you still need to delete the array that was created with new.
delete[] my_list;


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// If my_list points to a local array where each element
// is pointing to an int that has been created with new ...
const int arr_length = 5;
int* arr[arr_length];
for (int i = 0; i < arr_length; ++i)
{
	arr[i] = new int;
}
int **my_list = arr;


// ... then you only need to use delete on the array elements.
for (int i = 0; i < arr_length; ++i)
{
	delete arr[i];
}


1
2
3
4
5
6
7
// If my_list points to a local array where each 
// element is pointing to a local variable ...
int local_var;
int* arr[] = {&local_var, &local_var};
int **my_list = arr;

// ... then you don't need to use delete at all. 
Last edited on
@Peter87, Thank you so much. Your answers are helpful to me.
a lot of people will tell you to set null after delete.
delete [] ptr;
ptr= null;

now you can check it (if ptr==null) or you can delete it again harmlessly (delete of a null ptr does nothing).

this is 'defensive programming' .. its not required, but it will help prevent bugs. This was said above, but it can't be stressed enough.
Last edited on
Topic archived. No new replies allowed.