Article View: comp.std.c++
Article #2114Re: object contruction/destruction in c++, design flaw?
From: Lars Farm
Date: Thu, 05 Oct 1995 08:22
Date: Thu, 05 Oct 1995 08:22
74 lines
3212 bytes
3212 bytes
In article <44odn6$c61@gabi.gabi-soft.fr>,
kanze@gabi-soft.fr (J. Kanze) wrote:
>Basically, the most correct way of writting this would be to change the
>pointer declarations to auto_ptr, and forget the try/catch block
>completely. In fact, I would generally write it this way, with the
>initialization in the initialization part of the constructor, e.g.:
>
> a::a()
> : mBuffer1( new char[ 1000 ] )
> , mBuffer2( new char[ 1000 ] )
> {
> }
In article <9527516.21208@mulga.cs.mu.OZ.AU>,
fjh@munta.cs.mu.OZ.AU (Fergus Henderson) wrote:
>With auto_ptr, this becomes just
>
> a::a()
> {
> mBuffer1 = new char[1000];
> mBuffer2 = new char[1000];
> }
>
>or alternately
>
> a::a()
> : mBuffer1(new char[1000]),
> mBuffer2(new char[1000])
> {}
[...]
>With auto_ptr, you don't even need a destructor - the compiler-generated
>one will do the right thing.
auto_ptr does delete p; not delete[] p; is this "the right thing" when you
do p = new char[1000]? If so, please disregard the rest!
[Moderator's note: Lars Farm is right. My suggestion was
incorrect. -fjh.]
This demonstrates the confusion that comes from new/new[]. Other posts
defending two kinds of new have claimed that new/new[] is no problem and
users are/should/must be able to differentiate between new/new[] and
delete/delete[]. Fergus Henderson and James Kanze clearly are experts and
both experts did the same thing! If they miss this, ordinary users will
too, and much more so.
The motivation for splitting new into new/new[] is supposed to be speed and
size optimization. I would like to know what the actual quantified gain of
splitting new into new/new[] is. Can anyone quantify
(speed%,heapsize%,codesize%) the total gains in (1) a typical C++ program
and in (2) an extreme but complete working C++ program allocating lots and
lots of tiny single objects? I'm not interested in the microlevel of one
single object, but rather the effects on the entire programs.
The cost of splitting new into new/new[] is clear and demonstrated above.
Users must remember that (1) new != new[] (2) new belongs to delete not
delete[] (3) new[] belongs to delete[] not delete. (4) when to use which.
The language becomes harder to learn, use (even for experts) and teach.
If the gains turn out "small" wouldn't it be reasonable to merge new/new[]
and delete/delete[] so that new X is the same as new X[1]? Wouldn't the
simplification be worth it?
--
Lars Farm, lars.farm@nts.mh.se
---
[ comp.std.c++ is moderated. Submission address: std-c++@ncar.ucar.edu.
Contact address: std-c++-request@ncar.ucar.edu. The moderation policy
is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]
Technical Details
Message-ID:
<AC97F8D996681602E@sleipner.nts.mh.se>Path:
arc1-ca.pugleaf.net!in2.uu.net!bounce-backReferences:
<henk_kampman-2909951146290001@huisie-ppp1.knoware.nl> <9527516.21208@mulga.cs.mu.OZ.AU>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!in2.uu.net!bounce-back Date: 05 Oct 95 08:22:54 GMT Approved: fjh@cs.mu.oz.au From: lars.farm@nts.mh.se (Lars Farm) Newsgroups: comp.std.c++ Subject: Re: object contruction/destruction in c++, design flaw? X-Original-Date: Wed, 04 Oct 1995 08:39:53 +0100 Organization: pv Message-ID: <AC97F8D996681602E@sleipner.nts.mh.se> References: <henk_kampman-2909951146290001@huisie-ppp1.knoware.nl> <9527516.21208@mulga.cs.mu.OZ.AU>