Topic: Is delete [] always safe?


Author: steve@taumet.com (Steve Clamage)
Date: Mon, 22 Mar 1993 18:20:57 GMT
Raw View
dave@fcom.cc.utah.edu (Dave Martin) writes:

>Is it safe to do a delete [] pointer when pointer is not a vector?

No, it is never safe.

> What does/will the standard say?

The same thing the ARM already says.  The form of the delete-expression
must match the form of the new-expression.  You use the "[]" form in
the delete-expression if and only if the corresponding new-expression
used that form.  Anything else is an error and has undefined results.
--

Steve Clamage, TauMetric Corp, steve@taumet.com




Author: eomiya@borland.com (Elliot Omiya)
Date: Tue, 23 Mar 1993 00:46:04 GMT
Raw View
ARM page 65 Section 5.3.4 "Delete"

"The effect of deleting an array with the plain delete syntax is undefined,
as is deleting an individual object with the delete [] syntax."

In other words, no it is not safe.

--
-----------------------------------------------------------------------------
Elliot H. Omiya, KC6DAL     Borland International Inc.  -=<EHO>=-
-----------------------------------------------------------------------------




Author: davidm@consilium.com (David S. Masterson)
Date: 25 Mar 93 00:08:50 GMT
Raw View
>>>>> On 22 Mar 93 18:20:57 GMT, steve@taumet.com (Steve Clamage) said:

> dave@fcom.cc.utah.edu (Dave Martin) writes:

>>Is it safe to do a delete [] pointer when pointer is not a vector?

> No, it is never safe.

>> What does/will the standard say?

> The same thing the ARM already says.  The form of the delete-expression must
> match the form of the new-expression.  You use the "[]" form in the
> delete-expression if and only if the corresponding new-expression used that
> form.  Anything else is an error and has undefined results.

I've always wondered what the reasoning was for having two different forms for
the delete operation (scalar vs. array)?  Couldn't 'delete ptr' have handled
both cases?  Was the reason syntatic (ie. to balance the language), technical
(ie. less overhead), or something else?
--
====================================================================
David Masterson     Consilium, Inc.
(415) 691-6311     640 Clyde Ct.
davidm@consilium.com    Mtn. View, CA  94043
====================================================================
"Once you've had real champagne, you can never go back to Asti Spimanti."
-- Georgette Lundberg




Author: philr@dspcproj.demon.co.uk (Phil Reynolds)
Date: 25 Mar 93 11:16:04 GMT
Raw View
In article <1993Mar22.024915.14564@fcom.cc.utah.edu> dave@csulx.weber.edu writes:

> Is it safe to do a delete [] pointer when pointer is not a vector?
[ Rest of post deleted ... ]
>
> --
> dave@csulx.weber.edu (sig by ByteWatchers & TransmitFast)
>

I shall try to explain why this is not safe!

The syntax 'new Class [ Size ]' causes the compiler to generate
constructor calls for each element in the array. As well as
doing this it adds some information to the block of memory
allocated to tell it how many items there are in the array. This
is so when you use the delete [] syntax, it knows how many
destructors to call and how much memory to free.

Now it may be that your compiler always treats 'new Class' as
'new Class[1]' and thus delete [] will work but don't rely on it.

P.S. In the old days (when I were a lad) you had to provide the
size of the array to the delete operator e.g. delete [ Size ] pArray.

+-------------------------------------+
|Phil Reynolds                        |
|PC Projects                          |
|Datastream International Ltd.        |
|philr@dspcproj.demon.co.uk (I think!)|
+-------------------------------------+




Author: dano@dogfish.boeing.com (Dan Olson)
Date: Wed, 31 Mar 1993 01:16:32 GMT
Raw View
What I want to know, why is 'delete [] ptr' syntax even needed?  The
total size of memory pointed to by ptr is known and so is the sizeof
*ptr, so the number of elements is known.  Why not just have
'delete ptr' call the dtor for each element in the list?

Will someone enlighten me?


--
    UUCP: ..!uunet!bcstec!plato!dogfish!dano
Internet:  dano@dogfish.ds.boeing.com




Author: rvloon@cv.ruu.nl (Ronald van Loon)
Date: Wed, 31 Mar 1993 08:48:50 GMT
Raw View
In <C4qBJL.5AH@plato.ds.boeing.com> dano@dogfish.boeing.com (Dan Olson) writes:

|"What I want to know, why is 'delete [] ptr' syntax even needed?  The
|"total size of memory pointed to by ptr is known and so is the sizeof
|"*ptr, so the number of elements is known.  Why not just have
|"'delete ptr' call the dtor for each element in the list?
|"
|"Will someone enlighten me?

Because there is a difference between deleting an array of objects and
deleting the objects in the array themselves. Now, imagine an array of arrays
of objects, which is dynamically allocated. At one point I want to delete this
array of arrays, but not the arrays themselves, so I use delete ptr and not
delete [] ptr.
--
Ronald van Loon     | Consider this: In the United States, an automobile is
(rvloon@cv.ruu.nl)  | stolen EVERY 14.7 SECONDS.
3DCV Group, Utrecht |   If that statistic scares you, think how we felt when we
The Netherlands     | made it up. - Dave Barry, "CHRISTMAS BUYERS GUIDE"




Author: dave@fcom.cc.utah.edu (Dave Martin)
Date: Mon, 22 Mar 93 02:49:15 GMT
Raw View
Is it safe to do a delete [] pointer when pointer is not a vector?
It is not safe in the gnu gcc2.3.3 compiler.  What does/will the standard
say?  Is the only reason to require the [] on vectors to clue in the compiler
so it doesn't have to check at runtime whether it is deleteing a vector
( by seeing if sizeof(type) is < sizeof memory buffer) or is there some other
reason?  I had a program that was relying on delete [] ptr being safe for
both vectors and non-vectors until it broke.  Do I need to have special
cases?

--
dave@csulx.weber.edu (sig by ByteWatchers & TransmitFast)