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

An example Analysis of memory alignment in C language structure

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article mainly explains the "example Analysis of memory alignment in C language structure". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought. Let's study and learn "an example analysis of the memory alignment of C language structure".

Let's first look at a structure:

Typedef struct Test

{

Char a1

Int a2

Char a3

Short a4

} Test_T

What is the number of bytes of this structure in a 32-bit compilation system? Is it 1 "4" 1 "2" 8 bytes? No, the actual result is 12 bytes. Why? Because the compiler will automatically complete less than 4 bytes of variable space to 4 bytes (this is memory alignment) to improve the efficiency of CPU addressing (32-bit CPU addressed in 4-byte steps).

Memory alignment is the "jurisdiction" of the compiler. The compiler arranges each "data unit" in the program in an appropriate location so that each "data unit" can be found quickly. For 32bit's CPU, the addressing step is 4 bytes (that is, unsigned int byte length), which is often referred to as "4-byte alignment". Similarly, for 64bit's CPU, there is "8-byte alignment". This paper takes 32-bit CPU as an example.

Take a look at the following code:

# include

Typedef struct Test

{

Char a1

Int a2

Char a3

Short a4

} Test_T

Int main (void)

{

Test_T T

Printf ("\ nsizeof (T) =% d\ n", sizeof (T))

Printf ("A1 address:% d\ n", (unsigned int) & T.a1)

Printf ("a2 address:% d\ n", (unsigned int) & T.a2)

Printf ("A3 address:% d\ n", (unsigned int) & T.a3)

Printf ("A4 address:% d\ n", (unsigned int) & T.a4)

Return 0

}

The running result is:

It can be seen that it just confirms the above statement that after completion, there is a difference of exactly 4 bytes between the addresses of the structural member a1Magi a2jina3, and the difference between A3 and A4 is also because there is an extra blank byte in it. The running result of the program can be vividly described as the following figure:

A1 occupies only one byte, leaving three blank bytes for memory alignment; A3 and A4 add up to 3 bytes, and one blank byte is reserved for memory alignment. This is what the compiler does to store variables so that its employer, CPU, can find them more quickly.

Thank you for your reading. the above is the content of "example Analysis of memory alignment in C language structure". After the study of this article, I believe you have a deeper understanding of the problem of memory alignment in C language structure. the specific use still needs to be verified by practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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

Internet Technology

Wechat

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

12
Report