HiveBrain v1.2.0
Get Started
← Back to all entries
patterncppCritical

How does delete[] "know" the size of the operand array?

Submitted by: @import:stackoverflow-api··
0
Viewed 0 times
arrayoperandhowdeletethedoessizeknow

Problem

Foo* set = new Foo[100];
// ...
delete [] set;


You don't pass the array's boundaries to delete[]. But where is that information stored? Is it standardised?

Solution

When you allocate memory on the heap, your allocator will keep track of how much memory you have allocated. This is usually stored in a "head" segment just before the memory that you get allocated. That way when it's time to free the memory, the de-allocator knows exactly how much memory to free.

Context

Stack Overflow Q#197675, score: 234

Revisions (0)

No revisions yet.