f



delete and delete []

I understand that if you allocate an array using new as follows

object * b = new object[n];

then you must free the object as follows:

delete [] b;

however if you are allocating a char buffer as follows:

char * b = new char [n];

then isn't it irrelavant if you use

delete b or delete [] b ?

(Then only thing I can think of is that if you change the object type
from char to some other object then you might have some issues...)

0
bjobrien62 (537)
3/30/2007 5:11:43 PM
comp.lang.c++ 49423 articles. 5 followers. Post Follow

5 Replies
777 Views

Similar Articles

[PageSpeed] 34

"SpreadTooThin" <bjobrien62@gmail.com> wrote in message 
news:1175274703.360909.214000@p77g2000hsh.googlegroups.com...
>I understand that if you allocate an array using new as follows
>
> object * b = new object[n];
>
> then you must free the object as follows:
>
> delete [] b;

Correct.

> however if you are allocating a char buffer as follows:
>
> char * b = new char [n];
>
> then isn't it irrelavant if you use
>
> delete b or delete [] b ?

No, you must still use delete[].  It's still an
array (albeit whose objects are of a different type).

> (Then only thing I can think of is that if you change the object type
> from char to some other object then you might have some issues...)

The array's element type has no bearing on this.
If you use 'new', you must deallocate with 'delete'.
If you use 'new[]', you must deallocate with 'delete[]'.
Simple, huh?

-Mike


0
mkwahler (3821)
3/30/2007 5:18:29 PM
On Mar 30, 1:11 pm, "SpreadTooThin" <bjobrie...@gmail.com> wrote:
> however if you are allocating a char buffer as follows:
>
> char * b = new char [n];
>
> then isn't it irrelavant if you use
>
> delete b or delete [] b ?

No, not irrelevant.  If you allocate with [], delete with [].

0
3/30/2007 5:19:19 PM
SpreadTooThin wrote:
> I understand that if you allocate an array using new as follows
>
> object * b = new object[n];
>
> then you must free the object as follows:
>
> delete [] b;
>
> however if you are allocating a char buffer as follows:
>
> char * b = new char [n];
>
> then isn't it irrelavant if you use
>
> delete b or delete [] b ?

No, of course not.  Why do you think there might be a difference?
It's an array.  You use 'new[]' to allocate it, you use 'delete[]'
to free it.

> (Then only thing I can think of is that if you change the object type
> from char to some other object then you might have some issues...)

No, you can't change the object type.  That's nonsense, sorry.

V
-- 
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask 


0
v.Abazarov (13256)
3/30/2007 5:19:34 PM
On 30 Mar, 18:11, "SpreadTooThin" <bjobrie...@gmail.com> wrote:
> I understand that if you allocate an array using new as follows
>
> object * b = new object[n];
>
> then you must free the object as follows:
>
> delete [] b;
>
> however if you are allocating a char buffer as follows:
>
> char * b = new char [n];
>
> then isn't it irrelavant if you use
>
> delete b or delete [] b ?

Who told you that? It certainly is relevant. The behaviour of mixing
new [] with delete is undefined.

> (Then only thing I can think of is that if you change the object type
> from char to some other object then you might have some issues...)

As ever with undefined behaviour, whether you have issues, what those
issues are, whether they are different for different types etc. is
outside the scope of the language definition.

Gavin Deane

0
deane_gavin (897)
3/30/2007 5:29:30 PM
On Mar 30, 1:11 pm, "SpreadTooThin" <bjobrie...@gmail.com> wrote:
> however if you are allocating a char buffer as follows:
> char * b = new char [n];
> then isn't it irrelavant if you use
> delete b or delete [] b ?

You seem to be misunderstanding what delete & delete[] do.

basically, they functions like:
delete pObject;  =

       pObject->~object();
       free_n_bytes_of_memory(pObject, sizeof(object));


delete[] arrObject;   =

      for(int i =0; i < N; ++i)
         arrObject[i].~object();
       free_n_bytes_of_memory(arrObject, sizeof(object)* N );


Nw, you are correct in surmizing that, as a char dtor is empty, the
different in the first part of each is irrelevent.  However, the
difference in the second half is still significant.

To further confuse the issue, on most platforms,
free_n_bytes_of_memory could probably be implemented as:

void free_n_bytes_of_memory(void* ptr, int )
{
    free(ptr);
}

with the size parameter ignored, but that would be an implementation
detail, which you can't and shouldn't depend on.


0
3/30/2007 8:47:03 PM
Reply: