1

What if we delete an object and then try to access an object through the pointer which has been deleted? I found the following:

Before the lifetime of an object has started but after the storage which the object will occupy has been allocated or, after the lifetime of an object has ended and before the storage which the object occupied is reused or released, any pointer that refers to the storage location where the object will be or was located may be used but only in limited ways

[...]

— the object will be or was of a class type with a non-trivial destructor and the pointer is used as the operand of a delete-expression,

But this's not exacly the case I'm looking for. What happend if the storage has been release or reused? Does standard explain that case?

user3663882
  • 6,957
  • 10
  • 51
  • 92

4 Answers4

6

"Undefined behavior" must be answer from standard. But what ever standard says do not do this. It will get u only trouble with no gain :)

Vishal Kumar
  • 762
  • 1
  • 7
  • 15
  • There's no standard-proof answer? – user3663882 Jul 03 '15 at 07:35
  • "after the lifetime has ended and before the storage is reused or released, any pointer where the object will be or was located may be used but only in limited ways" is the answer... So after reallocation the pointer could be used... – Jean-Baptiste Yunès Jul 03 '15 at 12:52
3

If the memory has been released and then reallocated, then in theory there is no harm, but it is very unlikely that the reallocation will provide you a usable chunk of memory for your pointer type. Even if it will be the case, you will have strange/complex behavior because you pointer will see different objects (some kind of volatile objects ?).

I would certainly never rely on such a behavior, except if you need/want to manage the memory by yourself (with placement new for example)...

Jean-Baptiste Yunès
  • 34,548
  • 4
  • 48
  • 69
2

If I understand your question correctly, you're wondering if information is still accessible, even after it has been deallocated?

I would say that there is no guarantee. After deallocation, anything stored on the stack is no longer protected. "Limited use" most likely refers to accessing immediately after deallocation. Even then, there is a risk of corrupted data and a segmentation fault.

I hope this answers your question.

Daniel
  • 247
  • 3
  • 11
  • 1
    No "limited use" does not refer to "access the memory" (when memory is released dereferencing a pointer on it is undefined behavior). It refers to "limited use of the pointer", means that you can do what you want (arithmetic on it for example) but no dereferencing. – Jean-Baptiste Yunès Jul 03 '15 at 06:18
2

C++03 covers this in 3.7.3.2/4 (Deallocation functions). When the memory where the object was stored is deallocated all pointers to that memory get invalidated and after that they cannot be used - they cannot be dereferenced, cannot be cast (seriously!!!), cannot even be printed (no, not kidding), doing any of those yields undefined behavior. The only two things you can do to such pointers are either assigning them a null pointer or assigning them a valid pointer.

Community
  • 1
  • 1
sharptooth
  • 167,383
  • 100
  • 513
  • 979