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

Example Analysis of structure and memory alignment in C language

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

Share

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

This article mainly shows you the "C language structure and memory alignment example analysis", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and study the "C language structure and memory alignment example analysis" this article.

I. citing examples

What exactly is structure memory alignment? let's use a piece of code to introduce it.

Struct S1 {char c1Tracer 1 byte int a transponder 4 byte char c2 Tracer 1 byte}; int main () {printf ("% d\ n", sizeof (struct S1)); / / print 12} here

Let's first explain S1. There are two char types and one int type in structure S1. According to reason, it should account for 2 "1" 4 "6 bytes, ah, why print 12? At this point, we have to understand the rules of memory alignment in the structure:

1. The first member of the structure is always placed at the position where the offset of the starting position of the structure is 0.

You can understand the offset as follows: the offset of the array with subscript 0 relative to itself is 0, and the offset of subscript 1 relative to subscript 0 is 1.

Examples are as follows:

The first member of S1 is C1, which will be placed at the position where the offset of the starting position of the structure is 0, as shown in the red figure below.

two。 Starting with the second member, it is always placed at an integer with an offset of an alignment, which is the smaller value of the compiler's default alignment and the size of the variable itself.

Alignment = min (compiler default alignment, variable size)

There is no alignment in Linux-. The default is 8 in VS.

We still take S1 as an example. The second member of the structure is an of type int, accounting for 4 bytes. Under the author's VS environment, the default alignment number is 8, and the smaller value of the two is 4, then a should be placed on a multiple of 4 offset.

If you put it in a multiple of 4, that is to say, you can put it here with an offset of 4, and the three spaces with an offset of 1 and 2 will be wasted. An int occupies 4 bytes, so it takes up to the position where the offset is 7.

Then there is the third member of the structure. The c2 c2 of char type occupies 1 byte. In VS environment, the default alignment number is 8, and the smaller value is 1, that is, as long as the offset is a multiple of 1, we can put it immediately after a, that is, the position of offset 8.

Then all three members of the structure have been used up here, and there are only eight. Why is the print 12? Here is the third rule of memory alignment in the structure.

3. The total size of the structure must be an integral multiple of the largest alignment of each member.

From the previous explanation, we know that the alignment number of the three members of the structure, c1memery, c2, is respectively 1, 4, and the maximum alignment of these three is 4, and the total size should be an integral multiple of 4. At that time, some friends must ask: aren't we aligned to 8 now? isn't 8 a multiple of 4? Be careful! What we're talking about here is the total size of the space, and 8 is the so-called offset, which is calculated from 0 to 8, so we have 9 spaces here, so we have to go to 12, that is, the offset to 11.

(the three spaces added later are not needed, but because the regulations are still included in the total space of the structure)

Second, try the bull's knife

Let's take a look at a similar topic.

The code is as follows (example):

Struct S2 {char c1pact 1-byte char c2-pact; 1-byte int a; 4-byte}; int main () {printf ("% d\ n", sizeof (struct S2)); / / print 8} here

First of all, the first structure member is the c1 of type char, which is directly placed at the offset of 0 by Rule 1.

(the gray part of the picture)

The second member is c2 of char type, accounting for 1 byte. Under VS, the default alignment is 8, and the smaller value is 1, as long as it is placed on a multiple of 1 (any position), followed by 0, at the offset of 1 (red).

The last member, an of int type, accounts for 4 bytes. In VS environment, the default alignment number is 8. The smaller 4 is placed at an integer multiple of 4, that is, 4 here, and then int occupies 4 bytes all the way to offset 7.

Let's take a look at rule 3, the total size of the structure must be an integral multiple of the largest alignment of each member, that is, a multiple of 4, and we now take up exactly 8 spaces, and 8 is exactly a multiple of 4, so we don't have to waste space any more and print out 8.

3. Special cases of nested structures

The code is as follows (example):

Struct S3 {double dash double occupies 8 bytes, default alignment is 8, take a smaller value, alignment number 8 char / alignment 1 int I bank / alignment 4}; struct S4 {char c1; struct S3 S3; double d;}; int main () {printf ("% d\ n", sizeof (struct S4));}

As for the structure S3, we can use the same method as the previous S1 and S2 to calculate that it takes up 16 bytes of space. We focus on S4 here, and the partners who are interested in S3 can solve it on their own.

The first member C1 in S4 is placed directly at offset 0 according to rule 1. What about the second member S3? Here is the fourth rule of memory alignment in the structure:

If the structure is nested, the nested structure is aligned to an integer multiple of its maximum alignment, and the overall size of the structure is an integer multiple of all the maximum alignment (including the alignment of the nested structure)

The maximum alignment of the three members of the S3 structure is 8, that is, to the multiple of the offset of 8, and then S3 accounts for 16 bytes, so it accounts for 23 of the offset (the alignment of the S3 structure is the largest of the three members of the S3 structure).

Ps: in VS environment, the maximum alignment number of nested structures is more than 8, and 8 is still used as the maximum alignment number (larger than the default alignment number, a smaller value will take the default alignment number)

The last member of S4 double type d occupies 8 bytes, the default alignment number is 8, the alignment number is 8, and then it is placed at an integral multiple of the offset to the alignment number, just down at 24, which accounts for 31% of the 8 bytes itself.

The offset 0-31 accounts for 32 bytes, and the alignment number of the member c1grad in S4 is 1jing8, so the maximum alignment is 8, 32 is a multiple of 8, so there is no need to waste space to satisfy the rule that "the total size of the structure must be an integer multiple of the largest alignment of each member", the total size of the structure is 32.

Fourth, about why there is memory alignment 1. Platform reasons (migration reasons):

Not all hardware platforms can access any data on any address; some hardware platforms can only fetch certain types of data at certain addresses, otherwise a hardware exception is thrown

two。 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 accesses only once.

Generally speaking: the memory alignment of structures is to trade space for time.

The above is all the contents of the article "example Analysis of structure and memory alignment in C language". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

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