Click here to Skip to main content
13,090,299 members (76,349 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


7 bookmarked
Posted 18 Apr 2012

Compiler Optimization Techniques - Virtual Inheritance - Part 1

, 18 Apr 2012
Rate this:
Please Sign up or sign in to vote.
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.

class Mobile_OS  
   float iKernalVersion;  
   float iReleaseVersion;  
   char* strVendor ;  
   virtual float GetKernalVersion();  
   virtual float GetReleaseVerison();  
   ~ Mobile_OS();  
class WindowsPhone : public Mobile_OS  
   char* strCodeName ;  
   char* iHardwarePlatform ;    
   int iCustomRomVersion ;  
   float GetKernalVersion();  
   float GetReleaseVerison();  
   ~ 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:

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?

Mobile_OS* Lumia = new WindowsPhone();  

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:

&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:

&DataValue = DerivedClass_StartingAddress + Offset_To_BaseClassObject_Within_DerivedClass  

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.


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


About the Author

Vijay Rajanna
India India
I am Vijay hailing from Bangalore, the silicon valley of India. I posses extensive expertise working in mobility domain. I have a natural propensity for "Research and Innovation".

My areas of interest include Pervasive computing, Cognitive sensors, HCI, and Multimedia. Being strongly resolute with an everlasting fervor for learning, over the years I have learnt to come out of comfort zone to experiment with state of the art technologies and to contribute to developer community.

I blog about innovations and latest technologies from mobility domain, about development environments and programming languages across different platforms.

Blog : Amusement of a speculative mind...[^]
Projects : Amusement of a dilettante mind...[^]
LinkedIn : Vijay D R [^]

You may also be interested in...

Comments and Discussions

SuggestionFormatting needed Pin
Mika Wendelius18-Apr-12 10:11
mvpMika Wendelius18-Apr-12 10:11 
AnswerRe: Formatting needed Pin
Vijay Sringeri18-Apr-12 18:38
memberVijay Sringeri18-Apr-12 18:38 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170813.1 | Last Updated 18 Apr 2012
Article Copyright 2012 by Vijay Rajanna
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid