Shivam Naik
Shivam Naik

Reputation: 115

Memory allocation of member functions of a class object

For a simple C++ program.

#include <iostream>
#include <string>
using namespace std;

class Student
{ 
     float gpa;
     public:
     void read()
     {
         cin>>gpa;
     }

     void display()
     {
           cout<<"STUDENT GPA : "<<GPA<<endl;
     }
 };

 void main()
 {
     Student s1;
 }

For the object s1, 4 bytes must be allocated in main memory. But when checked, the memory allocated is slightly higher than 4 bytes because member functions, constructors and destructors allocate some memory.! How is the size calculated for all the functions and is it possible to optimize the code by reducing these extra allocations.?

Upvotes: 0

Views: 1758

Answers (3)

svick
svick

Reputation: 245056

For the object s1, 4 bytes must be allocated in main memory.

They don't. You only use the object inside a single function, don't manipulate it using pointers or references and it contains only a single float field, so it's possible that the compiler decides to store that field inside a register, not in the memory.

Also, you don't actually use that field, so it's possible the compiler does not allocate the variable and the field at all, since it's an optimization that's not observable.

To show this, you can look at disassembly of your code (using clang 3.3 on x64 with -O3):

main:
        xorl    %eax, %eax
        ret

Note that all this code does is to zero-out the EAX register, which is just the implicit return 0;.

Upvotes: 1

Serge Ballesta
Serge Ballesta

Reputation: 149185

First, this is clearly an implementation detail. I mean that different compilers or compilation options could lead to different values. But as you have no virtual functions here (*), the methods, constructors and destructor should not need any space in the object itself.

And (after fixing some errors and warnings in your code: main shall beint instead of void, and GPA should be gpa...) just displaying sizeof on a 32 bits architecture (clang version 3.4.1):

 int main()
 {
     Student s1;
     cout << "Size: " << sizeof(s1) << endl;
     return 0;
 }

displays... :

Size: 4

But it could be 8 on a 64 bits architecture or if using #pragma pack(8)

(*) Still an implementation detail, but virtual functions are commonly implemented as vtables meaning virtual functions tables. That mean that the object contains either a pointer to a table of the virtual functions of the object (only 1 pointer) or directly a copy of that table (one pointer per virtual function). Contructors are not virtual and occupy no space, but virtual destructors use one entry in the vtable.

Upvotes: 1

user207421
user207421

Reputation: 311054

But when checked, the memory allocated is slightly higher than 4 bytes

Correct.

because member functions, constructors and destructors allocate some memory.

Incorrect. It is because of memory alignment requirements in your case, and in other cases because virtual functions require some form of table which requires a pointer to point to it. The memory cost for constructors and destructors and methods is paid once, not per object.

How is the size calculated for all the functions

It isn't.

and is it possible to optimize the code by reducing these extra allocations.?

Of course, but that has no effect on the sizeof an object.

Upvotes: 2

Related Questions