Article View: comp.std.c++
Article #3211Re: Cleaning auto_ptr copy semantics.
From: Joe Buck
Date: Fri, 02 Feb 1996 21:54
Date: Fri, 02 Feb 1996 21:54
40 lines
2385 bytes
2385 bytes
gregor@netcom.com (Greg Colvin) writes:
>The smallest change I can see that preserves the semantics of strict
>ownership is to separate the concepts of "holding a pointer" and "owning
>an object", so that more than one auto_ptr can hold a pointer to an object,
>but only one auto_ptr can own the object.
Your new proposal is a recipe for dangling pointers. What about the
following program:
main () {
auto_ptr<int> outer = new int;
{
auto_ptr<int> inner = outer; // inner now owns the int
// end of inner's scope, int is deleted
}
*outer = 1; // crash
}
Under the old rules for auto_ptr, after the copy constructor for inner,
outer's internal pointer would be null, and I could test for this
(outer.get() == 0). Under your proposed change, though, outer contains
a dangling pointer.
I don't see a way around reference counts if you want a class that
contains a pointer and can do copies and assignments using the expected
semantics and guarantees to clean up the heap object.
We could still have plain auto_ptr, but with no assignment operator and no
copy constructor, and with an explicit member function for transferring
ownership. This means that a function cannot return an auto_ptr, but it
could still return one through a reference parameter.
--
-- Joe Buck <jbuck@synopsys.com> (not speaking for Synopsys, Inc)
Work for something because it is good,
not just because it stands a chance to succeed. -- Vaclav Havel
---
[ comp.std.c++ is moderated. Submission address: std-c++@ncar.ucar.edu.
Contact address: std-c++-request@ncar.ucar.edu. The moderation policy is
in http://reality.sgi.com/employees/austern_mti/std-c++/policy.html. ]
Technical Details
Message-ID:
<4etq2h$acg@hermes.synopsys.com>Path:
arc1-ca.pugleaf.net!fido.asd.sgi.com!austernReferences:
<gregorDLrrun.K2x@netcom.com>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!fido.asd.sgi.com!austern From: jbuck@Synopsys.COM (Joe Buck) Newsgroups: comp.std.c++ Subject: Re: Cleaning auto_ptr copy semantics. Date: 02 Feb 1996 13:54:53 PST Organization: Synopsys Inc., Mountain View, CA 94043-4033 Approved: austern@isolde.mti.sgi.com Message-ID: <4etq2h$acg@hermes.synopsys.com> References: <gregorDLrrun.K2x@netcom.com> NNTP-Posting-Host: isolde.mti.sgi.com X-Original-Date: 2 Feb 1996 19:52:49 GMT X-Auth: PGPMoose V1.1 PGP comp.std.c++ iQBVAwUBMRKIPUy4NqrwXLNJAQGMNwIAyfg6XuO5fQk9WZqyMbTHn8YEFiHO00MN qhypQ/MAcTusjDMq1s2CfdTM4v7KvpZVCpty+yltYNg0r0YxvvVitg== =21x0 Originator: austern@isolde.mti.sgi.com