I would like to know what happens to memory that is destroyed by "delete" operator in C++. Is 'destroying' memory in such way means setting given pieces of memory to 0 or something else?
-
Take look at this http://www.cplusplus.com/reference/new/operator%20delete%5B%5D/ – Apr 27 '13 at 11:27
-
I think this is a good question, because the standard itself seems to be a little unclear on what exactly "destruction" is. It says that constructs of C++ are used to create, destroy, and manipulate objects and objects are regions of storage. However, it also says that destructors are used to destroy objects, yet we can explicitly call a destructor and still have the region of storage available. (Maybe I'm treating the question with more depth than it's really asking for) – Joseph Mansfield Apr 27 '13 at 11:33
-
You'd normally say the memory was "deallocated" (or perhaps "freed" or "released") rather than "destroyed". The term "destroy" is more commonly used to describe what happens to the object(s) that were in the memory. – Mike Seymour Apr 27 '13 at 13:46
3 Answers
It is destroying (as in calling the relevant destructor for) the object instance passed to delete
, and then "frees" the memory so that it can be used for other purposes.
The C++ standard states nothing about what the contents of the memory will be after delete
, and it is certainly not guaranteed to be zero or any other value - nor is it guaranteed that it is NOT zero - it may be zerod, it may retain all the values it had before, or some parts of it may be altered and others remain the same.
The goal of C and C++ as languages is to "only do the minimum necessary", so a typical memory free will not overwrite the "old" memory.
You could of course use code in the destructor to set the memory to zero before it is freed.
Since you are not supposed to use memory after it has been deleted, it shouldn't really matter.

- 126,704
- 14
- 140
- 227
-
recommending memset is the best bug sources. What you say about V-tables (something C++ does not specify, it isw just an implementation technique) is valid for all the class members as well: if they are non-trivial objects, they also have destructors that have to be executed after the class dtor returns. If you wipe out the memory, you mislead everything. – Emilio Garavaglia Apr 27 '13 at 12:57
-
Ok, removed the memset suggestion, as it's hard to explain when it can and can't be used - and it's pretty pointless anyway to do this in general. – Mats Petersson Apr 27 '13 at 13:03
-
Never use it. C++ manages objects, not memory. Unless you are writing a raw allocator class (a-la `std::allocator`) or overriding `new` / `delete`, you should never interface memory as a "bare metal". – Emilio Garavaglia Apr 27 '13 at 13:23
delete
just releases the memory (previously allocated by new
) and in case that some object has been stored within this memory, the destructor is also invoked.
delete
doesn't change the value of the pointer and neither it modifies the memory that has been released, thus you'll notice that many people are used to assign NULL
to this pointer after calling delete
just to make sure they will not end up with dereferencing invalid (dangling) pointer, which produces undefined behavior.
Worth to have a look at: Is it good practice to NULL a pointer after deleting it?
No, it does not mean setting the memory to any particular value+. The memory simply gets back into the heap of values that can be reused. The runtime often use several bytes of the returned chunk to store "bookkeeping" information, but they do not set the entire chunk to a particular value. Once a memory chunk is reused, it is your program that sets its new values.
+ There are memory profiling tools that let you set released memory to some "junk" values to make sure that you get a crash faster.

- 714,442
- 84
- 1,110
- 1,523