bug-gdb
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Incorrect printing of C++ objects via casts


From: Stephen Tse
Subject: Re: Incorrect printing of C++ objects via casts
Date: Thu, 17 May 2001 20:35:04 GMT
User-agent: Gnus/5.0807 (Gnus v5.8.7) XEmacs/21.2 (Terspichore)

Hi Anthony, I came across your posting in google/deja. I have exactly
the same problem. Your solution using intermediate cast to "void *"
works great! (I can't believe you found it!) But have you found out a
better/official way to access derived class?

I have searched the gnu.gdb.bug group for that in vain.

Thanks!


> Hi,
>     (I'm a little concerned having searched the archives that nobody
> has mentioned this - perhaps it's a total misunderstanding on my part.
> If so apologies...)
> 
>     The behaviour of gdb looks incorrect when casting a base class
> pointer back to a derived class.   The example below is trying to
> print a "Big" object via a cast of a "Small" pointer.  Each time,
> different junk gets printed. An intermediate cast to "void *" seems
> to avoid the problem.
> 
>     This is gdb 4.18, compiled with gcc 2.95.2(pre) as shown below.
> Also see same behaviour in gdb 19990928 (current debian potato).
> 
> 
> 
> Regards
> 
> Anthony
> 
> 
> ~% cat tst.C
> #include <string>
> 
> struct Small {
>     int i;
> };
> 
> struct Big : Small {
>     string s;
> };
> 
> int main() {
>     Big *foo = new Big;
>     Small *bar = foo;
>     return(0);
> }
> 
> ~% c++ -g -v tst.C
> Reading specs from /usr/lib/gcc-lib/i386-linux/2.95.2/specs
> gcc version 2.95.2 19991019 (prerelease)
> GNU CPP version 2.95.2 19991019 (prerelease) (i386 Linux/ELF)
> GNU C++ version 2.95.2 19991019 (prerelease) (i386-linux) compiled by
> GNU C version 2.95.2
> 19991019 (prerelease).
> GNU assembler version 2.9.5 (i386-linux) using BFD version 2.9.5.0.16
> 
> ~% gdb a.out 
> GNU gdb 4.18
> Copyright 1998 Free Software Foundation, Inc.
> GDB is free software, covered by the GNU General Public License, and you
> are
> welcome to change it and/or 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.
> This GDB was configured as "i686-pc-linux-gnu"...
> (gdb) break main
> Breakpoint 1 at 0x80495d7: file tst.C, line 12.
> (gdb) run
> Starting program: /export/home/ajrh/a.out 
> 
> Breakpoint 1, main () at tst.C:12
> 12          Big *foo = new Big;
> (gdb) n
> 13          Small *bar = foo;
> (gdb) n
> 14          return(0);
> (gdb) print *foo
> $1 = {<Small> = {i = 0}, s = {static npos = 4294967295,
>     static nilRep = {len = 0, res = 0, ref = 2, selfish = false}, 
>     dat = 0x804b838 ""}}
> 
> 
> 
> (gdb) print *(Big*)bar
> $2 = {<Small> = {i = 0}, s = {static npos = 4294967295,
>     static nilRep = {len = 0, res = 0, ref = 2, selfish = false}, 
>     dat = 0x208 <Address 0x208 out of bounds>}}
> (gdb) 
> $3 = {<Small> = {i = 0}, s = {static npos = 4294967295,
>     static nilRep = {len = 0, res = 0, ref = 2, selfish = false}, 
>     dat = 0x400034fc "U\211å\203ì\024Sè"}}
> (gdb) 
> $4 = {<Small> = {i = 0}, s = {static npos = 4294967295,
>     static nilRep = {len = 0, res = 0, ref = 2, selfish = false},
>     dat = 0x0}}
> (gdb) 
> $5 = {<Small> = {i = 0}, s = {static npos = 4294967295,
>     static nilRep = {len = 0, res = 0, ref = 2, selfish = false}, 
>     dat = 0x83512d0 <Address 0x83512d0 out of bounds>}}
> 
> 
> 
> (gdb) print *(Big*)(void*)bar
> $6 = {<Small> = {i = 0}, s = {static npos = 4294967295,
>     static nilRep = {len = 0, res = 0, ref = 2, selfish = false}, 
>     dat = 0x804b838 ""}}
> 


reply via email to

[Prev in Thread] Current Thread [Next in Thread]