Topic: deleting one-dimensional arrays
Author: jgro@netcom.COM (Jeremy Grodberg)
Date: 19 Nov 91 07:52:22 GMT Raw View
Well, we just cannot seem to agree on this one here: the people who are
supposed to know disagree, and the ARM is unclear. In the version of C++
described in the ARM, should an array created like this:
char* str = new char[40];
Be deleted like this:
delete str;
or like this:
delete [] str;
????
The arm says that you need to use the second form when the pointer points
to an array, which str does, in some sense, but it also says that
new char[40] returns a char*, and not a pointer to an array that is part
of a multi-dimensional array. So the bottom-line question is , do you
use delete [] on single-dimension arrays?
Anyone on the ANSI committee got the answer to this one (and an explanation
I can take to work)?
Thanks.
--
Jeremy Grodberg Commited to developing user-friendly products,
jgro@netcom.com Because technology is supposed to make life easier
Author: edelson@xombul.inria.fr (Daniel Edelson)
Date: 19 Nov 91 10:52:57 GMT Raw View
In article <1991Nov19.075222.21001jgro@netcom.COM>, jgro@netcom.COM (Jeremy Grodberg) writes:
|> char* str = new char[40];
|>
|> Be deleted like this:
|>
|> delete str;
|>
|> or like this:
|>
|> delete [] str;
|>
|> ????
|>
|> The arm says that you need to use the second form when the pointer points
|> to an array, which str does, in some sense, but it also says that
|> new char[40] returns a char*, and not a pointer to an array that is part
|> of a multi-dimensional array.
It should be deleted with
delete [] str;
The object that str points to is an array in every sense of the word,
not being part of a multidimensional array notwithstanding.
Using the form:
delete str;
would tell the memory allocator that you are deleting a single char
whose address is str. Obviously, this is not the right thing because
str points at 40 chars.
It sounds in your posting as though you expect new to return
something of type (char[]), for example, for an array of chars.
Author: pew@cs.brown.edu (Peter E. Wagner)
Date: 19 Nov 91 15:50:38 GMT Raw View
In article <2694@seti.inria.fr>, edelson@xombul.inria.fr (Daniel Edelson) writes:
|> In article <1991Nov19.075222.21001jgro@netcom.COM>, jgro@netcom.COM (Jeremy Grodberg) writes:
|>
|> |> char* str = new char[40];
|> |>
|> |> Be deleted like this:
|> |>
|> |> delete str;
|> |>
|> |> or like this:
|> |>
|> |> delete [] str;
|> |>
|> |> ????
|> |>
|> |> The arm says that you need to use the second form when the pointer points
|> |> to an array, which str does, in some sense, but it also says that
|> |> new char[40] returns a char*, and not a pointer to an array that is part
|> |> of a multi-dimensional array.
|>
|> It should be deleted with
|> delete [] str;
|> The object that str points to is an array in every sense of the word,
|> not being part of a multidimensional array notwithstanding.
|>
|> Using the form:
|> delete str;
|> would tell the memory allocator that you are deleting a single char
|> whose address is str. Obviously, this is not the right thing because
|> str points at 40 chars.
|>
|> It sounds in your posting as though you expect new to return
|> something of type (char[]), for example, for an array of chars.
|> From the ARM, 5.3.3, ``The new operator returns a pointer to the
|> object created. WHen that object is an array, a pointer to its
|> initial element is returned.''
|>
|> The type of the return value of (new char[40]) may indeed be
|> (char *) rather than (char []), but the object is still an array.
|>
Is delete that different from free()? From the above, it sounds like
delete does not rely on the size field that lies just before the
allocated block. Why not?
Peter
--
----------------------------------------------------------------
Peter E. Wagner (401)863-7685 pew@cs.brown.edu
Department Computer Science Box 1910 pew@BROWNCS.BITNET
Brown University, Providence, RI 02912 uunet!brunix!pew
----------------------------------------------------------------
Author: steve@taumet.com (Stephen D. Clamage)
Date: 19 Nov 91 18:07:02 GMT Raw View
jgro@netcom.COM (Jeremy Grodberg) writes:
|Well, we just cannot seem to agree on this one here: the people who are
|supposed to know disagree, and the ARM is unclear. In the version of C++
|described in the ARM, should an array created like this:
| char* str = new char[40];
|Be deleted like this:
| delete str;
|or like this:
| delete [] str;
|The arm says that you need to use the second form when the pointer points
|to an array, which str does, in some sense, but it also says that
|new char[40] returns a char*, and not a pointer to an array that is part
|of a multi-dimensional array. So the bottom-line question is , do you
|use delete [] on single-dimension arrays?
You use delete[] on single-dimension arrays. There are no multi-
dimensional arrays in C or C++. The closest thing to a multi-dimensional
array is an array whose elements are of array type.
Someone is being entirely too obtuse about this. No matter how carefully
one writes a specification, it is always possible to extract a phrase
out of context and willfully misunderstand it.
At the bottom of page 64 of the ARM, it says:
"The form
delete [ ] _expression_
is used to delete arrays. The expression points to an array."
It doesn't say "The expression is of type 'pointer to array'."
In your example, 'str' is not an array; it points to (the first element
of) an array.
--
Steve Clamage, TauMetric Corp, steve@taumet.com
Author: Bruce.Hoult@actrix.gen.nz (Bruce Hoult)
Date: 20 Nov 91 03:57:25 GMT Raw View
In article <93543@brunix.UUCP> pew@cs.brown.edu (Peter E. Wagner) writes:
>
> Is delete that different from free()? From the above, it sounds like
> delete does not rely on the size field that lies just before the
> allocated block. Why not?
>
> Peter
Yes it does, but it has to know whether to call one destructor (for a single
object) or many (for an array).
--
Bruce.Hoult@bbs.actrix.gen.nz Twisted pair: +64 4 477 2116
BIX: brucehoult Last Resort: PO Box 4145 Wellington, NZ
"And they shall beat their swords into plowshares, for if you hit a man
with a plowshare, he's going to know he's been hit."
Author: pal@xanadu.wpd.sgi.com (Anil Pal)
Date: 20 Nov 91 01:04:08 GMT Raw View
In article <93543@brunix.UUCP>, pew@cs.brown.edu (Peter E. Wagner) writes:
|> Is delete that different from free()? From the above, it sounds like
|> delete does not rely on the size field that lies just before the
|> allocated block. Why not?
delete is different from free() in that it calls destructors for the
objects freed. If called as delete ptr; it calls exactly one
destructor, if called as delete [] ptr;, it calls destructors for each
element in the array. For a char* ptr, this difference doesn't
matter, but there is nonetheless a clear semantic difference, and so
delete [] is the "right" way.
Most implementations that I am aware of do indeed use malloc() and
free() underneath new and delete, and so will free the correct amount
of storage even if called incorrectly, but a well-written program will
not rely on this behavior.
--
Anil A. Pal, Silicon Graphics, Inc.
pal@wpd.sgi.com (415)-335-7279
Author: steve@taumet.com (Stephen D. Clamage)
Date: 20 Nov 91 17:49:38 GMT Raw View
pew@cs.brown.edu (Peter E. Wagner) writes:
>Is delete that different from free()? From the above, it sounds like
>delete does not rely on the size field that lies just before the
>allocated block. Why not?
Delete is not free(). It is built into the language, rather than just
being defined as a library function. It has semantic consequences
(calling destructors), and it is up to the implementation to ensure
the right things happen.
Originally, when you allocated an array of objects with 'new', you
had to tell 'delete' how many objects were in the array. This is
the obvious and easiest implementation of new/delete. It created
a bad situation for the programmer, though, since it is error-prone
and inconvenient for the program to keep track of the size of the
array. (Suppose you have a function which reads the number of members
of an array from a file. This has to be stored somewhere in order
to delete the array later.)
The syntax was changed so that the implementation, rather than the
programmer, is obligated to keep track of the number of objects
in the array. Current implementations generally use some sort of
associative array of address-vs-size.
--
Steve Clamage, TauMetric Corp, steve@taumet.com