2

我是结构对齐和打包的新手。我以为我明白了,但我发现了一些我没想到的结果(见下文)。

我对结构对齐的理解是:

  1. 类型通常在其大小的倍数的内存地址上对齐。

  2. 根据需要添加填充以促进正确对齐

  3. 结构的末尾必须填充到最大元素的倍数(以方便数组访问)

#pragma pack指令基本上允许覆盖基于类型大小对齐的一般约定:

#pragma pack(push, 8)
struct SPack8
{
  // Assume short int is 2 bytes, double is 8 bytes, and int is 4 bytes
  short int a;
  double b;
  short int c;
  int d;
};
#pragma pack(pop)

Pseudo struct layout: What I expected:
// note: PADDING IS BRACKETED
0, 1, [2, 3, 4, 5, 6, 7] // a occupies address 0, 1
8, 9, 10, 11, 12, 13, 14, 15, // b occupies 8-15 inclusive
16, 17, [18, 19, 20, 21, 22, 23] // c occupies 16-17 inclusive
24, 25, 26, 27 // d occupies 24-27 inclusive
// Thus far, SPack8 is 28 bytes, but the structure must be a multiple of
// sizeof(double) so we need to add padding to make it 32 bytes
[28, 29, 30, 31]

令我惊讶的是,在 VS 2015 x86 上 sizeof(SPack8) == 24。似乎 d 没有在 8 字节地址上对齐:

offsetof(SPack, a) // 0, as expected
offsetof(SPack, b) // 8, as expected
offsetof(Spack, c) // 16, as expected
offsetof(SPack, d) // 20..what??

有人可以解释发生了什么/我误解了什么吗?

谢谢!

4

1 回答 1

5

您的误解是,它#pragma pack可以让您扩大结构,但事实并非如此。pack如果需要,允许您更紧密地打包结构。告诉编译器#pragma pack(push, 8),它最多可以8 字节边界上对齐,但不能更多

例子:

#pragma pack(push, 2)
struct X {
    char a; // 1 byte
    // 1 byte padding
    int b; // 4 bytes, note though that it's aligned on 2 bytes, not 4.
    char c, d, e; // 3 bytes
    //1 byte padding
}; // == 10 bytes, the whole struct is also aligned on 2 bytes, not 4
#pragma pack(pop)

// The same struct without the pragma pack:
struct Y {
    char a; // 1 byte
    // 3 bytes padding
    int b; // 4 bytes
    char c, d, e; // 3 bytes
    // 1 byte padding
};

这就是这样pack做的,使用编译器通常使用的更少的填充。在您的示例中,您尝试对齐int8 字节边界,但由于您允许编译器最多对齐 8 个字节,因此编译器想要使用的 4 字节对齐很好。大小为 24 的整个结构的大小也为 8 的倍数(您的最大成员),因此无需填充即可将其填充到 32。

您可以强制对齐结构

__declspec(align(32)) struct Z {
    char a;
    int b;
    char c, d, e;
};

甚至是你的结构的成员

struct SPack8
{
  // Assume short int is 2 bytes, double is 8 bytes, and int is 4 bytes
  short int a;
  double b;
  short int c;
  __declspec(align(8)) int d;
};

在特定的边界上,但我看不出有理由强制 4 字节类型在 8 字节上对齐。

于 2016-09-07T02:01:43.647 回答