Source file (no preprocessing required): (note, I made it C-compatible so I could see if the bug is present in the C compiler as well, the answer being no, for reasons that will become obvious immediately) -------------------------------------------------------------------------------- struct st{ int n; }; struct st f(struct st x) { struct st z; z=x; z.n++; return z; } int main() { struct st c; c.n=1234; f(c); return 0; } -------------------------------------------------------------------------------- Terminal session: -------------------------------------------------------------------------------- brian@bbi5291-laptop:~/programs/Other$ g++ -v Using built-in specs. COLLECT_GCC=g++ COLLECT_LTO_WRAPPER=/usr/local/libexec/gcc/i686-pc-linux-gnu/4.5.0/lto-wrapper Target: i686-pc-linux-gnu Configured with: ./configure Thread model: posix gcc version 4.5.0 (GCC) brian@bbi5291-laptop:~/programs/Other$ g++ -g bug.c brian@bbi5291-laptop:~/programs/Other$ gdb a.out GNU gdb 6.8-debian Copyright (C) 2008 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html> This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law. Type "show copying" and "show warranty" for details. This GDB was configured as "i486-linux-gnu"... (gdb) break 8 Breakpoint 1 at 0x80483d2: file bug.c, line 8. (gdb) run Starting program: /home/brian/programs/Other/a.out Breakpoint 1, f (x={n = 1234}) at bug.c:8 8 z.n++; Current language: auto; currently c++ (gdb) print z.n $1 = 2424820 (gdb) q The program is running. Exit anyway? (y or n) y brian@bbi5291-laptop:~/programs/Other$ g++ -g -fno-elide-constructors bug.c brian@bbi5291-laptop:~/programs/Other$ gdb a.out GNU gdb 6.8-debian Copyright (C) 2008 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html> This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law. Type "show copying" and "show warranty" for details. This GDB was configured as "i486-linux-gnu"... (gdb) break 8 Breakpoint 1 at 0x80483e0: file bug.c, line 8. (gdb) run Starting program: /home/brian/programs/Other/a.out Breakpoint 1, f (x={n = 1234}) at bug.c:8 8 z.n++; Current language: auto; currently c++ (gdb) print z.n $1 = 1234 (gdb) q The program is running. Exit anyway? (y or n) y brian@bbi5291-laptop:~/programs/Other$ -------------------------------------------------------------------------------- It is evident to me from these test runs that a bug in the return value optimization is causing the wrong address for the variable z (since this is presumably elided altogether when the optimization is used) to be reported in the debug information. I first noticed this bug in g++ 4.4.1, and installed g++ 4.5.0 and found that it was there too; it does not occur in g++ 4.3.4 or earlier.
Works in 4.3.2 and on the trunk.
*** Bug 45973 has been marked as a duplicate of this bug. ***
Fixed in 4.6.0, the 4.5 branch is being closed.