Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

C speech domain

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >

Share

Shulou(Shulou.com)06/01 Report--

Some data do not need to occupy a good byte when stored, only one or more binary bits are needed. For example, as long as the switch is powered on and off, it is sufficient to use 0 and 1, that is, to use a binary. It is based on this thinking that C language provides a kind of data structure called bit domain.

When constructing body demarcation, we can specify the number of binary bits (Bit) occupied by a member variable, which is the bit field. Take a look at the following example:

Struct bs {unsigned m; unsigned n: 4; unsigned char ch: 6;}

The preceding number is used to limit the number of digits occupied by member variables The member m has no limit, and it can be calculated that it takes up 4 bytes (Byte) of memory according to the data type. Members n and ch are limited by the previous number, so that the length can no longer be calculated based on the data type, and they can distinguish between 4 and 6 bits (Bit) of memory.

The value scale of n and ch is very unlimited, and overflow occurs when the data is slightly larger. Please see the following example:

# include int main () {struct bs {unsigned m; unsigned n: 4; unsigned char ch: 6;} a = {0xad, 0xE,'$'}; / / enter printf ("% # x,% # x,% c\ n", a.m, a.n, a.ch) for the first time; / / re-enter A.M = 0xb8901c; a.n = 0x2d; a.ch ='z' after changing the value Printf ("% # x,% # x,% c\ n", a.m, a.n, a.ch); return 0;}

Operational consequences:

0xad, 0xe, $

0xb8901c, 0xd,:

With regard to n and ch, the data entered the first time is intact, and the data entered the second time is complete.

When entering for the first time, the values of n and ch are identified as 0xE and 0x24 (the ASCII code corresponding to'$'is 0x24), which is converted to 1110 and 10 0100 in binary, which does not exceed the limit and can be entered normally.

On the second input, the values of n and ch become 0x2d and 0x7a (the ASCII code corresponding to'z' is 0x7a), and the binary discrimination is 10 1101 and 111 1010, which all exceed the limit. The transcendental part is truncated directly, leaving 1101 and 11 1010, which are converted to 0xd and 0x3a in hexadecimal (the character corresponding to 0x3a is:).

C speech specification rules that the width of the bit field cannot exceed the length of the data type on which it depends. To put it simply, most member variables are typed, and this type limits the maximum length of member variables, and the previous number cannot exceed this length.

For example, the following bs,n type is unsigned int, with a length of 4 bytes, totaling 32 bits, then the number before n cannot exceed 32 Bytes. If the type of ch is unsigned char, the length is 1 byte, the total 8 bits, then the number before ch cannot exceed 8.

We can think of the bit field technique as selecting a local width in the memory occupied by the member variables to store the data.

The C language specification also rules that as long as an infinite number of data types can be used in the bit field. In ANSI C, these data types are int, signed int and unsigned int (int acquiescence is signed int); it is also supported by C99.

We have stopped to understand the C language specification and the difference between ANSI C and C99 in the VIP tutorial "two sets of C language specifications".

However, the compiler stops expanding when it is completed in detail, and the char, signed char, unsigned char, and enum types are rated, so the following code does not conform to the C language specification, but it can still be supported by the compiler.

Storage of bit domain

The C language specification does not have a detailed storage method for regular bit fields, and different compilers have different completion, but they all try their best to compact storage space.

The detailed storage rules for bit fields are as follows:

1) when the type of adjacent members is opposite, if the sum of their bit widths is less than the sizeof size of the type, then the previous member is adjacent to the previous member storage until it cannot be contained; if the sum of their bit widths is greater than the sizeof size of the type, then the previous member will re-start the storage unit with an offset of an integer multiple of the type size.

Take the following bit field bs as an example:

# include int main () {struct bs {unsigned m: 6; unsigned n: 12; unsigned p: 4;}; printf ("% d\ n", sizeof (struct bs)); return 0;}

Operational consequences:

four

The type of m, n, p is mostly unsigned int,sizeof. The consequence is 4 bytes (Byte), that is, 32 bits (Bit). The sum of the bit widths of m, n, p is 6: 12, 4 = 22, less than 32, so they are stored next to each other, and there are no cracks at both ends.

The size of sizeof (struct bs) believes that 4, not 3, is due to the need to align memory to 4 bytes in order to improve access effectiveness, which will be explained in the "C language memory alignment to improve addressing effectiveness" section of the topic "C language and memory".

If you change the bit width of member m to 22, then the input consequence will be 8. Since 22 is 12 = 34, a position greater than 32 will start storage, and the offset of absolute m is sizeof (unsigned int), that is, 4 bytes.

If the bit width of member p is also changed to 22, the input consequence will be 12, and none of the three members will be stored next to each other.

2) when the types of neighboring members are different, different compilers have different completion plans, GCC will compact storage, but VC/VS will not.

Take a look at the following bit field bs:

# include int main () {struct bs {unsigned m: 12; unsigned char ch: 4; unsigned p: 4;}; printf ("% d\ n", sizeof (struct bs)); return 0;}

The operation consequence under GCC is 4, and the three members are stored next to each other; under VC/VS, the operation consequence is 12, and the three members are stored according to their own type (as opposed to the storage method when not pointing to the location width).

The length discrimination of m, ch and p is 4, 1, 4 bytes, which takes up a total of 9 bytes of memory. Why is the input consequence under VC/VS 12? This question will be solved for you in the "C language memory alignment to improve addressing effectiveness" section of the topic "C language and memory".

3) if members intersect non-domain members, then austerity will not stop. For example, about the following bs:

Struct bs {unsigned m: 12; unsigned ch; unsigned p: 4;}

The consequence of sizeof under each compiler is 12.

After the following analysis, we find that bit field members often do not occupy intact bytes, and sometimes do not sweep the end of bytes, so it is meaningless to use & to obtain the address of bit field members, and C language also prevents it from doing so. The address is the number of bytes (Byte), not the number of bits (Bit).

Anonymous domain

Bit field members can not have a famous name, but only give the data type and bit width, as shown below:

Struct bs {int m: 12; int: 20; / / the member of this domain cannot use int n: 4;}

Anonymous domains are commonly used to fill or regulate membership. Since there is no famous name, the anonymous domain cannot be used.

In the following example, if there are no unknown members with a bit width of 20, m and n will be stored next to each other, and the consequence of sizeof (struct bs) will be 4; with these 20 bits as padding, m, n will leave the storage, and the consequence of sizeof (struct bs) will be 8.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Network Security

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report