Which heap pointers to delete

Hello,

I am curious if we have pointers and pointers to pointers to pointers, which variable must be deleted? I have two scenarios below that show deferent pointers being deleted:

Why couldn't we just delete the main pointer variables (in the two cases p and b) instead of *b?

1
2
3
4
5
6
7
8
9
10
11
12
13
int main() {
	int **p = new int*;
	int *v;
	int w;
	int i;

	v = &i;
	*p = &i;

	delete p;

	return 0;
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int main() {
	int *a;
	int ***b;
	int c;
	int d;
	b = new int**;
	a = new int;
	*b = new int*;
	**b = &d;

	delete *b;
	delete a;
	delete b;
	return 0;
}
I am curious if we have pointers and pointers to pointers to pointers, which variable must be deleted?

Anything that was created using new must be deleted. That's it.


Why couldn't we just delete the main pointer variables (in the two cases p and b) instead of *b?


Because then you would have things that were created using new that were never deleted. If you lost the pointer to them, you'd have a memory leak. The object that was created using new would never be destroyed. The memory would never be available for reuse.

If you're asking "if I delete a pointer to a pointer, why doesn't delete go down that chain and also delete the pointer it's pointing to?", the compiler isn't magic. It doesn't know when the right time to delete something is. What if you were still using something that was deleted? That would be a disaster.
Ah yes I understand thanks. Just to verify: for the second set of code, ***b would be the value of the object at the end of the pointers, right?
Last edited on
Given this set of pointers:

b (an int***) --> unnamed_pointer_one (an int**) --> unnamed_pointer_two (an int*) --> an integer

You can access unnamed_pointer_one; it's *b
You can access unnamed_pointer_two; it's **b
You can access the integer; it's ***b

Got it. Thanks!
Another question: if we want a stack pointer to point to a heap pointer, do we also use & to define the stack pointer? And also conversely? Or is that only when pointers are pointing to stack objects?

I am wondering why the code below doesn't work. The error states, " assigning to 'int ***' from incompatible type 'int **' p = &v[0];"

It just a heap pointer pointing to an array stack pointer. What could be the problem here?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int main ()
{
int *v[2];
int i;
int ***p;

 p = new int **;
 *p = &v[0];  // the problem is here but not sure why
 v[1] = new int [2];
 v[1] = &v[1][0];
 v[0] = &v[1][1];
 
 delete p;
 delete v[1];
 delete v[0];
}


Here is a visual representation: https://imgur.com/a/Yd2cM The top part is constructed as the "stack" and the bottom part as the "heap".
Last edited on
if we want a stack pointer to point to a heap pointer, do we also use & to define the stack pointer?


Not to define it, no. Just to get its address.

1
2
3
4
int* pointer_on_stack; // this pointer is on the stack
int** pointer_on_the_heap = new int**; // this pointer is on the heap

pointer_on_the_heap = &pointer_on_stack; // now the object we made on the heap is pointing at the object we made on the stack 


I am wondering why the code below doesn't work. The error states, " assigning to 'int ***' from incompatible type 'int **' p = &v[0];"


v[0] is an object of type int*. So &v[0] is an object of type int**.

p is an object of type int**, so *p is an object of type int*.

*p = &v[0]

On the left: an int*
On the right: an int**

See the problem? Just because two items are pointers doesn't mean they're the same type. They're not. If you want to assign a value to a pointer, it has to be the same type.
Yea I see the problem but I figured it must be this way since the heap pointer *p points to the stack object v[0] but v[0] is also a pointer. Hm...maybe with two && instead of one? But that wouldn't make much sense.
*p = v[0];

On the left: an int*
On the right: an int*
And that works because v[0] is a pointer? Usually we have to use & to get the address of an object.
It works because the thing on the left is the same type as the thing on the right.
Topic archived. No new replies allowed.