Article View: comp.std.c++
Article #373Re: delete [] p
From: David R Kohr
Date: Sun, 19 Aug 1990 17:02
Date: Sun, 19 Aug 1990 17:02
59 lines
3347 bytes
3347 bytes
In article <1231@lupine.NCD.COM> rfg@NCD.COM (Ron Guilmette) writes:
>In article <1990Aug16.162718.14828@sco.COM> tom@sco.COM (Tom Kelly) writes:
><The recent discussion of delete [] in comp.lang.c++ got me to thinking
><about some example C++ code I've seen.
><
>...
>< delete str;
>...
><
><The same code (essentially) appears in Dewhurst & Stark,
><_Programming in C++_, p. 70.
><
><It seems to me that the delete statement is incorrect, it
><should be delete []. According to E&S (ARM), section 5.3.4, p. 21:
><
><"The effect of deleting an array with the plain delete syntax
><is undefined".
>
>I believe that it would be useful to also have a compile-time enforcable
>constraint here, e.g.:
>
> It is illegal for the pointer given in a plain delete
> statement to be of any pointer-to-array type.
>--
>
>// Ron Guilmette - C++ Entomologist
>// Internet: rfg@ncd.com uucp: ...uunet!lupine!rfg
>// Motto: If it sticks, force it. If it breaks, it needed replacing anyway.
I'm still basically learning C++, so please pardon any obvious instances
of ignorance in the question which follows. Also, I hope this question
is not so basic that it should really have been posted to comp.lang.c++;
unfortunately, this thread of discussion isn't being mirrored in that
newsgroup, so I think I have to post here.
In K&R and ANSI C, of course you must specify the size of a region of
memory to allocate when using malloc(), but when returning that memory
using free() you merely need to pass a pointer to the memory. This
implies that the implementation of the memory allocator must be able
to figure out for itself the size of the memory region when it is
returned using free(). This can easily be handled either by using
a "hidden" size field in a header immediately preceding the region returned
by malloc(), or by maintaining a database which records the size of
the memory associated with each pointer returned by malloc() but not
yet recovered by free(). (The former technique seems more efficient,
and is presented in K&R itself.)
But in C++, the new and delete operators seem to have lost this
capability of calculating the size of allocated memory regions
automatically. I was under the impression that most implementations
of C++ would naturally use malloc() and free() as the underlying
mechanisms for implementing new and delete, so that it would not be
unreasonable to require that delete be able to figure out for itself
the size of an allocated region of memory. Why is this not the case?
--
David R. Kohr M.I.T. Lincoln Laboratory Group 45 ("Radars 'R' Us")
email: DRK@ATHENA.MIT.EDU (preferred) or KOHR@LL.LL.MIT.EDU
phone: (617)981-0775 (work) or (617)527-3908 (home)
Technical Details
Message-ID:
<1990Aug19.170256.21630@athena.mit.edu>Path:
arc1-ca.pugleaf.net!utzoo!attcan!utgpu!news-server.csri.toronto.edu!mailrus!uwm.edu!zaphod.mps.ohio-state.edu!usc!snorkelwacker!bloom-beacon!drkReferences:
<1990Aug16.162718.14828@sco.COM> <1231@lupine.NCD.COM>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!attcan!utgpu!news-server.csri.toronto.edu!mailrus!uwm.edu!zaphod.mps.ohio-state.edu!usc!snorkelwacker!bloom-beacon!drk From: drk@athena.mit.edu (David R Kohr) Newsgroups: comp.std.c++ Subject: Re: delete [] p Summary: Do new and delete typically rely on malloc() and free()? Message-ID: <1990Aug19.170256.21630@athena.mit.edu> Date: 19 Aug 90 17:02:56 GMT References: <1990Aug16.162718.14828@sco.COM> <1231@lupine.NCD.COM> Sender: daemon@athena.mit.edu (Mr Background) Organization: Massachusetts Institute of Technology Lines: 59