This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
g++ weird behavoir with values returned by copy.
- From: Leandro Lucarella <llucar at mecon dot gov dot ar>
- To: gcc at gcc dot gnu dot org
- Date: Fri, 21 Nov 2003 20:03:09 -0300
- Subject: g++ weird behavoir with values returned by copy.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Hi, I was tring to figure out how g++ deals with return values by copy.
First I found that it doesn't return a copy, at least when I'm returning
a local variable created in my function (or method).
If I do something like this:
class A;
A test() {
A tmp;
return A;
}
Is tmp who is returned, not a copy. This works fine on most cases, but
I've found a case where the behavoir is much more weird (at least for
me).
and I found a very weird case, in wich with cause an object to be
destroyed when it's still in use using a reference:
#include <iostream>
using namespace std;
struct A {
A(void) {}
A(const A& a) {}
~A(void) {}
A& operator=(const A& a) {
return *this;
}
A& print(void) {
return *this;
}
};
A test(void) {
A tmp;
cout << "Doing something." << endl;
return tmp;
}
int main(void) {
A& ar = test().print();
// Now ar points to a destroyed objec
ar.print();
return 0;
}
Here's a gdb session resumen transcription:
Breakpoint 1, main () at test_copia_return_simple.cpp:24
24 A& ar = test().print();
test() () at test_copia_return_simple.cpp:18
18 A tmp;
A (this=0xbffff880) at test_copia_return_simple.cpp:6 <---- tmp points to 0xbffff880
6 A(void) {}
test() () at test_copia_return_simple.cpp:19
19 cout << "Doing something." << endl;
Doing something.
20 return tmp;
A::print() (this=0xbffff880) at test_copia_return_simple.cpp:13 <---- ar points to 0xbffff880
13 return *this;
~A (this=0xbffff880) at test_copia_return_simple.cpp:8 <---- tmp (0xbffff880) is destroyed
8 ~A(void) {}
main () at test_copia_return_simple.cpp:26
26 ar.print();
A::print() (this=0xbffff880) at test_copia_return_simple.cpp:13 <---- 0xbffff880 is used (ar.print())
13 return *this;
14 }
main () at test_copia_return_simple.cpp:27
27 return 0;
If I try to do A& ar = test(); I get a compile time error.
I compiled this tests with g++ (GCC) 3.3.2 (Debian) using -Wall and -g3 flags only.
I don't know if this behavoir is normal since I'm a relative newbie C++ programmer,
but I found it odd so I thought it could be some kind of bug. If it's not a bug, I'll
appreciate if someone can explain me why copy returned values are not really returned
by copy.
Thanks a lot, and please Cc me because I'm not subscribed to the list.
- --
Leandro Lucarella @ MEcon .-----------------------------.
/ Powered by Debian GNU/Linux |
.-----------------------------------------------------------------------------|
| GPG Key: http://bal748.mecon.ar/~luca/public_key.gpg |
| GPG Key Fingerprint: 716E 4896 93B2 640D 917D 96D8 4B16 3177 C864 D90B |
`-----------------------------------------------------------------------------'
El buen vino no merece probarlo, quien no sabe paladearlo.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.3 (GNU/Linux)
iD8DBQE/vpmt7T/vBXlJp7MRAvSNAKDYXeSVQYo6GtQoRHHYpxcRHJVeMQCgtYmH
9xlTsdwiVfo6vy9uLUBi0cE=
=Jl8e
-----END PGP SIGNATURE-----