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<iostream>
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;
} 2 1777
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++
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(?fooE$4PPPPPPPMCAAEXXZ)--------------------------- | | |
| | | |
t11!E::`vftable' at 0041f178: | | | |
0041f178 004012f3 t11!ILT+750(?fooEUAEXXZ)--------------------------------- | | | <-
| | | |
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(?fooE$4PPPPPPPMCAAEXXZ): | <-
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(?fooEUAEXXZ) (004012f3)-- |
| |
t11!ILT+750(?fooEUAEXXZ): <- <-
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] This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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?
|
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;...
|
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...
|
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...
|
by: Adrian Herscu |
last post by:
Hi all,
In which circumstances it is appropriate to declare methods as non-virtual?
Thanx,
Adrian.
|
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
|
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...
|
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...
|
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...
|
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...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
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...
|
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...
|
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...
|
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,...
| |