Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do class methods increase the size of the class instances?

The question is pretty straight forward. For clarity, consider the example below:

// Note that none of the class have any data members // Or if they do have data members, they're of equal size, type, and quantity class Foo { public:     void foo1();     void foo2();     // 96 other methods ...     void foo99(); };  class Bar { public:     // Only one method     void bar(); };  class Derived1 : public Foo { }; class Derived2 : public Bar { };  int main() {     Foo f;     Bar b;     Derived1 d1;     Derived2 d2;     return 0; } 

Do instances f, b, d1, and d2 all occupy the same amount of space in memory? As an extension to this question, would copying instances of Foo when passing it around take longer than Bar, theoretically?

like image 627
Zeenobit Avatar asked Nov 08 '11 22:11

Zeenobit


People also ask

Do methods take up memory?

No. Methods occur only once in memory1. They don't vary on a per instance basis, so they don't need storage on a per-instance basis.

What is the purpose of a method in a class?

Methods can modify all variables of an object. Inside the class you have to use the self keyword, which refers to the instance created from the class. Because methods can be called from an object (instance), they are sometimes called instance methods.

Does object size depend on class data members?

So as objects are data only, it calculate size by adding all the sizes of data members of a class. Member functions are common for all the objects, it only differ by the first argument of the particular objects pointer (called this pointer) which is passed as the hidden pointer to every member function of the class.

What will be the size of the object of the class?

The size of object of a class depends on the no. of bytes occupied by the data members of the class. }; The object of class student will occupy space of 8 bytes.


1 Answers

Only instance data increases the size of instances of a class (in all implementations that I know of), except that if you add virtual functions or inherit from a class with virtual functions then you take a one-time hit for a v-table pointer.

Also, as someone else correctly mentions the minimum size of a class is 1 byte.

Some examples:

// size 1 byte (at least) class cls1 { };  // size 1 byte (at least) class cls2 {     // no hit to the instance size, the function address is used directly by calling code.     int instanceFunc(); };  // sizeof(void*) (at least, for the v-table) class cls3 {     // These functions are indirectly called via the v-table, a pointer to which must be stored in each instance.     virtual int vFunc1();     // ...     virtual int vFunc99(); };  // sizeof(int) (minimum, but typical) class cls4 {     int data; };  // sizeof(void*) for the v-table (typical) since the base class has virtual members. class cls5 : public cls3 { }; 

Compiler implementations may handle multiple virtual inheritance with multiple v-table pointers or other other methods, so these too will have effect on the class size.

Finally, member data alignment options can have an impact. The compiler may have some option or #pragma to specify that member data should have a starting address that is a multiple of the number of bytes specified. For example, with alignment on 4 byte boundaries and assuming sizeof(int) = 4:

// 12 bytes since the offset of c must be at least 4 bytes from the offset of b. (assuming sizeof(int) = 4, sizeof(bool) = 1) class cls6 {     int a;     bool b;     int c; }; 
like image 110
Codie CodeMonkey Avatar answered Oct 15 '22 00:10

Codie CodeMonkey