为什么一个结构中的内存对齐的变化? [英] Why does the memory alignment change within a structure?

查看:147
本文介绍了为什么一个结构中的内存对齐的变化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在<一个href="http://stackoverflow.com/questions/27841898/why-does-my-structure-element-carry-padding-bytes">$p$pvious问题,我了解到,当有8字节对齐的结构被嵌入到其他结构与4-字节对齐,填充需要的 8字节对齐的结构之前。

明白了。

至少我认为我的理解。

借助 VS 2012文档说:

  

有关结构,工会和数组,对齐,要求其成员中最大的对准要求。

所以,如果我有一个结构是这样的:

  typedef结构s_inner {
  无符号长UL1;
  双DBL1;
  fourth_struct S4;
  无符号长UL2;
  INT I1;
} t_inner;
 

我期望的此结构的所有成员都将是8字节对齐,因为 8字节对齐。

但我的内存转储显示我:

t_inner 从地址开始 1B8

  • 1B8 :在无符号长是因为填充结构为8字节对齐
  • 1C0 :在占用8个字节
  • 1C8 fourth_struct 如下,它有4个字节对齐

到现在为止,一切都如预期。但是,现在的 t_inner 对齐开关: 在地址 1E8 ,我希望找到刚无符号长这里,补齐4字节,因此,以下 INT 也排列在8个字节。但它好像对准现在因为改变unsigned long类型并的没有的进行填充字节。相反,下面的 INT 放置了4字节对齐。

为什么在 t_inner 对齐开关?其中的规则在这里应用?

解决方案
  

我期望这个结构的所有成员都将是自8字节对齐   双是8字节>对齐。

哦,不。每个键入的都有自己的定位的的结构体,而的结构本身的有定位即是其中的最大的它的内容比对。

 对齐。 typedef结构s_inner {
      无符号长UL1; // 4对齐
      双DBL1; // 8对齐(需要4填充在此之前)
      fourth_struct S4; // 4对齐尺寸32
      无符号长UL2; // 4对齐(无填充)
      INT I1; // 4对齐(无填充)
      //无需为结构填充为8字节的整数倍
    } t_inner;
 

该结构本身具有的,因为双对齐8。

In a previous question, I learned that when a structure that has 8-byte alignment gets embedded into another structure with 4-byte alignment, a padding is needed before the 8-byte aligned structure.

Understood.

At least I thought that I understood.

The VS 2012 docs say:

For structures, unions, and arrays, the alignment-requirement is the largest alignment-requirement of its members.

So, if I have a structure like this:

typedef struct s_inner {
  unsigned long ul1;
  double        dbl1;
  fourth_struct s4;
  unsigned long ul2;
  int           i1;
} t_inner;

I'd expect all members of this structure would be 8-byte aligned since the double is 8-byte aligned.

But my memory dump shows me this:

t_inner starts from address 1B8:

  • 1B8: the unsigned long is padded because the structure is 8-byte aligned
  • 1C0: The double consumes 8 bytes
  • 1C8: fourth_struct follows, it has 4-byte alignment

Until now, everything is as expected. But now the alignment switches inside t_inner: On address 1E8, I'd expect to find just the unsigned long here, padded with 4 bytes so that the following int is also aligned on 8 bytes. But it seems as if the alignment has now changed since the unsigned long does not carry padding bytes. Instead, the following int is placed with a 4-byte alignment.

Why does the alignment switch inside t_inner? Which rule is applied here?

解决方案

I'd expect all members of this structure would be 8-byte aligned since the double is 8-byte > aligned.

Well, no. Each type has its own alignment inside the struct, and the struct itself has an alignment that is the greatest of the alignments of it's content.

aligned.    typedef struct s_inner {
      unsigned long ul1;        // 4-aligned
      double        dbl1;       // 8-aligned  (need 4 padding before this)
      fourth_struct s4;         // 4 aligned  size 32
      unsigned long ul2;        // 4 aligned  (no padding)
      int           i1;         // 4 aligned  (no padding)
      // no padding needed as struct is a multiple of 8 bytes
    } t_inner; 

The struct itself has align 8 because of the double.

这篇关于为什么一个结构中的内存对齐的变化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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