Sem_t 结构中的信号量是否会填充以满足对齐要求?

huangapple go评论59阅读模式
英文:

Are sempahores (sem_t) inside C structs padded to respect alignment?

问题

I'm currently defining a struct in C (in Ubuntu x64). It looks like this:

#include <semaphore.h>
#include <stdio.h>
#include <stdbool.h>

typedef struct key{
    sem_t sem;
    char name[32];
    int val1;
    int val2;
    char k;
    int n;
} Key;

From what I know (correct me if wrong, please), struct members in x64 will align to 8 bytes and in x32 will align with 4 bytes. That's also the reason I chose 32 as the id array size.

What I wanted to know is: as it is (first member being a sem_t (32 bytes apparently), and next members being that or any other thing) will there be any padding between the first (sem) and the second member (name, in this case)? Or are they contiguous? If so, does this hold true for both x32 and x64 (as 32 bytes is a multiple of both 4 and 8 bytes)?

英文:

I'm currently defining a struct in C (in Ubuntu x64). It looks like this:

#include <semaphore.h>
#include <stdio.h>
#include <stdbool.h>

typedef struct key{
    sem_t sem;
    char name[32];
    int val1;
    int val2;
    char k;
    int n;
} Key;

From what I know (correct me if wrong, please), struct members in x64 will align to 8 bytes and in x32 will align with 4 bytes. That's also the reason I chose 32 as the id array size.

What I wanted to know is: as it is (first member being a sem_t (32 bytes apparently), and next members being that or any other thing) will there be any padding between the first (sem) and the second member (name, in this case)? Or are they contiguous? If so, does this hold true for both x32 and x64 (as 32 bytes is a multiple of both 4 and 8 bytes)?

答案1

得分: 2

只返回翻译好的部分:

  1. "It depends — on the implementation. One question you need to be sure you know the answer to: Why are you worried?"

    • "这取决于实现。 你需要确保你知道答案的一个问题是:你为什么担心?"
  2. "The size of a sem_t may vary between 32-bit and 64-bit implementations. In general, when N is a power of two, an N-byte object (or an array of N-byte objects) will be aligned on an N-byte boundary. The alignment for double doesn't always follow this rule. If you had any short (or uint16_t) data members, they would only need to be aligned on a 2-byte boundary."

    • "sem_t 的大小可能在 32 位和 64 位实现之间变化。 通常情况下,当 N 是 2 的幂时,一个 N 字节的对象(或 N 字节对象的数组)将在 N 字节边界上对齐。 double 的对齐不总是遵循这个规则。 如果你有任何 short(或 uint16_t)数据成员,它们只需要在 2 字节边界上对齐。"
  3. "A structure or union type will be aligned according to the alignment requirements of the member with the most stringent alignment requirements."

    • "结构或联合类型将根据具有最严格对齐要求的成员的要求进行对齐。"
  4. "There won't be any padding bytes between sem and name because character strings can be aligned on any boundary. There might be padding bytes between name and val1, but you've probably avoided that (in part because you chose the length 32 for name)."

    • "semname 之间不会有填充字节,因为字符字符串可以在任何边界上对齐。 nameval1 之间可能有填充字节,但你可能已经避免了这种情况(部分原因是因为你选择了 name 的长度为 32)。"
  5. "You will have padding between k and n; if you reverse their order, you'll (probably) only have trailing padding, but you will have trailing padding because of the single character. As written, you have interior padding (probably 3 bytes of it) and maybe no trailing padding."

    • "在 kn 之间会有填充;如果你反转它们的顺序,你(可能)只会有尾部填充,但因为单个字符,你会有尾部填充。 如所述,你有内部填充(可能是 3 字节),可能没有尾部填充。"
  6. "To investigate further, you'd need to use the offsetof macro from <stddef.h>, but be aware that it can only report on the current implementation, not other implementations."

    • "要进一步调查,你需要使用 <stddef.h> 中的 offsetof 宏,但要注意它只能报告当前实现,不能报告其他实现。"
英文:

It depends — on the implementation. One question you need to be sure you know the answer to: Why are you worried?

The size of a sem_t may vary between 32-bit and 64-bit implementations. In general, when N is a power of two, an N-byte object (or an array of N-byte objects) will be aligned on an N-byte boundary. The alignment for double doesn't always follow this rule. If you had any short (or uint16_t) data members, they would only need to be aligned on a 2-byte boundary<sup>*</sup>. A structure or union type will be aligned according to the alignment requirements of the member with the most stringent alignment requirements.

There won't be any padding bytes between sem and name because character strings can be aligned on any boundary. There might be padding bytes between name and val1, but you've probably avoided that (in part because you chose the length 32 for name).

You will have padding between k and n; if you reverse their order, you'll (probably) only have trailing padding, but you will have trailing padding because of the single character. As written, you have interior padding (probably 3 bytes of it) and maybe no trailing padding.

To investigate further, you'd need to use the offsetof macro from &lt;stddef.h&gt;, but be aware that it can only report on the current implementation, not other implementations.

<sup>* Technically, a short need not be 16 bits or 2 bytes, but it almost invariably is, and I'm assuming as much here.</sup>

huangapple
  • 本文由 发表于 2023年5月14日 11:18:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/76245663.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定