Say I have two items on the heap:
Foo *f = new Foo;
Foo *g = new Foo[42];
And say I have a function that receives a Foo pointer and within the function it needs to perform a delete:
void bar(Foo *p) {
// some stuff
delete p;
}
This function might be called like so:
bar(f); // passing a pointer to a Foo object on the heap
bar(g); // passing a pointer to an array on the heap
I recognize that delete[] and delete should be used to free memory allocated with new[] and new respectively; However since the function doesn't know if its parameter p was allocated with new or new[], how can this function properly delete or delete[]?