5

I have made a simulation of a stack using malloc to create an array of MAX elements and assign it to a pointer. I then made a second pointer pointing to the first one, to be able to navigate back and forth, executing pushes and pops.

int * stack= (int *) malloc(MAX * sizeof(*stack));
int * stack_ptr = stack;

In the end, I delete the pointer containing the array, using

free (stack);

After I delete the array (stack), stack_ptr is pointing to an address with no contents, right?

Can this cause a memory leak or any kind of problem, even if the program terminates exactly after?

Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
Georgy90
  • 155
  • 2
  • 12
  • 3
    It is *not* a pointer to another pointer, but a **copy** of the pointer obtained from malloc. Only one block of memory exists and you can use any copy of its pointer. It is the *value* you pass to `free` which matters, not which variable it is stored in. After that you must not dereference the pointer, no matter what variable it is stored in. – Weather Vane Mar 11 '19 at 22:07
  • 2
    Google "dangling pointer" - don't worry - it's a genuine computing term and safe for work ;-) – John3136 Mar 11 '19 at 22:08
  • 1
    `free (stack_ptr)` will also free the block. And just as you do not use stack_ptr to access the heap after freeing of the block it references, any other pointer referencing this block, must not access the freed block. – Ted D. Mar 11 '19 at 22:10
  • 1
    An example of **a pointer to another pointer** would be `int ** stack_ptr = &stack;` – Weather Vane Mar 11 '19 at 22:20

4 Answers4

2

After I delete the array (stack), stack_ptr is pointing to an address with no contents, right?

After free(stack);, attempting to perform *stack_pt is undefined behavior (UB). The contents pointed to stack_pt may be good, bad or ugly or simply cause code to crash. It is UB to attempt to find out.

Can this cause a memory leak or any kind of problem, even if the program terminates exactly after?

Having a unchanged copy of the former valid pointer in stack_pt in itself in not a leak nor UB. Just don't use stack_pt.


Often after freeing, it is good practice to NULL the pointer(s).

int * stack= (int *) malloc(MAX * sizeof(*stack));
int * stack_ptr = stack;
...
free (stack);
stack = NULL;
stack_ptr = NULL;

Aside: Recommend to allocate to the size of the referenced object, not the type and forego the unneeded cast.

// int * stack= (int *) malloc(MAX * sizeof(*stack));
int *stack =  malloc(sizeof *stack * MAX);
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
1

No memory leak because you did a copy of pointer. So the pointer stack_ptr is pointing to not-allocate space.

If you want to read the content pointed by stack_ptr after your code it will cause a segmentation fault because you freed the space allocated by your malloc.

Arnaud Peralta
  • 1,287
  • 1
  • 16
  • 20
1

https://en.wikipedia.org/wiki/Dangling_pointer

You may or may not segfault if you try to dereference it, but it is dangling. There is no memory leak though. stack_ptr is still on the stack, and will be popped off when the function returns, like normal stack variables. You are freeing the memory that it points to.

Bwebb
  • 675
  • 4
  • 14
1

It is not pointer to pointer only both have the same value. Nothing will happpen to the second one. It will still have the same value, but this time it will reference not allocated memory.

this is pointer to pointer (ptr2):

int *ptr1;
int **ptr2 = &ptr1
0___________
  • 60,014
  • 4
  • 34
  • 74