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

How to understand C++ memory alignment

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly explains "how to understand C++ memory alignment". The explanation in this article is simple and clear, easy to learn and understand. Please follow the ideas of Xiaobian and go deep into it slowly to study and learn "how to understand C++ memory alignment" together.

1. Why memory alignment?

To access misaligned memory, the processor has to access it twice (the data is read high first and then high again), to access aligned memory, the processor only has to access it once, in order to improve the efficiency of the processor reading data, we use memory alignment. Windows default alignment is 8 bytes, Linux default alignment is 4 bytes.

Memory alignment is also used for platform reasons: not all hardware platforms can access arbitrary data at specific addresses, and some platforms can only access fetch data at specific addresses or throw exceptions.

Second, the memory alignment principle:

In memory, the compiler allocates memory separately for each struct variable according to the list of members, leaving extra memory space between members when alignment requirements are required in stored procedures. If you want to know how much storage space a struct takes up, use the keyword sizeof to see the size, and if you want to know where a particular member of the struct is located in the struct, use offsetof(header file stddef.h)

The first data member of a struct or union is placed at a cheap 0, and each subsequent data member is placed at an offset that is an integer multiple of its alignment. (Alignment is the smaller of the variable size and default alignment.) The struct size must be an integer multiple of the maximum alignment.

3. What are the rules for byte alignment of structs?

In general, we assume that the starting position of the structure is 0x0000,N is the set n-byte alignment, and the formula 0x0000%N==0 is satisfied to determine the location of the structure member storage.

1. If the default alignment rule is used, the following steps need to be performed:

(1) A member of a structure has its own pair value N (the space occupied by the data type, for example, 4 bytes for int type on a 32-bit machine, and its own pair value is 4), which needs to be determined with the storage location. If "start position %N=0" is satisfied (N must also satisfy>= member self alignment value except for the first member of the structure), store the member at that start position. If the previous member has a certain distance from the storage location of the current member, the distance is regarded as the filling space.

(2) After all the members of the structure have been allocated storage locations, the structure itself must also be aligned. The structure itself must also satisfy "(M+X)%S=0" for alignment, where M+X is the storage space occupied by the entire structure, M is the sum of the storage space sizes of all structure members, X is the padding space size to satisfy an integer multiple of S, and S is the structure member with the largest self-alignment value.

If the #pragma pack(N) rule is used, the following steps need to be performed:

(1) The self-alignment value of a structure member is the amount of offset. If "start position %N=0" is satisfied (where N is the specified alignment value, i.e.#pragma pack(N)), then the member can be stored in the start position with the offset of the member's self-alignment value (for example, int type self-alignment value is 4, occupying 4 bytes of memory location).

(2) The alignment value of the structure itself is the largest of all the alignment values of the members of the structure, but because the valid alignment value of the structure (i.e., the specified alignment value) is N,"(M+X)%N=0",M+X is the size of the space stored in the structure.

Why does my compiler leave holes in the structure? This results in wasted space and the inability to read and write "binary" to external data files. Can you turn off padding or control how domains are aligned?

When values in memory are properly aligned, many machines can access them very efficiently. For example, in a byte-addressed machine, a 2-byte short int must be placed at an even address, while a 4-byte long int must be placed at an integer multiple of 4. Some machines cannot even access unaligned addresses at all, so all data must be properly aligned.

6. What is a bit field?

Bit field refers to the information stored, and does not need to occupy a complete byte, but only a few or one binary bit. For example, when storing a switching value, there are only two states of 0 and 1, and one bit binary can be used. In order to save storage space and make processing simple, C language also provides a data structure called "bit field" or "bit field." A bit field is the division of bits in a byte into different regions and the number of bits in each region. Each domain has a domain name that allows you to operate by domain name in your program. This allows several different objects to be represented in a single byte binary bit field.

Thank you for reading, the above is "C++ memory alignment how to understand" the content, after the study of this article, I believe that everyone on C++ memory alignment how to understand this problem has a deeper experience, the specific use of the situation also needs to be verified by practice. Here is, Xiaobian will push more articles related to knowledge points for everyone, welcome to pay attention!

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

Development

Wechat

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

12
Report