0

I am trying to check the size of a class. Also, I am new to C++. I have two classes, the difference between two classes is just that one has 8 methods in it. How come there is no difference in size of class in both of it. Can you please explain me with the concepts?

#include <iostream>

using namespace std;

class A
{

    int b;
    char c[2];

    int func(int x, int y)
    {
       return x+y;  
    }
    int func1(int x, int y)
    {
       return x+y;  
    }
    int func2(int x, int y)
    {
       return x+y;  
    }
    int func3(int x, int y)
    {
       return x+y;  
    }
    int func4(int x, int y)
    {
       return x+y;  
    }
    int func5(int x, int y)
    {
       return x+y;  
    }    
    int func6(int x, int y)
    {
       return x+y;  
    }
    int func7(int x, int y)
    {
       return x+y;  
    }
};

class B
{

    int b;
    char c[2];
};


int main()
{
     cout<<"Size of class A is "<< sizeof(A)<<endl;
     cout<<"Size of class B is "<< sizeof(B)<<endl;

    return 0;
}

The output is the following -

$ ./class_size.out 
Size of class A is 8
Size of class B is 8
dexterous
  • 6,422
  • 12
  • 51
  • 99
  • 1
    Why would they? If you make a million objects, you don't want eight million unnecessary copies of functions. – chris Feb 20 '14 at 15:28

3 Answers3

4

Exactly. Methods do not contribute to the size of an object. There is only one copy of each function, and this is passed as a hidden argument.

You will notice an increase in size when you add your first virtual function, because objects with virtual functions will need at least an extra pointer to resolve virtual calls dynamically.

marcus
  • 5,041
  • 3
  • 30
  • 36
  • +1 for mentioning virtual methods. More reading on that here: http://stackoverflow.com/questions/4766323/how-to-determine-sizeof-class-with-virtual-functions – m24p Feb 20 '14 at 15:32
1

sizeof() measures the size of an instance, not the class itself. The program code for the class methods is not stored together with int b and char c[2]; that would be one huge waste of memory if you had hundreds of instances with hundreds of copies of the function code.

So, each instance of a class only stores the member variables (and, in case of virtual functions, a vtbl pointer). Function code is stored only once per class, and does not contribute to instance sizes.

DevSolar
  • 67,862
  • 21
  • 134
  • 209
0

In general, a program is divided into multiple segments in the memory, the stack and heap contains instances of classes and variables, the code segment contains instructions associated with methods and functions. The space taken for instructions in a class does not increase based on number of instances.

The sizeof operator measures the size of an instance of the class. This instance is either placed on the stack or the heap, depending on wheter you allocate memory for it dynamically or not.

If you create a member variable as static it becomes a class variable, and is not part of the sizeof. (Placed in the data segment of the program)