0
int a = 10; 
int* ptrA = &a;
int* ptrB = &a;

delete ptrA;

return 0;

This should not cause a leak, cause the mem/data is getting freed by ptrA, but then we have 2 pointers which are pointing to a valid address, whose data has just been deleted? is that how it works?

Guillaume Racicot
  • 39,621
  • 9
  • 77
  • 141
Tesla
  • 87
  • 11
  • 11
    Leak is can only be caused by dynamic allocation. `delete` on a statically/automatically allocated data is illegal. – Eugene Sh. Apr 13 '18 at 19:42
  • Learning pointers is great! But what you should use for this pattern is `std::shared_ptr`. – Davislor Apr 13 '18 at 20:20

3 Answers3

7

is that how it works?

Not quite. First, you have undefined behavior, you can't delete an object with automatic storage (for example, on the stack).

Let's change your example to that:

int* ptrA = new int{10};
int* ptrB = ptrA;

delete ptrB;

return 0;

but then we have 2 pointers which are pointing to a valid address , whose data has just been deleted ?

Well, both. There are two pointer indeed, pointing to the same value.

If you print the value of ptrA and ptrB, you'll see something similar to that:

0x0034ab56
0x0034ab56

Both pointers point to the same address in memory.

So in other words, if you delete ptrA, you will deallocate the object at address 0x0034ab5. Since both pointers point to the same address, both pointer are now dangling. The official term for it is an invalid pointer value, and both ptrA and ptrB are invalid pointer values because the storage which both pointer were pointing to has been freed.

Guillaume Racicot
  • 39,621
  • 9
  • 77
  • 141
  • There is no guarantee that the pointers will stay the same after the delete. Dangling pointers are nearly the same as if they were again indeterminate, though they can be copied and compared to the null-pointer. – Deduplicator Apr 13 '18 at 20:04
  • @Deduplicator as far as I know, they are guaranteed to be the same. If you look the declaration of `operator delete`, you'll see that it doesn't take a reference to the pointer, but the value of the pointer. I hardly see how the value could change. – Guillaume Racicot Apr 13 '18 at 20:06
  • It's not that they are guaranteed to change, but there are just that few operations on them still allowed, respective have a guaranteed outcome. – Deduplicator Apr 13 '18 at 20:07
  • @Deduplicator can you link me to a source so I can learn and change my answer accordingly? – Guillaume Racicot Apr 13 '18 at 20:08
  • See [Is it legal to compare dangling pointers?](https://stackoverflow.com/questions/30694069/is-it-legal-to-compare-dangling-pointers), especially the highest voted answer. – Deduplicator Apr 13 '18 at 20:12
  • @GuillaumeRacicot Or, from the standard, [\[basic.stc\]/4](https://timsong-cpp.github.io/cppwp/n4659/basic.stc#4). – Daniel H Apr 13 '18 at 20:13
  • Thank u for the answer.... Just what i was looking for . so let me clear this , them memory gets freed but creates 2 dangling pointers , which we can always set to 0 or nullptr and then compare to null to avoid using them in future ? – Tesla Apr 13 '18 at 20:41
  • 1
    @Tesla Yes indeed, you can set them to null to be able to avoid using dangling pointers. – Guillaume Racicot Apr 13 '18 at 20:43
  • @Tesla if an answer is solving the question, consider accepting it. – Guillaume Racicot Apr 13 '18 at 20:44
0

You should use delete only on objects allocated with new.

If you are not using new the compiler will take care of the memory allocation.

Unfortunately compilers allow this kind of syntax which can have bad effects in some cases because you are calling destructor twice.

0

This should not cause a leak,

No. Nobody can tell you what this should or should not cause, because using delete on a pointer to an object with automatic storage duration is undefined behaviour.

All your further observations and all your further questions are invalid, because you are no longer dealing with a C++ program.

Christian Hackl
  • 27,051
  • 3
  • 32
  • 62
  • Did not know about the automatic storage thing .I am glad @Deduplicator clarified that problem too with a code snippet , unlike you. Thanks anyways – Tesla Apr 13 '18 at 21:09