Topic: please tell me diffrent between "delete" and "delete[]


Author: "qj" <injiwy@ya.com>
Date: Wed, 28 Nov 2001 17:48:51 GMT
Raw View
delete
delete[]

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]





Author: Adam Peterson <ahp6@email.byu.edu>
Date: Wed, 28 Nov 2001 18:19:29 GMT
Raw View
> delete
> delete[]

delete is used to deallocate objects allocated with new.  delete[] is used
to deallocate objects with new[].  Pure and simple, that's the only
difference.  Confuse them and you invoke undefined behavior.

ex:

std::string data;

data=new string;
delete data;
data=new string[1];
delete[] data;


---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]





Author: "Marcin 'Qrczak' Kowalczyk" <qrczak@knm.org.pl>
Date: Wed, 28 Nov 2001 20:06:57 GMT
Raw View
Wed, 28 Nov 2001 18:19:29 GMT, Adam Peterson <ahp6@email.byu.edu> pisze:

> delete is used to deallocate objects allocated with new.
> delete[] is used to deallocate objects with new[].

It's not strictly true. delete[] is used for arrays, delete is used
for all other types. So:

typedef int arr[10];
...
    int *x = new arr;
    delete[] x; // Not delete!

--
 __("<  Marcin Kowalczyk * qrczak@knm.org.pl http://qrczak.ids.net.pl/
 \__/
  ^^
QRCZAK

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]





Author: Francis Glassborow <francis.glassborow@ntlworld.com>
Date: Wed, 28 Nov 2001 20:15:43 GMT
Raw View
In article <9u1bbs$r50$1@mail.cn99.com>, qj <injiwy@ya.com> writes
>delete
>delete[]
Yes, but not here. And any good book should be able to tell you as long
as you know if you are talking about operators or expressions. If you do
not know the difference you probably need to do a bit more reading.

Try comp.lang.c++.moderated or alt.comp.lang.learn.c-c++


--
Francis Glassborow
I offer my sympathy and prayers to all those who are suffering
as a result of the events of September 11 2001.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]





Author: Adam Peterson <ahp6@email.byu.edu>
Date: Thu, 29 Nov 2001 16:41:38 GMT
Raw View
"Marcin 'Qrczak' Kowalczyk" <qrczak@knm.org.pl> wrote in message
news:slrna0agv5.pp.qrczak@qrnik.zagroda...
> Wed, 28 Nov 2001 18:19:29 GMT, Adam Peterson <ahp6@email.byu.edu> pisze:
>
> > delete is used to deallocate objects allocated with new.
> > delete[] is used to deallocate objects with new[].
>
> It's not strictly true. delete[] is used for arrays, delete is used
> for all other types. So:
>
> typedef int arr[10];
> ...
>     int *x = new arr;
>     delete[] x; // Not delete!

Well, there is a potential gotcha here.  However, the principal still holds.
x is initialized by calling operator new[](), not operator new().  Hence, it
should be deallocated with operator delete[](), not operator delete().  It
may not be obvious that the buffer is allocated with new[], but it is.


---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]





Author: Steve Clamage <clamage@eng.sun.com>
Date: Thu, 29 Nov 2001 16:44:46 GMT
Raw View
On Wed, 28 Nov 2001, Marcin 'Qrczak' Kowalczyk wrote:
> Wed, 28 Nov 2001 18:19:29 GMT, Adam Peterson <ahp6@email.byu.edu> pisze:
>
> > delete is used to deallocate objects allocated with new.
> > delete[] is used to deallocate objects with new[].
>
> It's not strictly true.

Yes it is.

>delete[] is used for arrays, delete is used
> for all other types.

Not strictly true:

 int arr[10];  // arr is an array
 delete[] arr; // error

>So:
>
> typedef int arr[10];
> ...
>     int *x = new arr;
>     delete[] x; // Not delete!

Yes. delete[] is used only to deallocate objects allocated with new[].

--
Steve Clamage, stephen.clamage@sun.com

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]