473,508 Members | 4,179 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Gdb issues with virtual inheritance

Hi,
I encounted some problems using GDB with classes that use virtual
inheritance. To illustrate this issue, I have created a simple test
case.
Here it goes.


#include <iostream>
using namespace std;
///////////////////////////////////////////////////////
class ViBase
{
public:

ViBase(const char* s)
: m_s(s)
{
cout << "- ViBase constructor called: 0x" << hex << this
<< " s=" << s << dec << endl << endl;
};

const char* GetS() const
{
return m_s;
};

private:

const char* m_s;
};
///////////////////////////////////////////////////////
class Base
: virtual public ViBase
{
public:

Base(const int k)
: ViBase("bad"),
m_k(k)
{
cout << "- Base constructor called: 0x" << hex << this
<< dec << endl << endl;
};

const int GetK() const
{
return m_k;
};

private:

const int m_k;
};
///////////////////////////////////////////////////////
class Final
: public Base
{
public:

Final(const char *s,
const int k)
: Base(k),
ViBase(s)
{
cout << "- Final constructor called: 0x" << hex << this
<< dec << endl << endl;
};

void Print()
{
cout << endl << "Base : S = " << GetS() << endl
<< " K = " << GetK() << endl << endl;
};

private:

int m_a;
};
///////////////////////////////////////////////////////
int main(int argc, char* argv[])
{
Final *x = new Final("ObjectX", 2007);
if (x == NULL)
cout << endl << "Out of memory." << endl << endl;

x->Print();

cout << "GetS() = " << x->GetS() << endl;
cout << "GetK() = " << x->GetK() << endl;

delete x;
return 0;
}

************************************************** ************************************************** ************************
////There are 3 levels here. ViBase <--Base<--Final
At each level as the object is constructed I print the address of the
"this" pointer
I also have a simple function to access some member variable name.
If I run the program I have no problems at all. The problem arises
when I use gdb to step into the code.
Here's what I get. (I have provided a snapshot of the gdb session)

************************************************** ************************************************** ************************

gdb ./a.out
GNU gdb Red Hat Linux (5.2-2)
Copyright 2002 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 "i386-redhat-linux"...
(gdb) b main.cpp:88
Breakpoint 1 at 0x80489ec: file main.cpp, line 88.
(gdb) r
Starting program: /usr2/maheshs/vir_inherit/a.out
- ViBase constructor called: 0x0x804a4bc s=ObjectX

- Base constructor called: 0x0x804a4b0

- Final constructor called: 0x0x804a4b0
Breakpoint 1, main (argc=1, argv=0xbfffa934) at main.cpp:88
88 x->Print();
(gdb) s
Final::Print() (this=0x804a4b0) at main.cpp:71
71 cout << endl << "Base : S = " << GetS() << endl
(gdb) p *this
$1 = {<Base= {<ViBase= {m_s = 0x8048f20 "ObjectX"}, _vptr.Base =
0x8049014, m_k = 2007}, m_a = 0}
(gdb) n

Base : S = ObjectX
K = 2007

73 };
(gdb) p x
No symbol "x" in current context.
(gdb) l
68
69 void Print()
70 {
71 cout << endl << "Base : S = " << GetS() << endl
72 << " K = " << GetK() << endl <<
endl;
73 };
74
75 private:
76
77 int m_a;
(gdb) n
main (argc=1, argv=0xbfffa934) at main.cpp:90
90 cout << "GetS() = " << x->GetS() << endl;
(gdb) p x
$2 = (Final *) 0x804a4b0
(gdb) p x->GetS()
$3 = 0x8048f20 "ObjectX"
(gdb) p x->GetK()
Cannot access memory at address 0x0
(gdb)
As you can see above, the pointers printed by the cout statments do
not correspond to the this pointer which I print. Also in the last
line, I'm unable to call "x->GetK()"
Is this a GDB issue?? HAs anyone seen this in the past?
Is there a patch or some fix available?
Do let me know
Thanks,
Mahesh

May 1 '07 #1
2 2573
Mahesh wrote:
Is this a GDB issue?? HAs anyone seen this in the past?
Is there a patch or some fix available?
Do let me know
Almost certainly, and completely off topic for this group, try
gnu.something.

It looks like gdb doesn't know how to adjust the pointer for the
virtual base which isn't too surprising, it's a bit tricky.

May 1 '07 #2
On Apr 30, 7:20 pm, Mahesh <sridharanmah...@gmail.comwrote:
Hi,
I encounted some problems using GDB with classes that use virtual
inheritance. To illustrate this issue, I have created a simple test
case.
Here it goes.

#include <iostream>
using namespace std;

///////////////////////////////////////////////////////
class ViBase
{
public:

ViBase(const char* s)
: m_s(s)
{
cout << "- ViBase constructor called: 0x" << hex << this
<< " s=" << s << dec << endl << endl;
};

const char* GetS() const
{
return m_s;
};

private:

const char* m_s;

};

///////////////////////////////////////////////////////
class Base
: virtual public ViBase
{
public:

Base(const int k)
: ViBase("bad"),
m_k(k)
{
cout << "- Base constructor called: 0x" << hex << this
<< dec << endl << endl;
};

const int GetK() const
{
return m_k;
};

private:

const int m_k;

};

///////////////////////////////////////////////////////
class Final
: public Base
{
public:

Final(const char *s,
const int k)
: Base(k),
ViBase(s)
{
cout << "- Final constructor called: 0x" << hex << this
<< dec << endl << endl;
};

void Print()
{
cout << endl << "Base : S = " << GetS() << endl
<< " K = " << GetK() << endl << endl;
};

private:

int m_a;

};

///////////////////////////////////////////////////////
int main(int argc, char* argv[])
{
Final *x = new Final("ObjectX", 2007);
if (x == NULL)
cout << endl << "Out of memory." << endl << endl;

x->Print();

cout << "GetS() = " << x->GetS() << endl;
cout << "GetK() = " << x->GetK() << endl;

delete x;
return 0;

}

************************************************** ************************************************** ************************
////There are 3 levels here. ViBase <--Base<--Final
At each level as the object is constructed I print the address of the
"this" pointer
I also have a simple function to access some member variable name.
If I run the program I have no problems at all. The problem arises
when I use gdb to step into the code.
Here's what I get. (I have provided a snapshot of the gdb session)

************************************************** ************************************************** ************************

gdb ./a.out
GNU gdb Red Hat Linux (5.2-2)
Copyright 2002 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 "i386-redhat-linux"...
(gdb) b main.cpp:88
Breakpoint 1 at 0x80489ec: file main.cpp, line 88.
(gdb) r
Starting program: /usr2/maheshs/vir_inherit/a.out
- ViBase constructor called: 0x0x804a4bc s=ObjectX

- Base constructor called: 0x0x804a4b0

- Final constructor called: 0x0x804a4b0

Breakpoint 1, main (argc=1, argv=0xbfffa934) at main.cpp:88
88 x->Print();
(gdb) s
Final::Print() (this=0x804a4b0) at main.cpp:71
71 cout << endl << "Base : S = " << GetS() << endl
(gdb) p *this
$1 = {<Base= {<ViBase= {m_s = 0x8048f20 "ObjectX"}, _vptr.Base =
0x8049014, m_k = 2007}, m_a = 0}
(gdb) n

Base : S = ObjectX
K = 2007

73 };
(gdb) p x
No symbol "x" in current context.
(gdb) l
68
69 void Print()
70 {
71 cout << endl << "Base : S = " << GetS() << endl
72 << " K = " << GetK() << endl <<
endl;
73 };
74
75 private:
76
77 int m_a;
(gdb) n
main (argc=1, argv=0xbfffa934) at main.cpp:90
90 cout << "GetS() = " << x->GetS() << endl;
(gdb) p x
$2 = (Final *) 0x804a4b0
(gdb) p x->GetS()
$3 = 0x8048f20 "ObjectX"
(gdb) p x->GetK()
Cannot access memory at address 0x0
(gdb)

As you can see above, the pointers printed by the cout statments do
not correspond to the this pointer which I print. Also in the last
line, I'm unable to call "x->GetK()"

Is this a GDB issue?? HAs anyone seen this in the past?
Is there a patch or some fix available?
Do let me know
Thanks,
Mahesh
It looks like you are using an old version of gdb (5.2). Try using a
newer version (6.5 or 6.6). C++ support has improved quite a bit
since.
May 2 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
2187
by: nenad | last post by:
Wouldn't it be nice if we could do something like this: class Funky{ public: auto virtual void doStuff(){ // dostuff } };
4
2881
by: JKop | last post by:
I'm starting to think that whenever you derive one class from another, that you should use virtual inheritance *all* the time, unless you have an explicit reason not to. I'm even thinking that...
14
1893
by: Bruno van Dooren | last post by:
Hi all, i am having a problems with inheritance. consider the following: class A { public: A(int i){;} };
3
1698
by: Imre | last post by:
Hi! I've got some questions regarding heavy use of virtual inheritance. First, let's see a theoretical situation, where I might feel tempted to use a lot of virtual inheritance. Let's...
3
4532
by: kikazaru | last post by:
Is it possible to return covariant types for virtual methods inherited from a base class using virtual inheritance? I've constructed an example below, which has the following structure: Shape...
2
1827
by: Heinz Ketchup | last post by:
Hello, I'm looking to bounce ideas off of anyone, since mainly the idea of using Multiple Virtual Inheritance seems rather nutty. I chalk it up to my lack of C++ Experience. Here is my...
12
2638
by: mijobee | last post by:
I'm very new to c++ and just writing some code to learn. I've run into a problem, with a javaish design, and want to know if there is any possible solution without modifying the design. I've read...
23
4578
by: Dave Rahardja | last post by:
Since C++ is missing the "interface" concept present in Java, I've been using the following pattern to simulate its behavior: class Interface0 { public: virtual void fn0() = 0; };
2
2022
by: Alexander Adam | last post by:
Hi! I got a class structure similar to: class Base_Object { ... some functions ... } class Object: public Base_Object,
0
1294
by: =?Utf-8?B?Zmplcm9uaW1v?= | last post by:
Hi all, As I mentioned in a previous thread (see 'Dbghelp, symbols and templates' in microsoft.public.windbg), we created a powerful symbol engine using dbghelp to dump the contents of the stack...
0
7228
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7332
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
7058
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7502
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5057
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3206
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3191
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1565
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
426
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.