Sparky
Sparky

Reputation: 2764

specific query on array de-allocation on the heap

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

Answers (2)

billz
billz

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

  • new T[5] results in a call of operator new, and
  • new(2,f) T[5] results in a call of operator 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

ForEveR
ForEveR

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

Related Questions