In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces "what are the reasons for the need for byte alignment". In the daily operation, I believe that many people have doubts about the reasons for the need for byte alignment. The editor consulted all kinds of data and sorted out simple and easy-to-use methods of operation. I hope it will be helpful for you to answer the question of "what are the reasons for byte alignment?" Next, please follow the editor to study!
Byte alignment
The memory of modern computing is divided into bytes, and in theory, the computer can access any variable from any address. But in practice, when computers access specific types of variables, they often access from specific memory addresses, which requires various types of data to be arranged in space according to certain rules, which is called memory alignment.
Reasons why byte alignment is required
The computer accesses the memory through the bus, and the width of the bus is generally 32-bit or 64-bit, assuming that it is a 32-bit bus. In each bus cycle, the computer will access 32-bit memory data from the even address. If a 32-bit data is not stored in a 4-byte divisible memory address, then the processor needs 2 bus cycles to access it, which will naturally reduce the efficiency of access. Therefore, in order for CPU to access the data quickly, the starting address of the data should be "aligned". For example, the starting address of 4-byte data should be on the 4-byte boundary, that is, the starting address can be divisible by 4.
Classification of byte alignment
There are two types of byte alignment:
Structure alignment, structure alignment is the main object of byte alignment.
Stack alignment, local variables in the function also need to be aligned, generally aligned according to 4 bytes.
Structure byte alignment
The compiler allocates space for each member of the structure according to its natural boundary.
Members are stored in memory in the order in which they are declared.
The address of the first member is the same as that of the entire structure.
Four basic Concepts of structure alignment
1) the alignment value of the data type itself: 1 byte for char data, 2 bytes for int/ type data, 4 bytes for char float type, and 8 bytes for chart type. 2) the self-aligned value of a structure or class: the value that has the largest self-aligned value among its members. 3) specify the alignment value value when specifying the alignment value: # pragma pack (value). 4) valid alignment values of data members, structures, and classes: the smaller of the self-alignment value and the specified alignment value, that is, the valid alignment value = min {self-alignment value, the currently specified pack value}.
During the period, the valid value N is the value that is ultimately used to determine how the data is stored. Valid N means "align on N", that is, the "storage starting address% Number0" of the data. The data variables in the data structure are stored in the order in which they are defined. The starting address of the first data variable is the starting address of the data structure. The member variables of the structure should be aligned and stored, and the structure itself should be rounded according to its own effective alignment value (that is, the total length of the structure member variable is an integer multiple of the effective alignment value of the structure, in order to efficiently access the structure data).
Three basic criteria for structural alignment
1) the first address of a structure variable can be divisible by the size of its widest base type member; 2) the offset (offset) of each member of the structure from the first address of the structure is an integral multiple of the size of the member, and the compiler will add internal adding between the members if necessary. 3) the total size of the structure is an integral multiple of the widest base type member size of the structure, and the compiler will add the padding byte {trailing padding} after the last member if necessary.
Rule 1: when opening up space for a structure, the compiler first finds the widest basic data type in the structure, and then looks for the location where the memory address can be divisible by the basic data type as the first address of the structure. Use the size of this widest basic data type as the alignment modulus described above.
Article 2: before opening up space for a member of the structure, the compiler first checks whether the offset of the first address of the pre-opened space relative to the head address of the structure is an integral multiple of the size of the member, and if so, store the member, on the contrary, fill a certain byte between this member and the previous member to achieve the requirement of integer multiple, that is, move the first address of the pre-opened space back a few bytes.
Article 3: the total size of the structure includes fill bytes, and the last member must meet the third in addition to the above two, otherwise a few bytes must be filled at the end to meet the requirements of this article.
Data communication between different processors
Byte alignment can cause changes in the length of the message structure on different compilation platforms or processors.
The compiler may populate the message structure in order to align bytes, and different compilation platforms may be populated in different forms, greatly increasing the risk of data communication between processors.
In order to solve the above problems, you can do this as follows:
1) for locally used data structures, four-byte alignment is adopted to improve memory access efficiency; at the same time, in order to reduce memory overhead, reasonably arrange the position of structure members, reduce the gap between members caused by four-byte alignment, and reduce memory overhead. 2) for the data structure between processors, it is necessary to ensure that the message length will not change due to different compilation platforms or processors, and one-byte alignment is used to tighten the message structure. in order to ensure the memory access efficiency of the message data structure between processors, byte filling is used to align the four bytes of the members in the message. 3) the member location of the data structure should take into account the relationship between the members, data access efficiency and space utilization. The principle of order is: four bytes first, two bytes next to the last four byte member, one byte followed by the last two byte member, and filled bytes last.
Default byte alignment
32-bit machines generally default to 4-byte alignment (32-bit machine word length 4 bytes)
64-bit machines generally default to 8-byte alignment (64-bit machines are 8 bytes long).
Change the way bytes are aligned
Use the directive # pragma pack (n): the C compiler will align to n bytes
Use the directive # pragma pack (): cancel custom byte alignment.
Byte alignment with the value of sizeof
Different byte alignment may affect the value of sizeof. When using sizeof, authentication should consider the length of the current byte alignment.
When carrying out network communication, it is often necessary to transmit a piece of buffer. The length of this buffer must be specified, and this length must be used when parsing.
At this point, the study of "what are the reasons for byte alignment" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.