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

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

问题描述

例如,有一种结构

 一个结构
{
所以char a;
INT I;
};

在这种情况下,我们有一个[1个字节] +填充[3个字节] + INT [4字节= 8。

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

 一个结构
{
INT I;
所以char a;
};

在这种情况下,焦炭int和无需添加填充字节来后,就意味着的sizeof(A)= 5个字节,但在这种情况下,我也得到了8个字节的结果。为什么呢?

好吧,什么这个案子

 的struct
   {
       INT B:
       双C;
       所以char a;
   };

根据下面给定的逻辑,有一个:大小= B [4字节] +填充[4字节] + C [8] +一[1] +填充[7字节到双对准] = 24
但执行后,我得到16这怎么可能?


解决方案

  

在这种情况下,字符来后 INT 键,无需添加填充字节,这意味着的sizeof(A)= 5 字节,但在这种情况下,我也得到了 8 字节的结果。为什么呢?


首先,你需要了解为什么需要填充?结果
维基说:


  

数据结构对准是数据被安排和在计算机存储器访问的方式。它由两个独立但相关的问题:数据对齐的和的数据结构填充的。 当现代计算机读取或写入到一个内存地址,它会在Word中做到这一点大小的块或(例如,4个字节的32位系统上的块)放大。数据对准装置将在一个存储器偏移量等于字的大小,从而增加了系统的性能的某个倍数的数据,由于CPU处理存储器的方式。向对齐的数据,可能有必要在最后一个数据结构的末尾和下一个的开始,这是数据结构填充之间插入一些无意义字节。


若要 4 ,第二个片段将与填充( INT 对齐)<大小多code> 3 字节。编译后的第二个片段将被填充适当调整为

 一个结构
{
    INT I;
    所以char a;
    炭填充[3]; // 3字节使结构的8个字节的总大小
};


编辑:始终记得结构填充两个黄金规则:


  • 当一个结构成员的填充只能插入其次大排列成员的要求,或在结构在结束即可。

  • 最后一个成员被填充以使得该结构的总大小应该是任何结构构件的最大对齐的倍数所需要的字节数。

在的情况下

 的struct
{
    INT B:
    双C;
    所以char a;
};

对齐都将作为

 的struct
{
    INT B: // 4字节。 b为随后用较大的对准的部件。
    炭Padding1 [4]; // 4字节的填充需要
    双C; // 8字节
    字符D组; // 1个字节。结构的最后一个成员。
    炭Padding2 [7]; // 7字节使结构24的总字节大小
};

还要注意的是,通过改变构件的排序中的结构,它是可以改变填充的保持对准所需的量。这可以通过,如果成员由降序排列排序的要求来完成。

 的struct
{
    双C; // 8字节
    INT B: // 4字节
    所以char a; // 1个字节。只有最后一个成员将被填充到大小给予16的结构
};

For example, there is a structure

struct A
{
char a;
int i;
};

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;
};

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 ?

Ok, and what about this case

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

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 ?

解决方案

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:

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.

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
};    


EDIT: Always remember the two golden rule 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.

In case of

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

alignment will take place as

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天全站免登陆