How does delete work? [duplicate]

In general, this is implementation-dependent.

The way this is usually implemented is that new/malloc will allocate slightly more bytes than you asked for, and will use the extra bytes for some book-keeping: many times the allocated blocks will also be maintained in a linked-list, and so the extra bytes will be used to store the next/prev pointer, as well as the size of the block.

This is not mandatory, however. The new/malloc calls can just as well store your pointer and the block’s size in, say, a map.

If you’re interested in a specific implementation, you will have to provide more information (e.g. what runtime/compiler are you using?).

Leave a Comment