Pages

23 Apr 2012

How delete[] works?

Let's consider the following code,

int size = 10;
A* pA = new A[size];
......
delete [] pA;

When we allocate an array of objects with new, allocated memory will have size stored on top of memory layout and then continuous memory for object * size. Now when we do a delete[] with out specifying how many to delete , size  will be read first and then destructor will be called for all objects.

This is again the reason why unexpected behavior when delete [] is used on new.

No comments:

Post a Comment