From 3760268484257981994 X-Google-Thread: f78e5,1b57b95bf879a9da X-Google-Attributes: gidf78e5,public X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!news2.google.com!proxad.net!newsfeed.stueberl.de!newsfeed.vmunix.org!peer-uk.news.demon.net!kibo.news.demon.net!mutlu.news.demon.net!news.demon.co.uk!demon!stump.algebra.com!devnull From: clarkcox3@gmail.com ("Clark S. Cox III") Newsgroups: comp.std.c++ Subject: Re: auto_ptr as a return value Date: Wed, 24 Aug 2005 19:52:09 GMT Organization: Posted via Supernews, http://www.supernews.com Lines: 95 Sender: mail2news@demon.net Approved: fjh@cs.mu.oz.au (Fergus Henderson , moderator of comp.std.c++) Message-ID: <2005082414550416807%clarkcox3@gmailcom> References: <1124894045.583570.226570@g14g2000cwa.googlegroups.com> NNTP-Posting-Host: news.news.demon.net Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 8bit X-Trace: news.demon.co.uk 1124913136 29844 158.152.254.254 (24 Aug 2005 19:52:16 GMT) X-Complaints-To: abuse@demon.net NNTP-Posting-Date: Wed, 24 Aug 2005 19:52:16 +0000 (UTC) X-Robomod: STUMP, ichudov@algebra.com (Igor Chudov) X-User-Agent: Unison/1.6.3 X-Virus-Scanned: amavisd-new at cs.mu.OZ.AU X-Path: comp-std-cpp-robomod!not-for-mail X-Received: (from fjh@localhost) by mulga.cs.mu.OZ.AU (8.12.10+Sun/8.12.9/Submit) id j7OJq9xD005118; Thu, 25 Aug 2005 05:52:09 +1000 (EST) X-Delivered-To: std-c++@ucar.edu X-Authentication-Warning: mulga.cs.mu.OZ.AU: fjh set sender to devnull@stump.algebra.com using -f X-Newsgroups: comp.std.c++ Xref: g2news1.google.com comp.std.c++:1916 On 2005-08-24 06:41:12 -0400, andru123@hotmail.com said: > auto_ptr makeBla() > { > return NULL; > } First, auto_ptr's constructor is explicit, which means that NULL will not be implicitly converted in this case (I'm assuming that in your real code, you're returning an actual object. > //Assuming auto_ptr is OWNER of the objects, I MUST call release() > auto_ptr bla = makeBla().release(); You don't have to do that at all. The assignment operator and copy constructor of auto_ptr both transfer the ownership of the contained pointer: [littleclark2:~] clarkcox% cat test.cpp #include #include using namespace std; struct Foo { Foo() { std::cout << "Foo::Foo() called.\n"; } ~Foo() { std::cout << "Foo::~Foo() called.\n"; } }; auto_ptr makeBla() { return auto_ptr(new Foo); } int main() { auto_ptr a = makeBla(); Foo *raw = a.get(); assert(a.get() == raw); auto_ptr b = a; assert(a.get() == NULL); assert(b.get() == raw); auto_ptr c; c = b; assert(a.get() == NULL); assert(b.get() == NULL); assert(c.get() == raw); std::cout << "a, b and c are about to go out of scope.\n"; return 0; } [littleclark2:~] clarkcox% c++ test.cpp && ./a.out Foo::Foo() called. a, b and c are about to go out of scope. Foo::~Foo() called. [littleclark2:~] clarkcox% > > > As you can guess, here i get an exception. > > Can I do this then: > > auto_ptr bla = NULL; > if (makeBla() != NULL) > { > bla = makeBla(); > } > > My question: > will the result of the first call of makeBla() be deleted/ handled > correctly? Yes, it will be deleted when the unnamed, temporary auto_ptr is destructed, however, as I explained above, your problem is elsewhere, as auto_ptr is explicitly designed to handle this case. -- Clark S. Cox, III clarkcox3@gmail.com --- [ comp.std.c++ is moderated. To submit articles, try just posting with ] [ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ] [ --- Please see the FAQ before posting. --- ] [ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]