Calculating the size of a C ++ class

Here the record about how to calculate the size of the class object.

Probably summed up, the size of the class to consider the following:

  1. Non-static member variable size
  2. Align to how many bits of data
  3. Whether the virtual function (ie, does not need to be a pointer to the virtual function table, considering the case of the inheritance, then inherited the need to look at how many points to virtual function table pointer)

Non-static member variable size

Empty class

First we look at an example of when nothing:

class test{

};

Can be seen, the size of the object class instance is 1. This is because even though the class is empty class, the compiler allocates a byte of space occupying, and the real difference for blank / empty variable off (after all instantiated in fact allocate some memory space, if not allocated space, then it would be without instantiating almost). But note that if the blank class is inherited as a base class, then, will not have an impact on its inherited class space, that is inheriting the moment, the base class size 0 and goes to inherit its class size only to its own members about the variable (here the default is single inheritance):

#include <iostream>
#include <string>

using namespace std;

class test {

};

class test2 : public test {
private:
    int a = 2;
};

int main() {
    test2 tmp;
    cout << "size of class test " << sizeof(tmp) << endl;
    getchar();
    return 0;
}

Only member variables

#include <iostream>
#include <string>

using namespace std;

class test3 {
private:
    int a = 3;
    float b = 3.0;
};

int main() {
    test3 tmp;
    cout << "size of class test " << sizeof(tmp) << endl;
    getchar();
    return 0;
}

Can be seen, the system 32, intand float4 bytes, so the size of final instance of class 8.

Effects of static static member variables of class size

#include <iostream>
#include <string>

using namespace std;

class test4 {
private:
    int a = 3;
    float b = 3.0;
    static int c;
};

int main() {
    test4 tmp;
    cout << "size of class test " << sizeof(tmp) << endl;
    getchar();
    return 0;
}

We can see, the output is still 8, even with the static member variables. This is because the place is actually a static member variables are stored elsewhere (global variable / static variable area, after all, to make visible all instances), so it will not affect the size of the instance.

Only member functions

class funcOnly {
public:
    funcOnly() {};
    ~funcOnly() {};
private:
    void boo() {};
};

Can be seen, the function does not occupy space class, where 1 is because the compiler is allocated one byte placeholder. We can also verify follows:

Thus, the space class from 1 byte 4 bytes into, as no longer empty class as a 1 byte.

Align to how many bits of data

class test5 {
private:
    char d;
    int a = 3;
    float b = 3.0;
    static int c;
};

int main() {
    test5 tmp;
    cout << "size of class test " << sizeof(tmp) << endl;
    getchar();
    return 0;
}

Although charonly occupies one byte, but because of the presence data alignment, it is necessary padded to a multiple of 4 (padded charto 4 bytes, the CPU calculates the sake of convenience). Further, there may in fact differentiate several other circumstances, such as a continuous two chartogether:

And a separate release:

可以看到,顺序对实例大小的影响。这是因为,如果两个char放在一起的话,那么编译器会将这两个char放在一起,然后补齐。如果不是连续放着的,那么会分别补齐到4字节。因此,尽量“凑”变量类型到4字节,这样可以让补齐后的实例大小小一些。另外,要注意的是,含有数组的时候是一个个地连续地放,而不是视为整体,所以如果有数组,例如:

class test8 {
private:
    char d[12];
    int a = 3;
};

再举个例子:

class test8 {
private:
    char d[11];
    int a = 3;
};

有数组的时候,先连续摆放好,然后再补齐。

注意,上面说到的补齐到4字节是因为类里面最大的类型就是int,是4个字节,如果有更大的,那么就要补齐到更大的字节对应的倍数,如:

这里出现了8字节的double,那么补齐到8字节。其实之所以补齐8字节,是因为我是在Windows平台下编译的,如果是Linux,即是用GCC,那么其实还是当类型大小超过4字节的时候,只要求起始地址是4的整数倍。

有无虚函数

这部分理解要结合虚函数相关的知识。

class funcOnly2 {
public:
    funcOnly2() {};
    virtual ~funcOnly2() {};
private:
    void boo() {};
};

int main() {
    funcOnly2 tmp;
    cout << "size of class funcOnly2 " << sizeof(tmp) << endl;
    getchar();
    return 0;
}

这里因为多了个虚指针,所以大小为4,所以类大小要加上虚指针的4:

class funcOnly2 {
public:
    funcOnly2() {};
    virtual ~funcOnly2() {};
private:
    void boo() {};
    int br;
};

例如上面这样的,就是int的4加上虚指针的4。

总结

大概总结下,类的大小需要考虑以下内容:

  1. 所有非静态成员变量大小
  2. 数据对齐到多少位
  3. 有无虚函数(即需不需要指向虚函数表的指针,如果考虑继承的情况,则还需要看继承了多少个指向虚函数表的指针)

参考

C++中空类占一字节原因详解:建议看,对空白类的讲解比较详细
sizeof计算空间大小的总结
《C++ Primer 第5版》

Guess you like

Origin www.cnblogs.com/yejianying/p/cpp_size_of_class.html