devxlogo

How Do ‘New’ and ‘Delete’ Actually Work?

How Do ‘New’ and ‘Delete’ Actually Work?

The C++ runtime library includes a heap manager (subroutines responsible for maintaining the heap). The heap manager does bookkeeping to keep track of which parts of the heap have been loaned out for usage (allocated), and which parts are still free (deallocated).
Every time a call to ‘new’ is made, the heap manager does the following things:
(1) It searches the free part of the heap for a chunk of memory that is big enough.
(2) It makes a record of the fact that the chunk of memory is now allocated.
(3) It returns the starting address of the allocated chunk as a result.
When you call delete, the heap manager updates its records to note that the block is free.

devx-admin

Share the Post: