0

When a pointer is allocated memory using malloc, pointer (say x)will now point to memory address. Later I free this(x) memory pointer,but pointer is still pointing to it's old memory. This would now create dangling pointer. (Because I did not point the old pointer to NULL after free)

Now, assume I use malloc and assume new pointer(y) now points to same memory location as old pointer (x) Doesn't memsetting new pointer (y) to 0 solve dangling pointer issue.

Assume I have only one struct type.So every malloc which I do is always of same size of same structure. If it was different struct , I know i may still have some data at the end of struct if new pointer (y) has small memory allocation than pointer (x)

bbcbbc1
  • 95
  • 7
  • What is your question? You have no control over the address returned by malloc so your assumption is... wild!? – Allan Wind Mar 30 '21 at 07:21
  • If I have dangling pointer, and newly allocated memory is same as old pointer which is dangling pointer. Doesn't doing memset to 0 of new pointer remove that dangling pointer values. (Assumption is same struct size was used throughout malloc in a program and same memory was allocated) – bbcbbc1 Mar 30 '21 at 07:24
  • 1
    `memset` has nothing to do with it. If a new allocation returns the same address, it's valid even without `memset` – Barmar Mar 30 '21 at 07:24
  • @barmar once I do memset of newly allocated memory for pointer "y" . The dangling pointer no longer exists. 1. Because malloc returned same old address 2. Also I had done memset of new pointer with 0, so any old data if any present is 0. Isn't it fair to assume I wouldnt see any undefined behavior with my assumption in question. – bbcbbc1 Mar 30 '21 at 07:30
  • Does this answer your question? [Reusing freed pointers in C](https://stackoverflow.com/questions/17863923/reusing-freed-pointers-in-c) – Antonin GAVREL Mar 30 '21 at 07:32
  • 1
    The dangling pointer no longer exists as soon as `malloc()` returns the same pointer. `memset` is irrelevant. – Barmar Mar 30 '21 at 07:32
  • you might want to read https://mcpmag.com/articles/2010/07/27/trip-down-memory-lane-with-emulateheap.aspx?admgarea=BDNA&m=1 – Antonin GAVREL Mar 30 '21 at 07:40

2 Answers2

1

Dangling pointers are only a concern if you try to use them after you've freed them.

Yes, it's possible that a new allocation can return the same address that x has. But you can never know whether this is going to happen, so you still can't use x any more. It would just be a coincidence if its address became valid again.

Even if you keep allocating and freeing the same size, there's no expectation that it will keep reusing the same address.

For safety you must assume that a freed pointer will never become valid again.

Barmar
  • 741,623
  • 53
  • 500
  • 612
1

The term dangling pointer means that whatever address in memory it points to is invalid. If you make it valid, like your 2nd malloc, then the address becomes valid. If you store the same address in two different variables (via your assumption) both are valid pointers:

#include <stdio.h>
#include <stdlib.h>


struct s { int i; };


int main() {
    struct s *p = malloc(sizeof(struct s));
    printf("before: %p\n", (void *) p);
    free(p);
    // p is dangling
    printf("after:  %p\n", p);

    struct s *p2 = malloc(sizeof(struct s));
    // p and p2 are valid
    printf("before: %p\n", (void *) p2);
    free(p2);
    // p and p2 are dangling
    printf("after:  %p\n", p2);
}

and the output from my pleasingly corroborative malloc:

before: 0x561b73d3b260
after:  0x561b73d3b260
before: 0x561b73d3b260
after:  0x561b73d3b260
Allan Wind
  • 23,068
  • 5
  • 28
  • 38