C++ virtual inheritance memory layout

Hi, I am trying to figure out why _vptr$Grandparent & grandparent_data occupy the last memory in class child's memory layout although it is top in hiearchy? Still unable to understand why. Would appreciate anyone who can enlighten me on this.

Child's memory layout:
Size Value
8 bytes _vptr$Parent1
4 bytes parent1_data (+ 4 bytes padding)
8 bytes _vptr$Parent2
4 bytes parent2_data
4 bytes child_data
8 bytes _vptr$Grandparent
4 bytes grandparent_data (+ 4 bytes padding)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
class Grandparent {
 public:
  virtual void grandparent_foo() {}
  int grandparent_data;
};

class Parent1 : virtual public Grandparent {
 public:
  virtual void parent1_foo() {}
  int parent1_data;
};

class Parent2 : virtual public Grandparent {
 public:
  virtual void parent2_foo() {}
  int parent2_data;
};

class Child : public Parent1, public Parent2 {
 public:
  virtual void child_foo() {}
  int child_data;
};

int main() {
  Child child;
}
Last edited on
Why do you want to know (or care)?

It is (at best) a peculiarity of your particular compiler implementation.

Use a different compiler (or even just patch or upgrade your current compiler), and you could find something else entirely being done.

Sure, it's curiously interesting.
But you're not going to become a better C++ programmer from some deep understanding of why this is so for your particular machine.
Topic archived. No new replies allowed.