如果字符在 int 之后,为什么要添加填充? [英] Why padding are added, if char comes after int?

查看:15
本文介绍了如果字符在 int 之后,为什么要添加填充?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

比如有一个结构

struct A
{
char a;
int i;
};

在这种情况下,我们有 [1 byte] + padding[3 byte] + int[4 byte] = 8.

In this case, we have a[1 byte] + padding[3 byte] + int[4 byte] = 8.

现在让我们对上面的结构做一点更新,

Now let's make little update into struct above,

struct A
{
int i;
char a;
};

在这种情况下,char 在 int 之后,不需要添加填充字节,这意味着 sizeof(A) = 5 字节,但在这种情况下,我也得到了 8 字节的结果.为什么?

In this case char comes after int and no need to add padding bytes, it means sizeof(A) = 5 byte, but in this case I also get the 8 byte result. Why ?

好的,那么这个案子呢

struct s
   {
       int b;
       double c;
       char a;
   };

根据下面给出的逻辑,有一个:size = b[4 bytes] + padding[4 bytes] + c[8] + a[1] + padding[7 bytes to align with double] = 24,但执行后我得到 16.这怎么可能?

According logic given below, there is a: size = b[4 bytes] + padding[4 bytes] + c[8] + a[1] + padding[7 bytes to align with double] = 24, but after execution I get 16. How this is possible ?

推荐答案

在这种情况下charint之后并且不需要添加填充字节,它意味着sizeof(A) = 5字节,但是在这种情况下,我也得到 8 字节结果.为什么 ?

In this case char comes after int and no need to add padding bytes, it means sizeof(A) = 5 byte, but in this case I also get the 8 byte result. Why ?

首先你需要明白为什么需要填充?
维基说:

First you need to understand why padding is needed?
Wiki says that:

数据结构对齐是数据在计算机内存中排列和访问的方式.它由两个独立但相关的问题组成:数据对齐数据结构填充.当现代计算机读取或写入内存地址时,它将以字大小的块(例如 32 位系统上的 4 字节块)或更大的块执行此操作.数据对齐意味着将数据放在等于字大小的某个倍数的内存偏移处,由于 CPU 处理内存的方式,这会提高系统性能.为了对齐数据,可能需要在最后一个数据结构的末尾和下一个数据结构的开始之间插入一些无意义的字节,这就是数据结构填充.

Data structure alignment is the way data is arranged and accessed in computer memory. It consists of two separate but related issues: data alignment and data structure padding. When a modern computer reads from or writes to a memory address, it will do this in word sized chunks (e.g. 4 byte chunks on a 32-bit system) or larger. Data alignment means putting the data at a memory offset equal to some multiple of the word size, which increases the system's performance due to the way the CPU handles memory. To align the data, it may be necessary to insert some meaningless bytes between the end of the last data structure and the start of the next, which is data structure padding.

为了使 4 的大小倍数(int 的对齐),第二个代码段将填充 3 字节.编译后,第二个代码段将被填充以正确对齐

To make the size multiple of 4 (alignment of int) , the second snippet will be padded with 3 bytes. After compilation the second snippet will be padded for proper alignment as

struct A
{
    int i;
    char a; 
    char Padding[3]; // 3 bytes to make total size of the structure 8 bytes
};    

<小时>

永远记住这两个结构填充的黄金法则:


Always remember these two golden rules of structure padding:

  • 填充仅在结构成员跟随具有更大对齐要求的成员或在结构的末尾时插入.
  • 最后一个成员填充了所需的字节数,以便结构的总大小应该是任何结构成员的最大对齐的倍数.
  • Padding is only inserted when a structure member is followed by a member with a larger alignment requirement or at the end of the structure.
  • The last member is padded with the number of bytes required so that the total size of the structure should be a multiple of the largest alignment of any structure member.

如果

struct s
{
    int b;
    double c;
    char a;
};  

对齐将发生在

struct s
{
    int b;             // 4 bytes. b is followed by a member with larger alignment.
    char Padding1[4];  // 4 bytes of padding is needed 
    double c;          // 8 bytes
    char d;            // 1 byte. Last member of struct. 
    char Padding2[7];  // 7 bytes to make total size of the structure 24 bytes 
};   

另请注意,通过更改结构中成员的顺序,可以更改保持对齐所需的填充量.这可以通过成员是否按降序排列要求进行排序来完成.

Also note that by changing the ordering of members in a structure, it is possible to change the amount of padding required to maintain alignment. This can be done by if members are sorted by descending alignment requirements.

struct s
{ 
    double c;   // 8 bytes
    int b;      // 4 bytes 
    char a;     // 1 byte. Only last member will be padded to give structure of size 16 
};   

这篇关于如果字符在 int 之后,为什么要添加填充?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆