Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / All-Topics

Compiler Optimization Techniques - Virtual Inheritance - Part 1

5.00/5 (1 vote)
18 Apr 2012CPOL3 min read 10.7K  
This article discusses the primitive inheritance technique, i.e., single level inheritance, object layout, memory allocation, implicit class pointer conversion, and pointer offsetting.

Inheritance is one of the pillars of Object Oriented Programming. Many high level languages have different implementations for inheritance and also they support wider ramification of inheritance. Despite these differences, the core implementation structure of inheritance converges to an accepted design which is common across all languages. The advantage of diving deeper into understanding the implementation details of inheritance is that it imparts greater confidence in you to be able to exactly figure out what compiler is trying to achieve.

The reason behind writing this article is, out of all the high level programming languages, C++ stands out by supporting multiple inheritance, which none of the other high level languages support in its native form, but rather as interfaces. As a programmer, it is always good to understand your compiler and its optimization techniques which ultimately helps you to write efficient code.

The main article will be presented as two separate posts, and this being Part 1 of the article, discusses primitive inheritance technique, i.e., single level inheritance, object layout, memory allocation, implicit class pointer conversion, and pointer offsetting. Part 2 of the article provides comprehensive insight into implementation details of multiple and virtual inheritance.

Part 1

Consider two simple classes, "Mobile_OS" and "WindowsPhone" which is derived from "Mobile_OS" with a public access specifier.

C++
class Mobile_OS  
{  
 public:  
   float iKernalVersion;  
   float iReleaseVersion;  
   char* strVendor ;  
   
   virtual float GetKernalVersion();  
   virtual float GetReleaseVerison();  
   Mobile_OS();  
   ~ Mobile_OS();  
};  
   
class WindowsPhone : public Mobile_OS  
{  
 private:  
   char* strCodeName ;  
   char* iHardwarePlatform ;    
   
 public:  
   int iCustomRomVersion ;  
   float GetKernalVersion();  
   float GetReleaseVerison();  
   WindowsPhone();  
   ~ WindowsPhone();  
};

The blue print of "WindowsPhone" class lays out all the non virtual data members in the order of their declaration starting from the base class data members, followed by derived class data members as shown in the diagram below:

Image 1

Why This Object Layout is Preferred for Derived Class?

The C++ Standards Committee allows object layout of any ordering of data members each separated by access declarator. But VC++ and most of the other compilers ensure that the objects are laid out in the order of declarations, with derived class data members following the base class data members.

What Optimization Does Compiler Achieve with this Layout?

Derived class inherits all the public properties and behaviors of base class. The complete instance of base class's data members are contained within the derived class address space.

By placing the base class "Mobile_OS" at the starting address of a derived class, "WindowsPhone" ensures that the address of the base object "Mobile_OS" within derived object "WindowsPhone" corresponds to very first byte of "WindowsPhone". And hence this layout avoids offset calculations for base object data access within derived object.

How Offset Calculation Is Avoided?

C++
Mobile_OS* Lumia = new WindowsPhone();  
Lumia->iKernalVersion;

Consider the code snippet above, here base class data member "iKernalVersion" is accessed from "Lumia" object pointer whose static type is "Mobile_OS*" but dynamic type is "WindowsPhone*".

Since the implicit upcasting form "WindowPhone" to "Mobile_OS" is successful in this case, compiler just extracts value of "iKernalVersion" based on "Mobile_OS" layout, and hence no offset calculations are required in this case. So to get to the base class data member compiler just needs to compute:

C++
&DataValue = DerivedClass_StartingAddress + Offset_of_DataMember_Within_BaseClass 

What If Base Class Data Members Followed Derived Class Data Members?

Consider the same code snippet specified above, and if at all base class data members followed derived class data members, accessing any data member of the base class would have been an overload with offset computation. So to get to the base class data member, compiler needs to compute:

C++
&DataValue = DerivedClass_StartingAddress + Offset_To_BaseClassObject_Within_DerivedClass  
+Offset_of_DataMember_Within_BaseClass

I hope this provides convincing explanation about object layout and its advantages in case of single inheritance. And as mentioned earlier, Multiple and Virtual Inheritance implementations will be discussed in Part 2 of the article. Kindly revert back if you have any comments or suggestions.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)