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 solve the problem of memory alignment calculation in C++ structure

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

Share

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

This article will explain in detail how to solve the problem of memory alignment calculation in C++ structure. The content of the article is of high quality, so the editor shares it for you as a reference. I hope you will have some understanding of the relevant knowledge after reading this article.

Compilation environment: vs2015

Alignment principle:

Principle 1: data member alignment rule: the data member of the structure (struct) (or union). The first data member is placed where the offset is 0, and then each data member is aligned according to the value specified by # pragma pack and the length of the data member, the smaller one.

Principle 2: the overall alignment rule of the structure (or union): after the data members have completed their respective alignment, the structure (or union) itself should be aligned according to the value specified by # pragma pack and the smaller of the length of the most big data member of the structure (or union).

Principle 3: structure as a member: if there are some structure members in a structure, the structure member should be stored at an address multiple of the maximum element size within it.

Default alignment value:

Linux default # pragma pack (4)

Window default # pragma pack (8)

Note: you can change this factor by precompiling the command # pragma pack (n), nasty 1, 2, 4, 8, 16, where n is the specified "alignment factor".

Example 1: one-byte alignment

Step 1: member data alignment

# pragma pack (1) struct AA {int a; / / length 4

< 1 按1对齐;偏移量为0;存放位置区间[0,3] char b; //长度1 = 1 按1对齐;偏移量为4;存放位置区间[4] short c; //长度2 >

1 is aligned by 1; the offset is 5; the storage location interval is char d; / / the length 1 = 1 is aligned by 1; the offset is 6; the storage location interval [7] / / as a whole is stored in the location interval [0,7], with a total of eight bytes. }; # pragma pack ()

Step 2: overall alignment

Global alignment factor = min ((max (int,short,char), 1) = 1, so global alignment is no longer required. The overall size is 8.

The figure is as follows:

Example 2: two-byte alignment

Step 1: member data alignment

# pragma pack (2) struct AA {int a; / length 4 > 2 is aligned by 2; offset is 0; storage location range [0Magne3] char b; / / length 1

< 2 按1对齐;偏移量为4;存放位置区间[4] short c; //长度2 = 2 按2对齐;偏移量要提升到2的倍数6;存放位置区间[6,7] char d; //长度1 < 2 按1对齐;偏移量为7;存放位置区间[8];共九个字节};#pragma pack() 第二步: 整体对齐 整体对齐系数 = min((max(int,short,char), 2) = 2,将9提升到2的倍数,则为10.所以最终结果为10个字节。 图示如下:(X为补齐部分) 例三:四字节对齐 第一步: 成员数据对齐 #pragma pack(4)struct AA { int a; //长度4 = 4 按4对齐;偏移量为0;存放位置区间[0,3] char b; //长度1 < 4 按1对齐;偏移量为4;存放位置区间[4] short c; //长度2 < 4 按2对齐;偏移量要提升到2的倍数6;存放位置区间[6,7] char d; //长度1 < 4 按1对齐;偏移量为7;存放位置区间[8];总大小为9};#pragma pack() 第二步: 整体对齐 整体对齐系数 = min((max(int,short,char), 4) = 4,将9提升到4的倍数,则为12.所以最终结果为12个字节。 图示如下:(X为补齐部分)

Example 3: octet alignment

Step 1: member data alignment

# pragma pack (8) struct AA {int a; / length 4 < 8 is aligned by 4; offset is 0; storage location interval [0Power3] char b; / / length 1 < 8 is aligned by 1; offset is 4; storage location interval [4] short c; / / length 2 < 8 is aligned by 2; offset is raised to a multiple of 2; storage location interval [6] char d; / length 1 < 8 is aligned by 1; offset is 7 Storage location interval [8] with a total size of 9}; # pragma pack ()

Step 2: overall alignment

The overall alignment factor = min ((max (int,short,char), 8) = 4, if you raise 9 to a multiple of 4, it is 12. So the final result is 12 bytes. The picture is shown above.

Note: you can view the offset of the corresponding structural elements through the offsetof macro in the stddef.h library.

Example 4: the operation of a structure containing a structure

The overall calculation process is as follows

Struct EE {int a; / length 4 < 8 is aligned by 4; offset is 0; storage location interval [0re3] char b; / / length 1 < 8 is aligned by 1; offset is 4; storage location interval [4] short c; / / length 2 < 8 is aligned by 2; offset is increased from 5 to 6 The maximum element inside the structure is int. Since the offset is 8 which happens to be an integral multiple of 4, the next struct FF struct FF {int A1 is stored from 8; / the length 4 < 8 is aligned by 4; the offset is 8; the storage location interval is [8] char b1; / the length 1 < 8 is aligned by 1; the offset is 12. Storage location interval [12] short C1; / / length 2 < 8 is aligned by 2; offset is 13, rising to a multiple of 2 14; storage location interval [14] char D1; / / length 1 < 8 is aligned with 1; offset is 16 Storage location interval [16]}; / / overall alignment factor = min ((max (int,short,char), 8) = 4, complete memory size from 17 to integer multiple of 4 20 char d; / / length 1 < 8 is aligned by 1; offset is 21; storage location interval [21] / / overall alignment factor = min ((max (int,short,char), 8) = 4, complete memory size from 21 to 4 integer multiple 24}

The figure is as follows:

Example 5: the calculation of a nested structure

The overall calculation process is as follows

Struct B {char e [2]; / length 1 < 8 is aligned by 2; offset is 0; storage location interval [0 int 1] short h; / / length 2 < 8 is aligned by 2; offset is 2; storage location interval [2jue 3] / / the largest element inside the structure is double, offset is 4, up to 8, so store the next struct A struct A {int a from 8; / / length 4 < 8 press 4 alignment Offset is 8; storage location interval [8 max 11] double b; / / length 8 = 8 aligned by 8; offset 12, raised to 16; storage location interval 16 float c; / / length 4 < 8, aligned by 4; offset 24, storage location interval [24]}; / / overall alignment factor = min ((max (int,double,float), 8) = 8, make up the memory size from 28 to 8 by 32}

The figure is as follows:

Summary: when the n value of # pragma pack equals or exceeds the length of all data members, the size of this n value will have no effect.

On how to solve the C++ structure memory alignment calculation problem is shared here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

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