473,772 Members | 2,552 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 2590
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.comwrot e:
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
2225
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
2901
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 there shouldn't have been a "virtual" keyword for this purpose, but instead, a "nonvirtual" keyword! In teaching inheritance, you see the common example: class Vehicle {}
14
1926
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
1715
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 suppose, we're creating a little strategy game. In our game, there are Units. A Unit can be either a Human, or a Vehicle. Obviously, Human and Vehicle are subclasses of Unit.
3
4551
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 = base class Triangle, Square = classes derived from Shape Prism = class derived from Shape TriangularPrism, SquarePrism = classes derived from Triangle and Prism, or Square and Prism respectively
2
1869
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 scenario... I have 5 Derived Classes I have 3 Base Classes
12
2663
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 up on virtual inheritance and from my understanding this should work fine but I haven't found any docs that use such a tangled example. The gcc output containing the errrors: Example.cpp: In member function 'virtual IObject*...
23
4615
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
2039
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
1318
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 symbols when an exception occurs. The engine is able to dereference and process UDT symbols up to their highest base class. It also supports multiple inheritance. However, we are having trouble with virtual inheritance. Documentation is very...
0
9454
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10106
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10039
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8937
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7461
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6716
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4009
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 we have to send another system
2
3610
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2851
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.