From: doug@foxtrot.ccmrc.ucsb.edu (Douglas Scott)
Subject: gdb 4.6 bug report
Organization: GNUs Not Usenet
Distribution: gnu
Date: Fri, 24 Jul 1992 21:35:21 GMT

System:  Sun 4/440 running OS 4.1
Compiler:  g++ 2.2.2
Gdb version: 4.6, compiled with g++ bug fix flag
Description of problem:
When tracing program execution of assignment operators
in the Complex class, I discovered that gdb incorrectly
determines the value of 'this' when descending into
assignment operator = (Complex &) in cases where the
class instance is globally or locally static. 


Comments start with "[NOTE: ".

tango> gdb a.out
GDB is free software and you are welcome to distribute copies of it
 under certain conditions; type "show copying" to see the conditions.
There is absolutely no warranty for GDB; type "show warranty" for details.
GDB 4.6, Copyright 1992 Free Software Foundation, Inc...
(gdb) break main
Breakpoint 1 at 0x22f0: file ggg.c, line 9.
(gdb) run
Starting program: /home/doug/C++/a.out 


Breakpoint 1, main () at ggg.c:9
9               static Complex sc0;     // should be automatically zeroed
Warning: the current language does not match this frame.
(gdb) n
10              Complex c0 = 0;
(gdb) 

11              Complex c1 = 1;
(gdb) 

12              Complex c2;             // this will not be initialized
(gdb) 

14              c2 = c0;                // test assignment to local object

[NOTE: printing values before descending into operator]

(gdb) print c0
$1 = {
  re = 0, 

  im = 0
}
(gdb) print c2
$2 = {
  re = -1.0560207522679516e+270, 

  im = 0
}
(gdb) s
Complex::operator= (this=0xf7fffb38, y=@0xf7fffb58) at ./Complex.h:131
131       re = y.real(); im = y.imag(); return *this; 


(gdb) fin
Run till exit from #0  Complex::operator= (this=0xf7fffb38, y=@0xf7fffb58)
    at ./Complex.h:131
main () at ggg.c:16
16   cout << "zero " << zero << " one " << one << " s_zero " << s_zero << "\n";
Value returned is $3 = (Complex &) @0xf7fffb38: {re = 0, im = 0}

[NOTE that here (local variable), &c2 == this from above operator, as it should]

(gdb) print &c2
$4 = (Complex *) 0xf7fffb38	

(gdb) n
zero (0, 0) one (1, 0) s_zero (0, 0)
17 cout << "sc0 " << sc0 << " c0 " <<  c0 << " c1 " << c1 << " c2 " << c2 << "\n";

(gdb) n
sc0 (0, 0) c0 (0, 0) c1 (1, 0) c2 (0, 0)
19              sc0 = c1;             // test assignment to local static object

(gdb) print sc0
$5 = {
  re = 0, 

  im = 0
}

(gdb) print c1
$6 = {
  re = 1, 

  im = 0
}

(gdb) print &sc0
$7 = (Complex *) 0x241e0

[NOTE that in this next operator call, this != &sc0]

(gdb) s			
Complex::operator= (this=0x26418, y=@0xf7fffb48) at ./Complex.h:131
131       re = y.real(); im = y.imag(); return *this; 


(gdb) n
132     } 


(gdb) n
main () at ggg.c:21
21              cout << "now sc0 = " << sc0 << "\n";

[NOTE: that gdb still thinks sc0 is (0, 0)]

(gdb) print sc0
$8 = {
  re = 0, 

  im = 0
}

[NOTE: but when printed, it is correctly set to (1, 0)]

(gdb) n
now sc0 = (1, 0)
23              s_zero = c1;         // test assignment to global static object

[NOTE: for global static, same thing:  &s_zero != this]

(gdb) print &s_zero
$9 = (Complex *) 0x241f8

(gdb) s
Complex::operator= (this=0x26430, y=@0xf7fffb48) at ./Complex.h:131
131       re = y.real(); im = y.imag(); return *this; 


(gdb) fin
Run till exit from #0  Complex::operator= (this=0x26430, y=@0xf7fffb48)
    at ./Complex.h:131
0x255c in main () at ggg.c:23
23              s_zero = c1;            // test assignment to global static object
Value returned is $10 = (Complex &) @0x26430: {re = 1, im = 0}

[NOTE:  again, gdb thinks it is still set to (0, 0)]

(gdb) print s_zero
$11 = {
  re = 0, 

  im = 0
}
(gdb) n
24      }
(gdb) cont
Continuing.

Program exited normally.

Here is the program.
----------------------------------------------------------------
#include <Complex.h>
#include <stream.h>

static Complex zero = 0;
static Complex one = 1;
static Complex s_zero;		// should be automatically zeroed

main() {
	static Complex sc0;	// should be automatically zeroed
	Complex c0 = 0;
	Complex c1 = 1;
	Complex c2;		// this will not be initialized

	c2 = c0;		// test assignment to local object

	cout << "zero " << zero << " one " << one << " s_zero " << s_zero << "\n";
	cout << "sc0 " << sc0 << " c0 " <<  c0 << " c1 " << c1 << " c2 " << c2 << "\n";

	sc0 = c1;		// test assignment to local static object

	cout << "now sc0 = " << sc0 << "\n";

	s_zero = c1;		// test assignment to global static object

}


Newsgroups: gnu.gdb.bug