Spin: Why is C + + hollow and empty struct size 1?

Source: Internet
Author: User
Tags diff

Reference: http://www.spongeliu.com/260.html

Why is C + + hollow class and empty struct size 1? On November +, in C language, language learning, by sponge

This article is a translation, with the previous article echoed, the original text here.

For structs and empty class size is 1 This problem, first of all this is a C + + problem, in C language, the size of the empty structure is 0 (of course, this is compiler-related). The empty and empty structures here refer to the absence of any members in the class or struct body.

In C + +, the size of the empty class and the empty struct is 1 (compiler-related), which is why? Why not 0?

This is because the C + + standard stipulates that "No object shall has the same address in memory as any other variable", that is, any different object cannot have the same memory address. If the empty class size is 0, if we declare an array of objects of this class, then each object in the array has the same address, which is obviously against the standard.

But maybe you have another question, why is there such a boring rule in the C + + standard?

There is, of course, a reason for this provision. We assume that there is a type T in C + +, we declare an array of type T, and then we declare a pointer of type T to an element in the middle of the array, then we subtract the pointer by 1 and we should get another index of the array. The following code:

12345
T Array[5];  int diff = &array[3]-&array[2]; diff = 1

The above code is a pointer operation that subtracts two pointers, and the compiler makes the action shown in the following facet:

diff = ((char *) &array[3]-(char *) &array[2])/sizeof T;

The formula should not be difficult to understand, it is obvious that the calculation of this formula depends on the sizeof T. Although the above is just an example, basically all pointer operations depend on the sizeof T.

Well, let's look at the following example, if allowing different objects to have the same address will raise what kind of problem:

1234
&ARRAY[3]-&array[2] = &array[3]-&array[1]                       = &array[3]-&array[1]                       = &array[3]-&A Mp;array[0]                       = 0

As we can see, in this example, if each object has the same address, there is no way to differentiate between objects by pointer operations. A more serious problem is that if sizeof T is 0, it causes the compiler to produce an operation with a exception of 0, which throws an uncontrolled error.

For this reason, if the size of the struct or class is allowed to be 0, the compiler needs to implement some complex code to handle the pointer operations of these exceptions.

Therefore, the C + + standard stipulates that different objects cannot have the same address. So how can we ensure that this condition is met? The simplest way to do this is not to allow the size of any type to be 0. So the compiler adds a dummy byte to each empty class or empty struct (some compilers may add more) so that the empty class and the empty structure will not be 0 in size, so that their objects will have separate addresses from each other.

Spin: Why is C + + hollow and empty struct size 1?

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.