#include <string> std::string foo() { std::string s; s = "hello"; s += "world"; return s; } std::string bar() { std::string s; s = "hello"; s += "world"; return "something else"; } int main() { int a = foo().size(); int b = bar().size(); return a + b; } /* Gcc seems to apply return value optimization (even at -O0) but there is some problem with either the generated debug information or the way gdb handles it. To reproduce it, save this file here as rvo.cpp and run g++ -g -O0 rvo.cpp -o rvo gdb -ex 'set confirm off' -ex 'file ./rvo' \ -ex 'b rvo.cpp:8' -ex 'b rvo.cpp:16' \ -ex 'run' -ex 'print s' \ -ex 'cont' -ex 'print s' \ -ex 'quit' The result will look like: Breakpoint 1, foo () at rvo.cpp:8 8 s += "world"; $1 = {static npos = <optimized out>, _M_dataplus = {<std::allocator<char>> = {<__gnu_cxx::new_allocator<char>> = {<No daa fields>}, <No data fields>}, _M_p = 0x1 <Address 0x1 out of bounds>}} Breakpoint 2, bar () at rvo.cpp:16 16 s += "world"; $2 = {static npos = <optimized out>, _M_dataplus = {<std::allocator<char>> = {<__gnu_cxx::new_allocator<char>> = {<No data fields>}, <No data fields>}, _M_p = 0x804b014 "hello"}} Program exited with code 030. RVO seems to be applied in the first case with "dangling" debug information. If no RVO is used (second case), all is fine. */
Created attachment 4882 [details] Test code and build instructions
Andre, Thanks for the nice test case. 's' seems to have a location in the debug info but it is either wrong or gdb is reading it wrong. I'll take a look at this when I get a chance. If anyone else is interested, in the mean while, go for it :) Sami
It turns out it is known on the gcc side: http://gcc.gnu.org/bugzilla/show_bug.cgi?id=44731
Possibly this gcc bug: http://gcc.gnu.org/bugzilla/show_bug.cgi?id=44731
*** Bug 12106 has been marked as a duplicate of this bug. ***
*** Bug 9640 has been marked as a duplicate of this bug. ***
*** Bug 9607 has been marked as a duplicate of this bug. ***
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=44367 Breakpoint 1, foo () at rvo.C:8 8 return s; $1 = "helloworld" Breakpoint 2, bar () at rvo.C:16 16 return "something else"; $2 = "helloworld" with /r: Breakpoint 1, foo () at rvo.C:8 8 return s; $1 = {static npos = 18446744073709551615, _M_dataplus = {<std::allocator<char>> = {<__gnu_cxx::new_allocator<char>> = {<No data fields>}, <No data fields>}, _M_p = 0x601058 "helloworld"}} Breakpoint 2, bar () at rvo.C:16 16 return "something else"; $2 = {static npos = 18446744073709551615, _M_dataplus = {<std::allocator<char>> = {<__gnu_cxx::new_allocator<char>> = {<No data fields>}, <No data fields>}, _M_p = 0x601028 "helloworld"}} This should be a GCC-only bug which is already fixed: FAIL g++ (GCC) 4.4.6 20110124 (prerelease) PASS g++ (GCC) 4.5.3 20110124 (prerelease) PASS g++ (GCC) 4.6.0 20110212 (experimental) PASS gcc-c++-4.6.0-0.6.fc15.x86_64 Behaves the same with both: GNU gdb (GDB) 7.2.50.20110213-cvs gdb-7.2.50.20110206-18.fc15.x86_64 Is the bug still reproducible with recent GCC?
Hi Jan, thanks for the information. Sorry I couldn't test it yet, there only is g++ 4.4.3 on my system. I suppose the confusing behaviour will have gone, the test code is simple enough. Regards Richard jan.kratochvil at redhat dot com schrieb: >http://sourceware.org/bugzilla/show_bug.cgi?id=11836 > >Jan Kratochvil <jan.kratochvil at redhat dot com> changed: > > What |Removed |Added >---------------------------------------------------------------------------- > Status|NEW |WAITING > CC| |jan.kratochvil at redhat > | |dot com > >--- Comment #8 from Jan Kratochvil <jan.kratochvil at redhat dot com> 2011-02-14 12:34:01 UTC --- >http://gcc.gnu.org/bugzilla/show_bug.cgi?id=44367 > >Breakpoint 1, foo () at rvo.C:8 >8 return s; >$1 = "helloworld" > >Breakpoint 2, bar () at rvo.C:16 >16 return "something else"; >$2 = "helloworld" > >with /r: > >Breakpoint 1, foo () at rvo.C:8 >8 return s; >$1 = {static npos = 18446744073709551615, _M_dataplus = {<std::allocator<char>> >= {<__gnu_cxx::new_allocator<char>> = {<No data fields>}, <No data fields>}, > _M_p = 0x601058 "helloworld"}} > >Breakpoint 2, bar () at rvo.C:16 >16 return "something else"; >$2 = {static npos = 18446744073709551615, _M_dataplus = {<std::allocator<char>> >= {<__gnu_cxx::new_allocator<char>> = {<No data fields>}, <No data fields>}, > _M_p = 0x601028 "helloworld"}} > >This should be a GCC-only bug which is already fixed: >FAIL g++ (GCC) 4.4.6 20110124 (prerelease) >PASS g++ (GCC) 4.5.3 20110124 (prerelease) >PASS g++ (GCC) 4.6.0 20110212 (experimental) >PASS gcc-c++-4.6.0-0.6.fc15.x86_64 > >Behaves the same with both: >GNU gdb (GDB) 7.2.50.20110213-cvs >gdb-7.2.50.20110206-18.fc15.x86_64 > >Is the bug still reproducible with recent GCC? > > >
So let's say it has never been a GDB bug.
*** Bug 260998 has been marked as a duplicate of this bug. *** Seen from the domain http://volichat.com Page where seen: http://volichat.com/adult-chat-rooms Marked for reference. Resolved as fixed @bugzilla.