delete and delete[]

Hi Everybody.

I'm having a question about these operands

if we have a declaration, let's say

ClassA* a = new ClassA[256];

which of these we should use to delete the memory block? And why?
Number 1
delete a;

Number 2
delete[] a;

Number 3
1
2
3
4
for(int i = 0; i<256; i++)
{
delete a[i];
}


And, I guess, Number 3 Option, is used when the objects are already allocated? or they do allocate when we make the declaration of

ClassA* a = new ClassA[256];

All of they do allocate? or we need to allocate like:

a[0] = new ClassA;

for each object?
Last edited on
delete is for just raw pointers, wich pretty much kicks out no. 1 and 3.
Everything you allocate with new [] has to be deallocated with delete [], and everything you allocate with new has to be deallocated with delete.
There is no reason, this is how it will work correctly.
yes, there is a reason. The new operator allocates a pointer, which is just a pointer, while new[] allocates an array, which is way different than just a pointer, except that it is internaly a pointer to the first element. But the compiler know which is which, and you must use new ... delete ONLY for raw pointers, and new [] ... delete [] ONLY for arrays. No. 1 uses delete for an array, and no. 3 uses it for an object of type ClassA, both of wich is impossible.
Topic archived. No new replies allowed.