C++ Virtual Memory Layout

137 views Asked by At

Understanding Virtual Inheritance Memory Layouts

I am working towards a better understanding in resolved memory layouts that vTables and vPointers give us. I fully understand polymorphism and why it works and have really no trouble with it however there are a couple small things I seem to be tripping up on regarding memory layouts and understanding virtual base offsets. I know this stuff is not totally necessary in every day C++ code however it is an interest of mine, to get what is going on.

I have been referring to this site for some help understanding the memory layout that occurs as a result of using virtual inheritance.

In the Virtual Inheritance section of the page about half way down it offers visuals for the vTables generated, however I want to clarify the order in which I think several events take place:


(source: phpcompiler.org)

In the picture above, it seems to me that:

  • We first include the vPtr that points to the Left vTable
  • Next include any values unique to the Left class first (Left::b)
  • Next, offsets are calculated so we can put any virtually inherited data directly after the data we just included. Size of vPtr + int Left::b = 8, so we offset Top::a by 8, and add that to the bottom of our layout.
  • Rinse and repeat fort the Right class


(source: phpcompiler.org)

In this picture above, it seems to me that:

  • We first include vPtr of first class we inherit from
  • Since we are not virtually inheriting from that immediate base class, we can directly inherit any non-virtual data members specific to the class, without offsets i.e. (Left::b)
  • We include vPtr of second class we inherit from
  • Since we are not virtually inheriting from that immediate base class, we can directly inherit any non-virtual data members specific to the class, without offsets i.e. (Right::c)
  • Next include any unique data to the Bottom class, Bottom::d
  • Next we calculate offsets for the virtual data in our superclass's and include this at the bottom of our memory layout.
  • Finally we are done with our memory layout for the Bottom class

To me, I think the above processes make sense in determining the memory layout in many cases, however I am wondering what happens if I change a few things.

  1. How would the memory layout change if the Bottom virtually inherited from both Left and Right
  2. Bottom inherits Left and Right regularly, but say Right defines its own int a, obviously this would make the bottom.a equal to whatever the bottom.Right::a is since its the most recently overrided, however where would this be placed in the memory layout?

I have a proposal layout for each of my questions (2) and would like to know if my thoughts behind them are correct, and if not, why...Thanks!

1.)

+--Bottom---+
| vPtrLeft  |
| vPtrRight |
| Bottom::d |
|  Left::b--+---From offset calculated by vPtr.Left in Bottom vTable
|  Right::b-+---From offset calculated by vPtr.Right in Bottom vTable
|  Top::a --+---From offsets calculated from vPtr.Left and vPtr.Right
+-----------+
  • Include vPtr.Left, then look for any unique data to Left not inherited virtually (None) (i.e. do not require offsets)
  • Include vPtr.Right, then look for any unique data to Right not inherited virtually (None) (i.e. do not require offsets)
  • Add any data specific to this class, (Bottom::d)
  • Acquire all virtual data requiring an offset from Left, then Right climbing up the chain of inheritance until we finish Left and Right's virtually inherited Top::a attribute

2.)

+---Bottom---+
|  vPtrLeft  |
|  Left::b---+---inheriting normally from Left
|  vPtrRight +
|  Right::c--+---inheriting normally from Right
|  Right::a--+---inheriting normally from Right
|  Bottom::d |   also most recently overrided value for A
|  Top::a----+---From offsets calculated from vPtr.Left and vPtr.Right
+------------+

Looking for my feedback on the logic behind these layouts, thank you.

Edit:

I know this was marked as a duplicate but I believe that this question has more specifics involved with it than the other, and I am looking for some answers to variations on regular multiple virtual inheritance to understand Object Memory Layouts with Virtual Inheritance

0

There are 0 answers