Here I am going to tell different memory layout of a class of different types in C++.
Like Empty Class, Simple Class having non-static data members and functions, Class having static data members and functions, Derived Class, Class having Virtual Functions and so on. Also I am going to explain How Virtual table, Virtual pointer for Virtual functions are internally working.
Example 1) Empty Class
class Test
{
};
main()
{
Test obj;
cout << "obj's Size = " << sizeof(obj) << endl;
}
Output:
Sobj's Size = 1 //minimum memory required for the entry of the class in the symbol table.
Example 2) Simple Class with static data members and function's memory layout
class Test
{
public:
static int data;
int data;
};
main()
{
Test obj;
cout << "obj's Size = " << sizeof(obj) << endl;
}
Output:
Sobj's Size = 4//Size of 'int' data type is 4bytes only, not the static data member
Example 3) Simple Class with non-static data members and function's memory layout
class Test
{
public:
int data;
int fun1();
};
main()
{
Test obj;
cout << "obj's Size = " << sizeof(obj) << endl;
}
Output:
Sobj's Size = 4//Size of 'int' data type is 4bytes
Note: Any Plane member function does not take any memory.
Example 4: Memory Layout of Derived class
class Test
{
public:
int a;
int b;
};
class dTest : public Test
{
public:
int c;
};
main()
{
Test obj1;
cout << "obj1's Size = " << sizeof(obj1) << endl;
dTest obj2;
cout << "obj2's Size = "<< sizeof(obj2) << endl;
}
OUTPUT:
obj1's Size = 8
obj2's Size = 12
Example 5: Memory layout If we have one virtual function.
class Test
{
public:
int data;
virtual void fun1()
{
cout << "Test::fun1" << endl;
}
};
main()
{
Test obj;
cout << "obj's Size = " << sizeof(obj) << endl;
}
OUTPUT:
obj's Size = 8//size of one data member and a pointer for virtual function
Note: Adding one virtual function in a class takes 4 Byte extra.
Example 6: More than one Virtual function
class Test
{
public:
int data;
virtual void fun1() { cout << "Test::fun1" << endl; }
virtual void fun2() { cout << "Test::fun2" << endl; }
virtual void fun3() { cout << "Test::fun3" << endl; }
virtual void fun4() { cout << "Test::fun4" << endl; }
};
main()
{
Test obj;
cout << "obj's Size = " << sizeof(obj) << endl;
}
OUTPUT:
obj's Size = 8
Note: Adding more virtual functions in a class, no extra size taking i.e. Only one machine size taking(i.e. 4 byte)
Example 7: Priority of a Virtual Function in a Class Layout in C++
class Test
{
public:
int a;
int b;
Test(int temp1 = 0, int temp2 = 0)
{
a=temp1;
b=temp2;
}
int getA()
{
return a;
}
int getB()
{
return b;
}
virtual ~Test();
};
main()
{
Test obj(5, 10);
// Changing a and b
int* pInt = (int*)&obj;
*(pInt+0) = 100;
*(pInt+1) = 200;
cout << "a = " << obj.getA() << endl;
cout << "b = " << obj.getB() << endl;
}
OUTPUT:
a = 200
b = 10
If we Change the code as then
// Changing a and b
int* pInt = (int*)&obj;
*(pInt+1) = 100; // In place of 0
*(pInt+2) = 200; // In place of 1
OUTPUT:
a = 100
b = 200
Note: Who sits 1st place of Class : Answer is VPTR
VPTR - 1st placed in class and rest sits after it.
Example 8:
class Test
{
virtual void fun1()
{
cout << "Test::fun1" << endl;
}
};
main()
{
Test obj;
cout << "VPTR's Value " << (int*)*(int*)(&obj+0) << endl;
}
OUTPUT:
VPTR's Value 0046C060
NOTE: This VPTR's value is a address of Virtual table. Lets see in next Example.
Example 9: Virtual Function with Function Pointer in C++
class Test
{
virtual void fun1()
{
cout << "Test::fun1" << endl;
}
};
typedef void (*Fun)(void);
main()
{
Test obj;
cout << "VPTR's Address " << (int*)(&obj+0) << endl;
cout << " VIRTUAL TABLE 's Address " << (int*)*(int*)(&obj+0) << endl; // Value of VPTR
cout << "Value at first entry of VIRTUAL TABLE " << (int*)*(int*)*(int*)(&obj+0) << endl;
Fun pFun = (Fun)*(int*)*(int*)(&obj+0); // calling Virtual function
pFun();
}
OUTPUT:
VPTR's Address 0012FF7C
VIRTUAL TABLE 's Address 0046C0EC
Value at first entry of VIRTUAL TABLE 0040100A
Test: fun1
No comments:
Post a Comment