The size instance analysis of C + + NULL class and class without member variable _c language

Source: Internet
Author: User

The well-known C + + Hollow class has a size of 1, but there are quite a few developers who have a lot of ambiguity about the size of a class that has no member variables other than the empty class.

Here we take the following code as an example:

#include
using namespace std;
Class A {};
Class b{};
Class C:p ublic a{
virtual void fun () = 0;
};
Class D:p ublic B, Public c{};
int main ()
{
cout << "sizeof (a)" << sizeof (a) << Endl;
cout << "sizeof (b)" << sizeof (b) << Endl;
cout << "sizeof (c)" << sizeof (c) << Endl;
cout << "sizeof (d)" << sizeof (d) << Endl;
GetChar ();
return 0;
}

The output of the program execution is:

sizeof (a) =1
sizeof (b) =1
sizeof (c) =4 sizeof
(d) =8

Why is this kind of result happening? Beginners will certainly be puzzled, class a,b is clearly empty class, its size should be 0, why the compiler output of the result is 1? That's why we just said the instantiation (the empty class can also be instantiated), and each instance has a unique address in memory. To achieve this, the compiler tends to implicitly add a byte to an empty class, so that the empty class gets a unique address in memory after the instantiation , so the a,b size is 1.

and Class C is derived from Class A, which has a pure virtual function, because of the reason for the virtual function, there is a pointer to the virtual function table (vptr, there are multiple virtual functions are still only one pointer), the 32-bit system assigned to the size of the pointer to 4 bytes, so the final C class size is 4 .

The size of Class D will make many beginners wonder, Class D is derived from the class b,c, its size should be both and 5, why is 8? This is because in order to improve the efficiency of the instance in memory, there will be data alignment, so the class size is often adjusted to 4-byte integer times. and take the nearest law to the larger direction to get the nearest multiple, is the size of the class, so the size of Class D is 8 bytes (if D is derived from 3 empty classes and C, still 8).

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.