Skip to content

Instantly share code, notes, and snippets.

@prehistoricpenguin
Last active July 18, 2017 02:41
Show Gist options
  • Save prehistoricpenguin/6579596 to your computer and use it in GitHub Desktop.
Save prehistoricpenguin/6579596 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <string>
#include <algorithm>
#include <iterator>
#include <cctype>
#include <fstream>
#include <set>
using namespace std;
class Furniture {
public:
Furniture () { _price = 0;}
virtual ~Furniture() { cout << "Furiture dtor" << endl; }
virtual int Price() { return _price; }
protected:
int _price;
};
class Sofa: virtual public Furniture {
public:
Sofa():_color(2) { _price = 1; }
virtual ~Sofa() { cout << "sofa dtor" << endl; }
virtual int Color() { return _color; }
virtual void SitDown() { cout << "Sofa sitdown" << endl; }
protected:
int _color;
};
class Bed: virtual public Furniture {
public:
Bed():_length(4), _width(5) { _price = 3; }
virtual ~Bed() { cout << "Bed dtor" << endl; }
virtual int Area() { return _length * _width; }
virtual void Slee() { cout << "Bed sleep" << endl; }
protected:
int _length;
int _width;
};
class SofaBed : public Sofa, public Bed {
public:
SofaBed():_height(6) { }
virtual ~SofaBed() { cout << "SofaBed dtor" << endl; }
virtual void SitDown() { cout << "SofaBed sitdown" << endl; }
virtual void Sleep() { cout << "SofaBed sleep" << endl; }
virtual int Height() { return _height; }
protected:
int _height;
};
int main() {
SofaBed sbed;
cout << sizeof(Furniture) << endl;
cout << sizeof(Sofa) << endl;
cout << sizeof(Bed) << endl;
cout << sizeof(sbed) << endl;
return 0;
}
/*
output from vs2012:
8
20
24
40
SofaBed dtor
Bed dtor
sofa dtor
Furiture dtor
Press any key to continue . . .
output from Gcc:
8
16
20
32
SofaBed dtor
Bed dtor
sofa dtor
Furiture dtor
Process returned 0 (0x0) execution time : 0.180 s
Press any key to continue.
*/
@prehistoricpenguin
Copy link
Author

Without virtual functions

Same memory layout in vc++ and g++, just POD types

With virtual functions in grandparent

vc++

vptr
offset-ptr
Sofa::_color
vptr
offset-ptr
Bed::_length
Bed::_width
SofaBed ::height
Furniture ::_price

offset-ptr point to a table like this:
distance between vptr and offset-ptr
distance between vptr ans parent object

g++

vptr
Sofa::_color
vptr
Bed::_length
Bed::_width
SofaBed ::height
Furniture ::_price

In the layout of g++, addition information will be find just before the v-table, include the distance between vptr and parent type object.

With virtual functions in parent class or self

to be continued

@theidexisted
Copy link

theidexisted commented Jul 18, 2017

GCC version : 5.4 x86_64
sbed object size: 56

ABI of original code:

$3 = {
  <Sofa> = {
    <Furniture> = {
      _vptr.Furniture = 0x4012d8 <vtable for SofaBed+160>, 
      _price = 3
    }, 
    members of Sofa: 
    _vptr.Sofa = 0x401250 <vtable for SofaBed+24>, 
    _color = 2
  }, 
  <Bed> = {
    members of Bed: 
    _vptr.Bed = 0x401298 <vtable for SofaBed+96>, 
    _length = 4, 
    _width = 5
  }, 
  members of SofaBed: 
  _height = 6
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment