473,778 Members | 1,958 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

virtual-multiple inheritance. what an extra dword is for?

Hello All,

I've fount that if I compile the same program using gcc and vc 2003 the same class E (see complete source bellow) has different size (on vc it is 4 bytes bigger). Digging into this I've found that vc is reserving a dword in the E class right before storage for virtual base class. In my tests value of this dword always was 0. I did not see any code referencing this memory.

Anybody has any idea what this dword is for?

Vladimir.
/*

Compile:
cl -EHsc -Zi t11.cpp

Classes hierarchy
A
/ \
| |
| |
B D C
| | |
| | |
\ | /
E

Output: My Interpritation:

ma=ff1, sizeof(A)=8 sizeof(vtable) + sizeof(ma_)
mb=ff2, sizeof(B)=20 sizeof(vtable) + sizeof(mb_) + 1*sizeof(DWORD) + sizeof(A)
mc=ff3, sizeof(C)=20 sizeof(vtable) + sizeof(mb_) + 1*sizeof(DWORD) + sizeof(A)
md=ff4, sizeof(D)=8 sizeof(vtable) + sizeof(md_)
me=ff5, sizeof(E)=40
&before - &after = 13

Layout of the stack in the "main" function:

Nr Address Value Points to My Interpritation
01 0012feb0 00000fe2 << veriable "after"

02 0012feb4 0041f178 t11!E::`vftable ' << veriable e, start of E, start of E::D
03 0012feb8 00000ff4 << e.md_
04 0012febc 0041f188 t11!E::`vbtable ' << start of E::B
05 0012fec0 00000ff2 << e.mb_
06 0012fec4 0041f17c t11!E::`vbtable ' << start of E::C
07 0012fec8 00000ff3 << e.mc_
08 0012fecc 00000ff5 << e.me_
09 0012fed0 00000000 << ???WHAT ARE THIS FOR???
10 0012fed4 0041f174 t11!E::`vftable ' << start of E::A
11 0012fed8 00000ff1 << e.ma_, end of E
12 0012fedc 0000000c << veriable "distance"

13 0012fee0 00000fe1 << veriable "before"

*/

#include<iostre am>

using namespace std;

class A {
public:
explicit A(long ma)
: ma_(ma) {
}
virtual void foo();
private:
long ma_;
};

void A::foo() {
cout << hex << "ma=" << ma_ << ", sizeof(A)=" << dec << sizeof(A) << "\n";
}

class B : public virtual A {
public:
explicit B(long mb)
: mb_(mb), A(0) {
}
virtual void foo();
private:
long mb_;
};

void B::foo() {
cout << hex << "mb=" << mb_ << ", sizeof(B)=" << dec << sizeof(B) << "\n";
}

class C : public virtual A {
public:
explicit C(long mc)
: mc_(mc), A(0) {
}
virtual void foo();
private:
long mc_;
};

void C::foo() {
cout << hex << "mc=" << mc_ << ", sizeof(C)=" << dec << sizeof(C) << "\n";
}

class D {
public:
explicit D(long md)
: md_(md) {
}
virtual void foo();
private:
long md_;
};

void D::foo() {
cout << hex << "md=" << md_ << ", sizeof(D)=" << dec << sizeof(D) << "\n";
}

class E : public B, public C, public D {
public:
explicit E(long ma=0xFF1, long mb=0xFF2, long mc=0xFF3, long md=0xFF4, long me=0xFF5)
: A(ma), B(mb), C(mc), D(md), me_(me) {
}
virtual void foo();
private:
long me_;
};

void E::foo() {
A::foo();
B::foo();
C::foo();
D::foo();
cout << hex << "me=" << me_ << ", sizeof(E)=" << dec << sizeof(E) << "\n";
}

int main(int, char **) {
cout << hex;

int distance = 0;
int before = 0xFE1;

E e;
if(!distance) { //to prevent compiler from rearanging veriables on the stack
int after = 0xFE2;
distance = &before - &after;
}
e.foo();

cout << dec << "&before - &after = " << abs(distance) << "\n";

return 0;
}
Nov 17 '05 #1
2 1793
Vladimir_petter wrote:
Hello All,

I've fount that if I compile the same program using gcc and vc 2003 the same class E (see complete source bellow) has different size (on vc it is 4 bytes bigger). Digging into this I've found that vc is reserving a dword in the E class right before storage for virtual base class. In my tests value of this dword always was 0. I did not see any code referencing this memory.

Anybody has any idea what this dword is for?


See if this article helps you figure it out:

C++: Under the Hood
Jan Gray
http://msdn.microsoft.com/archive/de...angrayhood.asp

--
Doug Harrison
Microsoft MVP - Visual C++
Nov 17 '05 #2

Hello Doug,

Thanks for the link. It definetly helped.
I am still not 100% sure, but my guess is that this DWORD is a reservaton for a future vbtable to store deplacement of A relative to E.
See bellow corrected picture.

Thanks,
Vladimir.


Layout of the stack in the "main" function:

Nr Address Value Points to My Interpritation
01 0012feb0 00000fe2 << veriable "after"

02 0012feb4 0041f178 t11!E::`vftable ' << veriable e, start of E, start of E::D ---------------
03 0012feb8 00000ff4 << e.md_ |
04 0012febc 0041f188 t11!E::`vbtable ' << deplacement info for A relative to B------------- |
05 0012fec0 00000ff2 << e.mb_, start of E::b | |
06 0012fec4 0041f17c t11!E::`vbtable ' << deplacement info for A relative to C-------- | |
07 0012fec8 00000ff3 << e.mc_ | | |
08 0012fecc 00000ff5 << e.me_ | | |
09 0012fed0 00000000 << ?is this a reservation for future vbtable? | | |
10 0012fed4 0041f174 t11!E::`vftable ' << start of E::A---------------------------- | | |
11 0012fed8 00000ff1 << e.ma_, end of E | | | |
| | | |
12 0012fedc 0000000c << veriable "distance" | | | |
13 0012fee0 00000fe1 << veriable "before" | | | |
| | | |
| | | |
t11!E::`vftable ' at 0041f174 <- | | |
0041f174 0040112c t11!ILT+295(?fo oE$4PPPPPPPMCAA EXXZ)--------------------------- | | |
| | | |
t11!E::`vftable ' at 0041f178: | | | |
0041f178 004012f3 t11!ILT+750(?fo oEUAEXXZ)--------------------------------- | | | <-
| | | |
t11!E::`vbtable ' at 0041f17c | | <- |
0041f17c 00000000 | | |
0041f180 00000010 << decimal 16, 4 dwords from C to A in E | | |
0041f184 00000000 | | |
| | |
t11!E::`vbtable ' at 0041f188 | | <-
0041f188 00000000 | |
0041f18c 00000018 << decimal 24, 6 dwords from B to A in E | |
0041f190 00000000 | |
| |
t11!ILT+295(?fo oE$4PPPPPPPMCAA EXXZ): | <-
0040112c e98f540000 jmp t11!E::foo (004065c0)-- |
| |
t11!E::foo: <- |
004065c0 2b49fc sub ecx,[ecx-0x4] |
004065c3 83e920 sub ecx,0x20 |
004065c6 e928adffff jmp t11!ILT+750(?fo oEUAEXXZ) (004012f3)-- |
| |
t11!ILT+750(?fo oEUAEXXZ): <- <-
004012f3 e9e8040000 jmp t11!E::foo (004017e0)--
|
t11!E::foo [t11.cpp @ 117]: <-
004017e0 55 push ebp
004017e1 8bec mov ebp,esp
004017e3 51 push ecx
004017e4 894dfc mov [ebp-0x4],ecx
004017e7 8b45fc mov eax,[ebp-0x4]
Nov 17 '05 #3

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

Similar topics

12
1802
by: c++novice | last post by:
1--Can operators be virtual? 2--What is the difference between an operator= returning a refernce Vs a value?
5
4090
by: alex goldman | last post by:
When compiling this code with g++ -Wall -pedantic -ansi -c program.cpp # this is GCC-3.3.4 ###################################### #include <vector> class b { public: virtual void f() = 0; };
26
3986
by: pmizzi | last post by:
When i compile my program with the -ansi -Wall -pedantic flags, i get this warning: `class vechile' has virtual functions but non-virtual destructor, and the same with my sub-classes. But when i add a virtual destructor like this : " virtual ~vechile". I get this error: Undefined first referenced symbol in file vtable for vechile /var/tmp//ccC9yD6Z.o
12
2419
by: Daniel Kay | last post by:
Hi Folks! Everytime I work with virtual and pure virtual methods I ask myself if there is a difference between class B and class C (see below). Is it redundant to repeat the virtual keyword in a derived class? Or is there any difference I need to consider? I have seen both ways when I read source code from external sources. So far I didn't find any answers in my books or on the net. class A {
32
4527
by: Adrian Herscu | last post by:
Hi all, In which circumstances it is appropriate to declare methods as non-virtual? Thanx, Adrian.
16
3759
by: plmanikandan | last post by:
Hi, I have doubts reg virtual constructor what is virtual constructor? Is c++ supports virtual constructor? Can anybody explain me about virtual constructor? Regards, Mani
11
3454
by: ypjofficial | last post by:
Hello All, So far I have been reading that in case of a polymorphic class ( having at least one virtual function in it), the virtual function call get resolved at run time and during that the vtable pointer is made use of.. eg. class one {
11
3437
by: Nindi73 | last post by:
A few days a ago I posted my code for a deep copy pointer which doesn't require the pointee object to have a virtual copy constructor. I need help with checking that it was exception safe and exception neutral/ I got a reply from Bux with his code for a smart pointer with far fewer lines of code and more cleaner design, not over engineered like mine. ...
5
11360
by: druberego | last post by:
I read google and tried to find the solution myself. YES I do know that you can get undefined references if you: a) forget to implement the code for a prototype/header file item, or b) you forget to pass all the necessary object files to the linker. Neither of those are my problem. Please bear with me as the question I ask is rather long and I think it's beyond a CS101 level of linker stupidity. If it is a stupid CS101 mistake I'm making...
7
3091
by: eric | last post by:
hello i'm confused by an example in the book "Effective C++ Third Edition" and would be grateful for some help. here's the code: class Person { public: Person(); virtual ~Person(); // see item 7 for why this is virtual ...
0
9470
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
10298
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10127
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
10069
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
8957
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
7475
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
6723
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();...
0
5370
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
3
2865
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.