Hi,

I am debugging with gdb a huge program on HP-UX. It has a very strange behaviour and I could not identify the cause. Have a look at this for example:
(gdb) c
Continuing.

Breakpoint 2, CExtract::extractDAT (this=0x80000001007bb9d8, user=@0x80000001004d4670) at Extract.cpp:1761
1761 meas.getSumMethodName ( sCol.meas_sum_method_name );
(gdb) p &meas
$9 = (class CMeasurement *) 0x800003ffbfb1f358
(gdb) s
CMeasurement::getSumMethodName (this=0x800003ffbfb20198, sum_method_name=@0x800003ffbfb204d0) at Measurement.cpp:1020
1020 sum_method_name = m_sum_method_name;
(gdb)

I'm printing the address of meas before calling meas.getSumMethodName. After stepping inside the method, gdb prints the value of "this". Shouldn't "this" inside CMeasurement::getSumMethodName have the same value as &meas?
If yes, any idea why it doesn't?

> Shouldn't "this" inside CMeasurement::getSumMethodName have the same value as &meas?
not necessarily. if getSumMethodName is a function inherited from a base class (in a multiple inheritance scenario) and the base class is at a non-zero offset, the this pointer in the function should point to the base class sub-object (and not &meas).

#include <iostream>

struct A
{
  void foo() { std::cout << "A* A::foo::this: " << this << '\n' ; } 
  char filler[1234] ;
};

struct B 
{
  void bar() { std::cout << "B* B::bar::this: " << this << '\n' ; } 
  char filler[1234] ;
};

struct C : A, B
{
};

int main()
{
  C c ;
  C* pc = &c ;
  B* pb = pc ;
  std::cout << "C* pc: " << pc << '\n' ;
  std::cout << "B* pb: " << pb << '\n' ;
  c.foo() ;
  c.bar() ;
}

@vijayan121: Thank you for the replay. You are right: a similar behaviour could happen in case of multiple inheritance.
Unfortunately this is not my case. meas is an instance of CMeasurement and class CMeasurement is not derived from anything,

Any other suggestions? I think there might be a problem with the way the code is compiled/linked but I could not find anything wrong.

> meas is an instance of CMeasurement and class CMeasurement is not derived from anything
in that case, it is either a horrible gdb/gcc version mismatch or (horror) the stack frame getting trashed.
by any chance, are you compiling without -g or with -fomit-frame-pointer ? and are you handling signals anywhere in your code?

Yes, I am compiling with -g and I am handling singles. I am using aCC compiler.

ok. and what output do you get if you add this line to CMeasurement::getSumMethodName

std::cout << "CMeasurement::getSumMethodName::this: " << this << '\n' ;

and print out the value of &meas before the call? are these the same values as what gdb reports?

I have added the couts. The program prints:
Before calling getSumMethodName &meas: 800003ff,bfb20198
CMeasurement::getSumMethodName::this: 800003ff,bfb20198

gdb prints wrong value of &meas:
(gdb) p &meas
$1 = (class CMeasurement *) 0x800003ffbfb1f358
(gdb) s
CMeasurement::getSumMethodName (this=0x800003ffbfb20198, sum_method_name=@0x800003ffbfb204d0) at Measurement.cpp:1018

So it is a gdb problem. It means I probably have to use log message instead...

Thanks a lot for your help!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.