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
* [Bug c++/18272] weird behaviour on temporary return values
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 ` pinskia at gcc dot gnu dot org
2004-11-02 14:54 ` philippe dot haution at mines-paris dot org
` (2 subsequent siblings)
3 siblings, 0 replies; 5+ messages in thread
From: pinskia at gcc dot gnu dot org @ 2004-11-02 14:14 UTC (permalink / raw)
To: gcc-bugs
------- Additional Comments From pinskia at gcc dot gnu dot org 2004-11-02 14:14 -------
I think this is correct and here is why:
auto_ptr<vector<int> > res1 = ert(); // OK <-- we call the copy construtor so we increment the
refence
vector<int>& res2 = *res1; // OK <-- this is reference already
vector<int> *res3 = ert().get(); // NOK <-- we don't increment the reference so we will free it
right after this statement has been executated
vector<int>& res4 = *(ert()); // NOK if res5 not created afterwards
<---- same as above
vector<int>& res5 = *(ert().release());// OK
<--- should have caused an exeception as you are already freed it
temporary return values only last untill the statement is finished.
--
What |Removed |Added
----------------------------------------------------------------------------
Status|UNCONFIRMED |RESOLVED
Resolution| |INVALID
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=18272
^ permalink raw reply [flat|nested] 5+ messages in thread
* [Bug c++/18272] weird behaviour on temporary return values
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
3 siblings, 0 replies; 5+ messages in thread
From: philippe dot haution at mines-paris dot org @ 2004-11-02 14:54 UTC (permalink / raw)
To: gcc-bugs
------- Additional Comments From philippe dot haution at mines-paris dot org 2004-11-02 14:54 -------
You wrote about reference incrementing, but as far as auto_ptrs are concerned,
there's only ownership transfer of the underlying pointer.
res1 and res2 are OK, no problem about that.
But res5 = *(ert().release()); should and did not cause any exception because
this code is absolutely correct. Each call to the ert() function allocates a new
vector wrapped in a new temporary auto_ptr, so you can perform as many
ert().release() as you like and I am afraid I don't agree at all with your
comment about this line.
My initial concern was about :
vector<int> *res3 = ert().get();
vector<int>& res4 = *(ert());
After those two lines, it looks like res3 and res4 have lost the expected
result. If "temporary return values only last until the statement is finished",
then it's incorrect code and gcc's behaviour is OK. Then why do those 2
variables res3 and res4 hold the right result if I just add the instruction
about res5 ?
Try to execute the program with and without this instruction and see.
--
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=18272
^ permalink raw reply [flat|nested] 5+ messages in thread
* [Bug c++/18272] weird behaviour on temporary return values
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
3 siblings, 0 replies; 5+ messages in thread
From: pinskia at gcc dot gnu dot org @ 2004-11-02 14:57 UTC (permalink / raw)
To: gcc-bugs
------- Additional Comments From pinskia at gcc dot gnu dot org 2004-11-02 14:57 -------
Because it is just a flock that is all. the memory has been released but you can still access it.
--
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=18272
^ permalink raw reply [flat|nested] 5+ messages in thread
* [Bug c++/18272] weird behaviour on temporary return values
2004-11-02 12:38 [Bug c++/18272] New: weird behaviour on temporary return values philippe dot haution at mines-paris dot org
` (2 preceding siblings ...)
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
3 siblings, 0 replies; 5+ messages in thread
From: philippe dot haution at mines-paris dot org @ 2004-11-02 15:58 UTC (permalink / raw)
To: gcc-bugs
------- Additional Comments From philippe dot haution at mines-paris dot org 2004-11-02 15:58 -------
Well OK, I'll check with purify that res3 and res4 memory area is released
whether or not res5 has been intialized.
I will from now on use
vector<int>& res5 = *(ert().release());
as the correct way to get a reference to an object returned wrapped into an
auto_ptr.
Regards.
--
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).