Do class methods increase the size of the class instances?

后端 未结 4 1168
鱼传尺愫
鱼传尺愫 2020-12-14 00:40

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          


        
相关标签:
4条回答
  • 2020-12-14 01:06

    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;
    };
    
    0 讨论(0)
  • 2020-12-14 01:06

    Foo and Bar should each be one byte. Derived1 and Derived2 might be one or two bytes.

    The reason is the compiler stores all static information in the executable code, including all member functions. Where your code calls foo1 on an instance of Foo, it just calls Foo::foo1(this), which is the same for every Foo in the program. virtual functions are an exception, but do not add additional size per member function, only a one-time additional size (usually 4/8 bytes) if there's any virtual functions at all.

    0 讨论(0)
  • 2020-12-14 01:21

    Strickly speaking, this is implementation dependent. But the number of methods should not change the size of class objects. For non-virtual methods, there is nothing inside the object memory that relates to method pointers. If you have virtual methods, then each object has a single pointer to a vtable. The vtable grows when you add more methods, but the pointer size stays the same.

    Further info: for non-virtual methods, the compiler tracks method pointers for each class. when you call a non-virtual method, the compiler passes a pointer to the method that points to the object, either as a hidden parameter, or on the stack. This is how a method 'knows' its object and has access the this pointer. For virtual methods, the method pointer is actually an index into the vtable, so the compiler passes this to the dereferenced entry in the vtable.

    0 讨论(0)
  • 2020-12-14 01:25

    Yes they will. Actually in your case the size will be 1. In C++ a class even without any data member will have a size of 1.

    0 讨论(0)
提交回复
热议问题