OurClass *p = new OurClass();
and try to delete like this:-
then we are going to end up in trouble. Ofcourse we know that we will end up in trouble. But I have really not given a thought HOW ?
When we allocate an array of items eg. OurClass pa = new pa(), the compiler actually allocates the necessary amount of memory, calls the ctors for each allocated class and also prefixes the block of memory of the 'n' items allocated with the number of items allocated.
NumItems | OurClassObject1 | OurClassObject2 | ...... | OurClassObjectn
But pa always points to the first item in the allocation, thereby the item count prefix remains hidden. When we call delete pa, the compiler uses the item count prefix to delete the allocated objects and call the dtors.
Now i thin…