Article View: comp.std.c++
Article #364delete [] p
From: Tom Kelly
Date: Thu, 16 Aug 1990 20:27
Date: Thu, 16 Aug 1990 20:27
39 lines
1397 bytes
1397 bytes
The recent discussion of delete [] in comp.lang.c++ got me to thinking
about some example C++ code I've seen.
For example, in Lippman, _C++ Primer_, p. 234:
(with int len; char *str)
String::String(char *s)
{
len = strlen(s);
str = new char[len + 1];
strcpy(str, s);
}
String::~String()
{
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".
The annotations note that there is a practical difference only for
classes that have destructors, and of course, "char" has no
destructor. But the annotation further points out that it is
possible that during program evolution, a built-in type can be
replaced by a class with a destructor (using typedef) and so this
is a dangerous practice.
Comments?
Tom Kelly (416) 922-1937
SCO Canada, Inc. (formerly HCR) 130 Bloor St. W., Toronto, Ontario, Canada
{utzoo, utcsri, uunet}!scocan!tom
Technical Details
Message-ID:
<1990Aug16.162718.14828@sco.COM>Path:
arc1-ca.pugleaf.net!utzoo!attcan!ncrcan!scocan!tomShow Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!attcan!ncrcan!scocan!tom From: tom@sco.COM (Tom Kelly) Newsgroups: comp.std.c++ Subject: delete [] p Message-ID: <1990Aug16.162718.14828@sco.COM> Date: 16 Aug 90 20:27:18 GMT Organization: SCO Canada, Inc. (formerly HCR Corporation) Lines: 39