public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/18272] New: weird behaviour on temporary return values
@ 2004-11-02 12:38 philippe dot haution at mines-paris dot org
  2004-11-02 14:14 ` [Bug c++/18272] " pinskia at gcc dot gnu dot org
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: philippe dot haution at mines-paris dot org @ 2004-11-02 12:38 UTC (permalink / raw)
  To: gcc-bugs

If a function returns an auto_ptr, it is impossible to dereference the return
value on the fly and then use it, the temporary object seems to be destroyed
on the next line.

However, if there is a subsequent call to this function that releases another
return value, it works.

#include <iostream>
#include <vector>

using std::cout;
using std::cerr;
using std::endl;
using std::vector;
using std::exception;
using std::auto_ptr;

// function creating auto_ptrs
auto_ptr<vector<int> >
ert()
{
  auto_ptr<vector<int> > res(new vector<int>);

  res->assign(7, 20);
  return res;
}

int main(int argc, char ** argv)
{
  try {
    auto_ptr<vector<int> > res1 = ert(); // OK
    vector<int>& res2 = *res1;           // OK
    vector<int> *res3 = ert().get();     // NOK
    vector<int>& res4 = *(ert());        // NOK if res5 not created afterwards
    vector<int>& res5 = *(ert().release());// OK
    
    cout << Size " << res1->size()
         << " | " << res2.size()
         << " | " << res3->size()
         << " | " << res4.size()
         << endl;
  }
  catch(exception& exc) {
    cerr << exc.what() << endl;
    return -3;
  }
  catch(...) {
    cerr << "unexpected exception" << endl;
    return -5;
  }

  return 0;
}

-- 
           Summary: weird behaviour on temporary return values
           Product: gcc
           Version: 3.4.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P2
         Component: c++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: philippe dot haution at mines-paris dot org
                CC: gcc-bugs at gcc dot gnu dot org


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=18272


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2004-11-02 15:58 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-11-02 12:38 [Bug c++/18272] New: weird behaviour on temporary return values philippe dot haution at mines-paris dot org
2004-11-02 14:14 ` [Bug c++/18272] " pinskia at gcc dot gnu dot org
2004-11-02 14:54 ` philippe dot haution at mines-paris dot org
2004-11-02 14:57 ` pinskia at gcc dot gnu dot org
2004-11-02 15:58 ` philippe dot haution at mines-paris dot org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).