Summarize the sizeof questions about classes and struct in the pen questions

Source: Internet
Author: User

1. The empty class size is 1.

class Empty{};int main(){cout<<sizeof(Empty)<<endl;}

The size of an empty class is 1 rather than 0. this is why, because if you define the object of this class, you have to have its memory address, and if its size is 0, it will not occupy the memory address, therefore, the compiler sets its size to 1.

2. Static members are not included in the class size

class Empty{public:static int val;};int Empty::val = 1;int main(){cout<<sizeof(Empty)<<endl;}

The size is still 1. The static variable is allocated in the static storage area before the program is executed, and its size is not included in the class size.

3. The private member of the base class is also inherited by the derived class, but cannot be accessed. This member is not present in the derived class.

class C1{private:int v1;};class C2:public C1{private:int v2;};

They are 4 in size and 8 in size. Some people may think that C1 V1 is private and won't be inherited, right? Actually, it will be inherited but cannot be accessed.

4. The virtual function will increase the class size.

class C1{virtual void func(){}};class C2:public C1{};

The size of both classes is 4. this is because the implementation of virtual functions depends on the virtual function table. Both classes Save the address of the virtual function table. This is a pointer and requires 4 bytes.

6. Next we will consider the size of the struct (class), which is a frequently used question in the written examination.
First, we do not consider the code for modifying the method provided by Microsoft: # pragma pack and _ declspec. Consider the situation of C ++ itself:

class C1{char str;int ival;double dval;};class C2{int ival;double dval;char str;};

They are 16 and 24 respectively. Why? Because the alignment factors need to be considered when defining them in an hour:
The char type is 1, so its address can start from any offset; the int size is 4, so its starting address must be a multiple of 4; likewise, the starting address of the double type must be a multiple of 8. The size of the entire class is an integer multiple of the maximum size of the class member.
In our example, C1: Str occupies 1 byte, 3 bytes are left empty and put in ival, and then dval. The size is 1 + 3 + 4 + 8 = 16. For C2: ival, 4 bytes are occupied. Then, 4 bytes are empty, dval is put, STR is put, then add 7 bytes (an integer multiple of 8), so the total size is 4 + 4 + 8 + 1 + 7 = 24;
Therefore, if your class or struct has many variables, it is better to place them from large to small to save space.
In the above description, one thing is inaccurate: the size of the entire class is an integer multiple of the maximum size of the class member. This is for the basic type. Suppose we add an array for it:

class C1{char str;int ival;double dval;int arr[10];};class C2{int arr[10];int ival;double dval;char str;};

Then the size of the two is 56 and 64, which is not an integer multiple of the size (40) of the maximum member arr, but an integer multiple of 8, when the array size is involved in alignment calculation, it is measured by the size of each element. For example:

class C4{int val;char str;double arr[10];};

Its size is 88: Val, which occupies 4, STR occupies 1, and 3 are empty. It reaches the address with an offset of 8 and starts to put arr, it is 80 in size and 88 in total. Think of the truth, alignment is only intended for faster access. Therefore, arrays are continuous and the offset between each element is fixed, therefore, there is no need to convert the entire size into an integer multiple of the array size.

Similarly, if a class contains other member classes, when calculating the size of the entire class, only the largest element of its members is used to participate in alignment when treating the subclass; instead of the total size of the class:

class C4{double val;char str;int arr[10];};class C1{char str;int ival;double dval;C4 obj;};class C2{char str;C4 obj;int ival;double dval;};

In C1 and C2, there is a class member C4. Let's first look at its size: Val occupies 8, STR occupies 1, and then three are empty, then there is a 40-byte arr, and the size of the entire class is an integer multiple of the maximum member Val (8). Therefore, four values must be added: 8 + 1 + 3 + 40 + 4 = 56;
Next, we will analyze C1: Char occupies one, because the following is int, SO 3 are empty, put ival, then put dval, and then put OBJ, the total size is 1 + 3 + 4 + 8 + 56 = 72;
Finally, we can see that C2: Char occupies 1, because the following type is C4, which must be 8-byte aligned. So we need to add 7, put OBJ, and then put Val, at this time, the size is: 1 + 7 + 56 + 4 = 68, because the following is a duoble, so it must be aligned with 8, so you need to fill in 4, to 72, then put dval. The total size is: 1 + 7 + 56 + 4 + 4 + 8 = 80.

This is basically the case, but Microsoft provides us with special instructions to modify the method: # pragma pack (N) and _ declspec (align (n )).

7.

First look at # pragma pack, which specifies the number of aligned Bytes: each member is aligned according to its type of alignment parameter (usually the size of this type) and a smaller alignment in the specified alignment parameter.
For example, # pragma pack (16) will not affect the size of our class, and # pragma pack (2) means that the class members must be aligned in two bytes, so C4 is large and small: 8 (VAL) + 1 (STR) + 1 (null) + 40 = 50; C1 is: 1 (STR) + 1 (null) + 4 (ival) + 8 (dval) + 50 = 64, similarly, the C2 size is also 64. If you use # pragma pack (1), the elements in the class are tightly tied together. No C4 size is 49, and c1 and c2 are 62.
This preprocessing command has some other usage, but it is not closely related to the topic.

8.

One feature of declspec (align () is that it only specifies the Data Alignment position and does not specify the actual memory length occupied by the data, after the specified data is placed at a specified position, the subsequent data is still filled in the way specified by # pragma pack, the actual size of the class/structure and the memory pattern are as follows:
Before _ declspec (align (), data is filled in the way specified by # pragma pack, as described above. When _ declspec (align () is encountered, first find the alignment point closest to the current offset (the alignment length is max (the data length, the specified value )), then fill the specified data type from this point. The subsequent data type starts after it and is still filled according to # pragma pack, until the next _ declspec (align () is encountered ()).
After all the data is filled, compare the overall alignment value of the structure with the value specified by _ declspec (align (). Take the larger value as the alignment length of the entire structure.
In particular, this parameter does not work when the value specified by _ declspec (align () is smaller than the corresponding type length. For example:

# Pragma pack (2) _ declspec (align (16) Class C4 {public: Double val; // The starting offset address is 0, accounting for 8 bytes of char STR; // The starting offset address is 8, which occupies one byte, and then fills in one byte int arr [10]; // The starting address is 10, which occupies 40 bytes }; // after data is filled, the size is 50, and the multiple of the last 16 is 64, so the total size is 64.

Finally, you can use offsetof to view the offset address of a variable. The specific content can be Baidu.

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.