UrbanPro
true

Learn C++ Language from the Best Tutors

  • Affordable fees
  • 1-1 or Group class
  • Flexible Timings
  • Verified Tutors

Search in

Memory Layout in C++ vis-a-vis Polymorphism and Padding bits.

Somenath Mukhopadhyay
21/03/2017 0 0

I know there is no need for the knowledge of memory layout for a normal day-to-day development of C++. However, when someone goes in the bit/byte level of C++, he needs to look for such discussion.

To state the problem, let us have a class hierarchy like the following.

class A {

 public:
   virtual void funA()
   {}
   virtual ~A(){}
public:
   int a;
};

class B {
 public:
   virtual void funB(){};
   virtual ~B(){}
public:
   int b;
};

class C : public A, public B {
public:
   int c;
};

Now if we run this app using a GCC compiler on a 64 bit machine and see the sizeof() these classes, we will see A will be 16 byte, B will be 16 byte and C will be of 32 bytes . So lets discuss how these numbers have come.

To start with the discussion, i need to throw some lights about how polymorphic behavior in C++ using virtual functions is tackeled by a compiler. Whenever there is a class having virtual function, the compiler will generate a VTABLE of that class and all the objects of that class will use that VTABLE. VTABLE is nothing but a TABLE having pointers to the virtual functions in a class hierarchy. And whenever there is a VTABLE, the compiler will silently add a pointer in the beginning of an object of the class. This pointer is called vptr and it actually points to the beginning of the VTABLE.

So in the following case two VTABLES will be created (each for class A  and  B).

Hence the size of an object of class A will be size-of-a-pointer(vptr) + size-of-int. So for a 64 bit architecture (where a pointer is 8 byte long) it will be 8 + 4 = 12. If this is the case, then how come sizeof(A) shows 16. Here comes the concept of Alignment and Padding bits.

Here is an excerpt from Wiki about alignment.

"A memory address a, is said to be n-byte aligned when a is a multiple of n bytes (where n is a power of 2). In this context a byte is the smallest unit of memory access, i.e. each memory address specifies a different byte. An n-byte aligned address would have log2(n) least-significant zeros when expressed in binary.

The alternate wording b-bit aligned designates a b/8 byte aligned address (ex. 64-bit aligned is 8 bytes aligned).

A memory access is said to be aligned when the datum being accessed is n bytes long and the datum address is n-byte aligned. When a memory access is not aligned, it is said to be misaligned. Note that by definition byte memory accesses are always aligned.

A memory pointer that refers to primitive data that is n bytes long is said to be aligned if it is only allowed to contain addresses that are n-byte aligned, otherwise it is said to be unaligned. A memory pointer that refers to a data aggregate (a data structure or array) is aligned if (and only if) each primitive datum in the aggregate is aligned.

Note that the definitions above assume that each primitive datum is a power of two bytes long. When this is not the case (as with 80-bit floating-point on x86) the context influences the conditions where the datum is considered aligned or not.

Data structures can be stored in memory on the stack with a static size known as bounded or on the heap with a dynamic size known as unbounded."

To maintain the alignment, compiler inserts padding bits in the compiled code of a structure/class object.

"Although the compiler (or interpreter) normally allocates individual data items on aligned boundaries, data structures often have members with different alignment requirements. To maintain proper alignment the translator normally inserts additional unnamed data members so that each member is properly aligned. In addition the data structure as a whole may be padded with a final unnamed member. This allows each member of an array of structures to be properly aligned. .... ....

Padding is only inserted when a structure member is followed by a member with a larger alignment requirement or at the end of the structure" - Wiki

To get more info about how GCC does it, please look at

http://www.delorie.com/gnu/docs/gcc/gccint_111.html

and search for the text "basic-align".

Now lets come to this problem:

Using the example class, i have created this program for a GCC compiler running on a 64 bit Ubuntu.

int main() {

   A objA;
   C objC;
   cout<<sizeof(void*)<<endl;
   cout<<sizeof(int)<<endl;
   cout<<sizeof(A)<<endl;
   cout<<sizeof(B)<<endl;
   cout<<sizeof(C)<<endl;

   return 0;
}

And the result for this program is as the following:

8

4

16

16

32

As i was saying that object of class A will have a VPTR (pointing to the VTABLE of A) and an int. The pointer will be 8 byte long and the int will be 4 byte long. Hence before compile the size is 12 bytes. But the compiler will add extra 4 bytes at the end of int a as padding bits to maintain the alignment of class A equal to the highest length of the data member of the class which is divisible by 2, that is vPtr (8 byte long). Hence after compilation, the A's objects size will be 12 + 4 = 16 bytes.

Similarly for class B's objects.

Now object of C will have two VPTRs (one for each class A & class B) and 3 ints (a,b,c). So normally the size of object of C should have been 8 + 8 + 3*4 = 28 bytes. But here is what happens because the compiler adds padding bytes. After the addition of the padding bytes, the size becomes  8 (VPTR A) + 4 (int a) + 4 (padding bytes) + 8 (VPTR B) + 4 (int b) + 4 (int c) = 32 bytes. So the total size of C will be 32 bytes.

Note:

Type this to see the memory dump on the screen using gcc compiler

g++ -fdump-class-hierarchy=stdout MemoryLayout.cpp

where MemoryLayout.cpp is the file containing the class hierarchy…

0 Dislike
Follow 0

Please Enter a comment

Submit

Other Lessons for You

Class and Objects C++
CLASS AND OBJECT IN C++ WITH EXAMPLE FOUNDATION Lets define a variable x of integer type and give it a value 12. How will you do it. You will simply write int x=12; This statement can also be written...

Compiler vs Interpreter
Compiler Interpreter Convert all the code into binary format and execute. Convert one statement at a time and execute, then Convert next statement and execute. After conversion, source...

All About Programming And A Good Programmer.
Hi, This is my first lesson for you guys. Hope you enjoy reading it. In recent community questions, I found many people wanted to be good programmers, or wanted to have good hands on certain language,...

C++ Program-Working with constant using const keyword
//Header files #include<iostream.h>#include<conio.h> //Main function void main(){ //using const keyword to declare constants with a specific type const int len=10; const int br=5; const...
S

Software Development Training In Jaipur
Satyam Web Solution provides website designing &development and software designing &development training in Jaipur for various stream’s students. MCA 6 month Industrial Training/Internship B....
X

Looking for C++ Language Classes?

The best tutors for C++ Language Classes are on UrbanPro

  • Select the best Tutor
  • Book & Attend a Free Demo
  • Pay and start Learning

Learn C++ Language with the Best Tutors

The best Tutors for C++ Language Classes are on UrbanPro

This website uses cookies

We use cookies to improve user experience. Choose what cookies you allow us to use. You can read more about our Cookie Policy in our Privacy Policy

Accept All
Decline All

UrbanPro.com is India's largest network of most trusted tutors and institutes. Over 55 lakh students rely on UrbanPro.com, to fulfill their learning requirements across 1,000+ categories. Using UrbanPro.com, parents, and students can compare multiple Tutors and Institutes and choose the one that best suits their requirements. More than 7.5 lakh verified Tutors and Institutes are helping millions of students every day and growing their tutoring business on UrbanPro.com. Whether you are looking for a tutor to learn mathematics, a German language trainer to brush up your German language skills or an institute to upgrade your IT skills, we have got the best selection of Tutors and Training Institutes for you. Read more