This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: Determining type of an object at debug time.




On Tue, 14 Mar 2000, Martin v. Loewis wrote:

> > Finding the vpointer is easy, we always know where it is.
> 
> How exactly do you do that? There might not even be a vpointer in the
> struct... Consider
> 
> struct Base{
>   char buf[1000];
>   int i;
> };
> 
> struct Derived:virtual Base{
>   virtual ~Derived();
> };
> 
> Given a Base*, how exactly do you find out that the complete object is
> a Derived*? Or, worse, what if it is

You can't, in that case.

> 
> void foo(void*x){
>  ...
> }
> 
> foo(new Derived);
> 
> How do you know x is a Derived* inside foo?

Can't.

Then again, RTTI doesn't get it right either:
void foo(void *x)
{
        cout<<typeid(*x).name()<<endl;
        cout<<typeid(x).name()<<endl;
}

when you pass new Derived, it'll print
v
Pv

However, to be fair, if you also add a

Base *bob=(Derived *)x;
and print bob, it'll tell you it's a Base *.
If you try
print (Derived *)bob
It'll tell you it's a "suspicious *", and if you deref it, it'll print 
(gdb) p ((Derived *)bob)[0]
$8 = {
  <Base> = <invalid address>,
  members of Derived:
  _vb$Base = 0x0,
  _vptr$ = 0x0
}
(gdb)

However, if you try the most intuitive thing:
(gdb) p ((Derived *)x)[0]
$7 = (Derived) {
  <Base> = {
    buf =       '\000' <repeats 999 times>,
    i = 0
  },
  members of Derived:
  _vb$Base = 0x8051008,
  _vptr$ = 0x804f020
}
, it works still.
So while it's not perfect, it's almost the best we can do, given the info
we have.
Personally, i'm happy saying "If RTTI can't figure it outat runtime, we
can't figure it out at runtime", on the pathological cases.
But, i'll still make it work as best it can.


> 
> > I have it working.
> 
> Very good!
> 
Thanks,
Dan

> Martin
> 


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]