Reputation: 2764
When passing around heap allocated arrays at some point we call:
delete [] ptrName
How does the compiler remember how much memory needs to be de-allocated?
Context:
We've got several programs that throw around pointers to byte arrays on the heap, for various reasons casting between int32, int8 and others as appropriate. When the memory needs de-allocating with a delete call what exactly goes on to make sure the right amount is freed?
Thanks
Upvotes: 0
Views: 66
Reputation: 45470
Compiler allocates x
bytes of overhead which has the allocated memory information, it knows how much to delete when delete []
is called.
§ 5.3.4 NEW
Here, x and y are non-negative unspecified values representing array allocation overhead; the result of the new-expression will be offset by this amount from the value returned by operator new[]. This overhead may be applied in all array new-expressions, including those referencing the library function operator new[](std::size_t, void*) and other placement allocation
Upvotes: 1
Reputation: 55897
There is nothing in standard about it. Developers of compiler make decisions, where to store how much memory needs to be deallocated, by writing in some place amount of memory, that will be allocated by new.
Upvotes: 0