search
HomeCommon ProblemWill byte alignment affect the efficiency of memory access?

Since C projects are rarely done and relatively small, the issue of byte alignment has not been paid attention to. However, byte alignment should have a great impact on memory management and CPU execution efficiency in large-scale applications. This article is based on some information and makes a short summary.

Will byte alignment affect the efficiency of memory access?

#First of all, throw out the first conclusion, byte alignment can improve the execution efficiency of the CPU. (Recommended learning: PHPSTORM )

CPU execution instructions to obtain data from the memory from the memory, the size of the block may be 2-bytes, 4-bytes, 8 -bytes, 16-bytes......

At this time, if the starting address of the CPU reading data of 4-bytes or more is 1, it needs to read at least 2 data blocks, and then put 2 Unnecessary data in the block is discarded, and then the useful data is spliced ​​into 4-bytes data. This obviously increases the CPU operation and affects the execution efficiency of the CPU instructions. If the starting address of the data read by the CPU is at 0, 4..., then only one data block needs to be read at a time, and the CPU's read instruction is an atomic operation.

Byte alignment means that for every data read by the CPU, it ensures that its starting address is at the beginning of the data block, and expands the value where the number of data bytes is smaller than the CPU data block (granularity) , making it occupy a complete memory space of one granularity. Byte alignment saves the CPU from data interception and splicing operations.

Another conclusion is that byte alignment is helpful for optimizing memory.

In the structure, according to the byte automatic alignment principle, the structure

typedef struct _test {
char a;
int b;
char c;
} test;

The compiler will use the number of bytes of the basic type int with the largest number of bytes in the structure as According to the alignment standard, the char type will be expanded to 4-bytes, therefore, sizeof(test)=12, but this memory utilization efficiency is relatively low.

If you specify 1-byte alignment by the compiler, the execution efficiency of the CPU will be reduced.

#praama pack(1)
typedef struct _test {
char a;
int b;
char c;
} test;
#pragma pack()

In order to optimize the memory of the program while ensuring CPU execution efficiency, it is necessary to adjust the order of the data members in the structure

typedef struct _test {
int b;
char a;
char c;
} test;

At this time, the structure Member b occupies 4 bytes, a and c share the next 4 bytes, two of the members occupy the first two bytes, and the last two bytes are invalid data filled during byte alignment. This structure occupies 8 bytes of memory space.

The above is the detailed content of Will byte alignment affect the efficiency of memory access?. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Will R.E.P.O. Have Crossplay?
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Atom editor mac version download

Atom editor mac version download

The most popular open source editor