About the sizeof operator (Union + struct + array)

Source: Internet
Author: User
Tags element groups
About the sizeof Operator

We often use sizeof in C programming, especially during the examination, this is often a required knowledge point.

Next, let me talk about some problems of sizeof in programming and let friends know what it is when they encounter it. But before talking about it, familiarize yourself with this formula.

A % (min (B, c) = 0/* This formula is not summarized by me, but I wrote this formula */

A: Address of the Variable)

B: The number of bytes occupied by the variable (bytes)

C: current alignment modules)

 

Another important thing is to calculate the sizeof of struct. If the last member variable does not occupy the address required by the Full-byte alignment, you need to fill in several padding S.

 

Let's take a look at the following question:

/*************************************** *************/

Union

{

Long I;

Short int K [5];

Char C;

} Date;

 

Struct B

{

Short int cat;

Char sheep;

Date cow;

Double Dog;

} Too;

 

Date Max;

Evaluate: the value of sizeof (struct date) + sizeof (max.

/*************************************** *************/

Let's split it step by step.

Union

{

Long I;

Short int K [5];

Char C;

} Date;

First, declare that the computer is 4-byte aligned by default.

We know that I occupies 4 bytes, K occupies 10 bytes, and C occupies one byte. Since AA is a union, all its members share the memory, therefore, the size of a depends on the size of array K. What is the size of? Is it 10? Obviously not. Because the bytes need to be aligned, and the system defaults to 4 bytes alignment, A is actually 12 bytes, that is, it needs to add two padding based on 10, to ensure that long member variables in a are correctly aligned with multiples of 4 (mainly to ensure that when a date array is defined, the starting position of the second element of the array is correct ).

The sizeof of Union indicates the amount of storage space required by all members of this type, including the unused space after members and members, the rule is the length that can be occupied by the elements to be joined to this type of Array (n times the length of N element groups for any type, including Union ).

 

Struct B

{

Short int cat;

Char sheep;

Date cow;

Double Dog;

} Too;

Here, cat occupies 2 bytes, sheep occupies 1 byte, cow occupies 12 bytes, and dog occupies 8 bytes. struct is different from Union, and its size is the sum of all member sizes, because of alignment, it is definitely not a simple 23 (2 + 1 + 12 + 8) bytes. The following describes the actual situation.

Bytes

×

×

×


×

×

×

×

×

×

×

×

×

×

×

×

×

×

×

×

×

×

×

×

Storage address

0

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

×: Represents the 1 byte of the variable;

○: Represents the byte complement during byte alignment.

 

Why?

1. The cat variable occupies 2 bytes. All characters are stored starting from 0, so the 0-1 bytes naturally store the variable cat;

2. The Variable sheep occupies 1 byte. For more information about the storage, see the formula "A % (min (B, c) = 0. In this case, a = 2, B = 1, C = 4, min (B, c) = 1,2% 1 = 0. The condition is met and the byte complement is not required, therefore, the two-byte storage Variable sheep;

3. The variable cow occupies 12 bytes. In this case, a = 3, B = 12, c = 4, min (B, c) = 4, 3% 4! = 0, so the byte complement is required. After the complement is 1, A = 0. If the condition is met, the cow is stored in 4 to 15 bytes;

4. The variable dog occupies 8 bytes. At this time, a = 16, B = 8, c = 4, min (B, c) = 4, 16% 4 = 0, do not need to fill in byte, dog is placed in 16 to 23 bytes.

Therefore, sizeof (struct B) = 24, we can see that the value of sizeof (struct B) + sizeof (max) is 24 + 12 = 36.

 

Let's look at the following code:

Union aaa

{

Struct

{

Char;

Short B;

Char C;

} Half;

Long D;

} Number;

After running the command, the following result is displayed:

Sizeof (Union AAA) = 8

Sizeof (number. Half) = 6

The size of the struct is 6, while that of the Union is 8.

Char A occupies 1 byte; short B occupies 2 byte; char C occupies 1 byte. The formula "A % (min (B, c) = 0" is used to know:

Bytes

×

×

×

×

Storage address

0

1

2

3

4

5

The three members only occupy 5 bytes, but to ensure that the short B byte occupies 2 is aligned, that is, it must be a multiple of 2, so you must add a padding, therefore, sizeof (number. half) = 6.

For union, there are two members, one is a 6-byte struct, and the other is a 4-byte long d. The size of Union depends on struct, which is just the reason, 2 padding must be added after 6 bytes to become a multiple of 4 to ensure that D can be aligned in bytes.

 

Char * P = "World Cup ";

Char A [] = "World Cup ";

Strlen (p) = 9

Strlen (A) = 9

Sizeof (p) = 4

Sizeof (A) = 10

Sizeof (* P) = 1

 

Strlen () is a function that is included in string. H. It calculates the length of a string, and does not contain '\ 0'

Sizeof () is an operator that represents the space occupied by an arithmetic string, including '\ 0'

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.