About the structure memory alignment and bit segment in C language

Tip: After the article is written, the table of contents can be automatically generated. For how to generate it, please refer to the help document on the right


Structure memory alignment and bit segment

One, the memory alignment of the structure

1. Why is there memory alignment?

In order to calculate the size of the structure, the first thing to understand is the memory alignment of the structure. So why is there memory alignment? Most references say:

  1. Platform reason (transplant reason): Not all hardware platforms can access any data at any address; some hardware platforms can only
    fetch certain types of data at certain addresses, otherwise a hardware exception will be thrown.
  2. Performance reasons: Data structures (especially stacks) should be aligned on natural boundaries as much as possible. The reason is that in order to access unaligned
    memory, the processor needs to make two memory accesses; while aligned memory access only requires one access.

# Give an example : For a computer with 32 address buses, the unit of reading each time is 4 bytes, and the fake entry defines the following structure:

struct s1
{
    
    
char c1;
int i;
char c2;
}

Since c1 occupies 1 byte, i occupies 4 bytes, and c2 occupies 1 byte, when the computer reads, it will first read the 3 bytes of c1 and i (four bytes in total), and then Read the last byte of i and c2. Therefore, the calculator not only needs to read the memory twice, but also needs to splice the data of i, which virtually wastes running time. Therefore, in order to reduce the waste of time, memory alignment is adopted.
In general: memory alignment is a way to trade space for time.

2. How to memory alignment

First, you need to master the rules of memory alignment:

  1. The first member is at an address with an offset of 0 from the structure variable.
  2. Other member variables should be aligned to an address that is an integer multiple of a certain number (alignment number).
    Alignment number = the smaller value of the compiler default alignment number and the member size.
    The default value in VS is 8
  3. The total size of the structure is an integer multiple of the maximum alignment number (each member variable has an alignment number).
  4. If the case where the nested structure, the nested structure is aligned to an integer multiple of the maximum number of their alignment, the whole structure
    body is the size of all the maximum alignment (alignment number including nested structure) is an integer multiple of .
    Try the structure defined above:

Insert picture description here
As shown in the figure, the blue part is the number of bytes developed (where c1, i, and c2 are 1, 4, 1 bytes respectively), and the red part is the number of bytes that have passed the memory alignment rules.
It can be seen that the structure occupies a total of 12 bytes and needs to be read 3 times.

3. When designing the structure, how to meet the alignment and save space

The answer is: Let the members that take up small space be gathered together
as much as possible. Continue to take the above structure as an example; the structure members we defined remain unchanged, just change the order and try:

struct s1
{
    
    
char c1;
char c2;`
int i;
}

Calculate the size of the structure:
Insert picture description here
you can see that after the improved definition form, the structure only occupies 8 bytes, and only reads twice.

Second, position

1. What is a position

Bit segments are generally used in embedded development and underlying development, with the goal of greatly saving memory space.
The declaration and structure of the bit segment are similar, with two differences:
1. The members of the bit segment must be int, unsigned int or signed int.
2. There is a colon and a number after the member name of the bit segment.
such as:

struct A {
    
    
 int _a:2;
 int _b:5;
 int _c:10;
 int _d:30;
};

The number after the member name indicates the number of digits that can be created.

2. Memory allocation of bit segments

  1. The members of the bit segment can be int unsigned int signed int or char (belonging to the integer family) type
  2. The space of the bit segment is opened up in 4 bytes (int) or 1 byte (char) as needed.
  3. Bit segments involve many uncertain factors. Bit segments are not cross-platform. Programs that focus on portability should avoid using bit segments.

#for example

struct S {
    
    
 char a:3;
 char b:4;
 char c:5;
 char d:4;
};
struct S s = {
    
    0};
s.a = 10; 
s.b = 12;
s.c=3;
s.d=4;

How is the structure space in the above code opened up?
The allocation is shown in the following figure:
a, b, c, d are allocated four 3, 4, 5, 4 bits each, 8 bits represent a byte, so a and b are allocated in one byte, c and d Each occupies one byte.
At the same time, there is a truncation problem in the bit segment. For example, in the above structure, a has only 3 bits allocated, but sa=10, and the binary number of 10 is 1010; therefore, the first 1 will be truncated to 010; then combined with b Became 01100010.
Insert picture description here

3. Cross-platform issues of position

  1. It is uncertain whether the int bit field is treated as a signed number or an unsigned number.
  2. The number of largest bits in the bit segment cannot be determined. (The 16-bit machine has a maximum of 16, and the 32-bit machine has a maximum of 32. Write it as 27,
    which will cause problems on a 16-bit machine .
  3. The members of the bit segment are allocated from left to right in the memory, or the criteria for allocation from right to left have not been defined.
  4. When a structure contains two bit segments, and the member of the second bit segment is relatively large and cannot fit in the remaining bits of the first bit segment
    , it is uncertain whether to discard the remaining bits or use them.

to sum up

The above are some discussions on structure memory and bit segment issues in C language. Thanks for watching and welcome to correct me!

Guess you like

Origin blog.csdn.net/weixin_47460769/article/details/112770433