The size of various classes in C + +

Source: Internet
Author: User

Note: The compiler version used in this test instance is clang-703.0.29, the system int length is 4 bytes, and the pointer length is 8 bytes.

1. Empty class

class A {};

The result of the empty class sizeof is 1, why not 0? Because the C + + standard specifies that the memory addresses of two different instances must be different (poke here), use this byte to occupy different memory addresses, so that two instances of an empty class can be distinguished from each other. While most compilers support null base class optimizations (empty base classes optimization, EBCO), classes derived from empty base classes do not increase by 1 bytes, such as:

class b:a {  int  A;};

The result of sizeof (B) is 4 instead of 5 or 8.

2. Classes with static data members

class C {  int  A;   Static int b;};

sizeof (C) results are 4, and static data members are stored outside the class object.

3. Classes with non-virtual function members

class D {  public:    void  func1 () {}    staticvoid  Func2 () {}};

sizeof (D) results are 1, both ordinary member functions and static member functions are stored outside the class object.

4. Classes with virtual function members

class E {  public:    virtualvoid  func () {}};

sizeof (E) The result is 8, and the class object with the virtual function member contains a pointer to the virtual table of the class.

5. Alignment rules

class F {  char  A;   int   b;};

sizeof (f) has a result of 8 instead of 5, because the maximum alignment value of f is 4 (int), so A and B are padded by 3 bytes.

6. Common derived classes

class  Public C {  int  A;};

The result of sizeof (G) is 8, and the derived class holds a copy of the non-static data member (a in c) of the base class.

7. Base class derived classes with virtual functions

class  Public E {};

sizeof (H) results in 8, and because of the virtual function in the base class, a pointer to virtual table of the derived class must also be saved in the derived class.

8. Derived classes for multiple inheritance

class  Public  Public C {};

sizeof (I) results are added in the case of 8,b and C non-static data member lengths and whether pointers to virtual table need to be pointed.

9. Alignment rules under multiple inheritance

class  Public  Public F {  char  A;};

sizeof (J) resulted in a total of two int two char in the 16,b, F, and J classes, and 16 for Zi 4*4.

10. Derived classes for virtual inheritance

class Virtual  Public A {}; class Virtual  Public B {};

The result of sizeof (K) is 8, and the derived class holds a pointer to the virtual inheriting base class unique instance. The result of sizeof (L) is 16 because the alignment rule int is padded. Note that virtual inheritance and virtual functions are two completely different concepts that are not directly related to each other.

class Virtual  Public A {}; class  Public  Public M {};

The result of sizeof (N) is 16, although the virtual inheritance pointers in the K class and M classes point to the same base class instance, but they are different pointers, and the N classes need to hold both pointers.

The size of various classes in C + +

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.