-3

I have read that dangling pointers are bad as they point to location that is supposed to be free.But I don't understand why is that bad?So can you give me any example of disadvantages of dangling pointer.

1
  • Comments are not for extended discussion; this conversation has been moved to chat. Feb 6, 2020 at 23:36

3 Answers 3

7

Once you have a dangling pointer, two things are forbidden:

  1. You are not allowed to dereference the pointer. The behaviour on doing so is undefined.

  2. You are not allowed to read the pointer. It's as if the pointer is an uninitialised variable, or one you've just declared.

Few folk tend to fully grasp (2), and are surprised the first time they find out about this!

As such, dangling pointers can compromise program stability. And that's quite a disadvantage.

11
  • i am not one of few folk :) can you give a reference? Feb 6, 2020 at 18:05
  • @idclev463035818: I'll do my best but I'm on the choo choo, back to my house in the West Country, although the damn train is running late!
    – Bathsheba
    Feb 6, 2020 at 18:06
  • 1
    @PrathamShah: "Dangling Pointer" is adequate terminology for a "Dangling Pointer". Just try to avoid introducing them, although that can be tricky sometimes.
    – Bathsheba
    Feb 6, 2020 at 18:13
  • 1
    The C++ standard says It's implementation defined behavior, not UB for point 2: timsong-cpp.github.io/cppwp/basic.stc#4 Feb 6, 2020 at 18:19
  • 2
    @NathanOliver That's for C++. For C, the pointer value is indeterminate as per 6.2.4p2: "... The value of a pointer becomes indeterminate when the object it points to (or just past) reaches the end of its lifetime."
    – dbush
    Feb 6, 2020 at 18:24
5

A dangling pointer is a pointer that points to a location in memory where there was an object but that object is already gone.

A dangling pointer is nothing useful or desirable, it rather describes that the pointer is completely useless and you probably have a logic error in your code.

Consider this:

int* x;
{ 
    int y = 4;
    x = &y;
}
*x = 42;   // BOOOM !! Dont do this !! Undefined behavior !!

After closing the scope (}) the object x points to does no longer exists. x will not automagically be set to NULL or anything the like. There is no y at the memory location x was pointing to. The official terminology is that after closing the scope x has a invalid pointer value in C++ and in C the value of the pointer is indeterminate.

On the other hand, if you do not dereference x after y is gone, there is no problem. x is still a dangling pointer but you can simply assign something else to make it point to some object again:

int* x;
{ 
    int y = 4;
    x = &y;
}                 // <- x can be said to be dangling here, but who cares?
int z = 5;
x = &z;           // <- x again points to an object
6
  • "x value is still the memory adress where there was" -> you can't be certain of this.
    – Bathsheba
    Feb 6, 2020 at 18:06
  • @Batsheba just (re)learned that from your answer, will try to be a bit more vague on this point ;) Feb 6, 2020 at 18:07
  • so is this the only limitation of dangling pointer just logical errors? Feb 6, 2020 at 18:13
  • 2
    @PrathamShah logic errors can cause dangling pointers. I didnt say that "logic errors are a limitation of dangling pointers". The limitation of dangling pointers is that you cannot do anything useful with them (other than assigning a differnent value). And if you still try to do something with them you typically get undefined behavior Feb 6, 2020 at 18:16
  • @463035818_is_not_a_number I have tried the example that you've discussed here but on my system, it doesn't produce any errors, any idea why? Sep 16, 2021 at 13:09
1

Dangling pointer is risky because once allocated memory has been freed, it can be reclaimed by other allocations.

Consider this example:

int main(int argc, char *argv[]) {
  int *p = (int *) malloc(sizeof(int));
  free(p);
  *p = 5;
  printf("%d\n",*p);
}

Once p is freed, there is no guarantee that 5 will be printed anymore. Also, If we write to p's address, we might be overwriting some other value used in another part of your program and trigger segmentation fault.

1
  • Segfault is the least of your worries. It's so minor compared to most of the other possibilities that you should pray for a segfault. With a segfault you know the code is wrong and have a good place to start looking for the bug. Seemingly random memory corruption, on the other hand, is an absolute <expletive deleted> to trace and you might not even notice that the results are slightly wrong before a disaster happens and people wind up dead. Feb 6, 2020 at 22:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.