🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.std.c++
Article #2116

Re: object contruction/destruction in c++, design flaw?

#2116
From: Dietmar Kuehl
Date: Thu, 05 Oct 1995 08:27
55 lines
2098 bytes
Hi,

auto_ptr doesn't seem to be usable for array objects...

Fergus Henderson (fjh@munta.cs.mu.OZ.AU) wrote:
: Use the following instead:
: 
: 	class a {
: 	public:
: 		auto_ptr<char> mBuffer1;
: 		auto_ptr<char> mBuffer2;
: 		a();
: 		~a();
: 	}
: 
: 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.

I doubt this statement. Doesn't this result in "undefined behavior"?

	[Moderator's note: yes, you're absolutely right.  My mistake.
		-fjh.]

According to lib.auto.ptr.cons the destructor of auto_ptr results in
"delete get()". But applying "operator delete" to an array object (the
result of "new char[1000]" is an array object: expr.new verse 7) yields
undefined behavior (expr.delete). Is this yet another problem for the
auto_ptr class that it can't deal with array objects or am I missing
something (maybe I'm not up to date with the latest state of the
standard...)?

dk
--
http://www.informatik.uni-konstanz.de/~kuehl
dietmar.kuehl@uni-konstanz.de
I am a realistic optimist - that's why I appear to be slightly pessimistic
---
[ 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: <44tj4s$2j1@eurybia.rz.uni-konstanz.de>
Path: arc1-ca.pugleaf.net!in2.uu.net!bounce-back
References: <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:27:26 GMT
Approved: fjh@cs.mu.oz.au
From: kuehl@uzwil.rz.uni-konstanz.de (Dietmar Kuehl)
Newsgroups: comp.std.c++
Subject: Re: object contruction/destruction in c++, design flaw?
X-Original-Date: 4 Oct 1995 09:11:56 GMT
Organization: Fakultdt f|r Mathematik und Informatik
Message-ID: <44tj4s$2j1@eurybia.rz.uni-konstanz.de>
References: <9527516.21208@mulga.cs.mu.OZ.AU>
Reply-To: Dietmar.Kuehl@uni-konstanz.de
X-Newsreader: TIN [version 1.1 PL6]