C中最严格的类型是什么意思?

What is meant by the most restrictive type in C?

The C Programming Language一书在第 8.7 节中讨论了 "the most restrictive type",Example — A Storage Allocator

Although machines vary, for each machine there is a most restrictive type: if the most restrictive type can be stored at a particular address, all other types may be also. On some machines, the most restrictive type is a double; on others, int or long suffices.

在他们的代码中,union header 使用类型 long 对齐。

最严格的类型是什么意思?它可能是最大的类型(例如 double),还是有其他方法?

我认为引号是指最严格的类型对齐方式。例如,如果遵循此逻辑,char 是破坏性最小的类型。 char 类型的对象不对其对齐施加约束,而例如 int 类型的对齐要求通常为 4 字节边界。因此 int 是比 char.

更具限制性的类型

CPUs 通常要求(或者工作效率更高)某些类型的数据存储在某个(2 的幂)值的倍数的地址处。这个值被称为数据的 alignment。例如,CPU 可能要求四字节整数存储在四的倍数的地址(它们具有 四字节对齐,或者 对齐四个字节).

最严格的类型是指在这方面具有最严格要求的类型。所以如果例如long double 在某些机器上需要八字节对齐,并且没有其他类型需要比这更大的对齐,那么该机器上限制最严格的类型将是 long double.

满足最严格类型对齐要求的malloc(3)到return地址是有意义的,因为这意味着returned内存可用于存储任何类型. malloc()不知道内存会怎么用,所以无法适配

较大的数据类型不一定需要更大的对齐,但对齐要求往往会随着大小的增加而增加。

(某些类型的数据可能需要比 malloc() 提供的更大的对齐。例如,许多 x86 SSE 指令使用按 16 字节对齐的数据向量,而例如 malloc() 在 glibc 中仅保证八字节对齐。posix_memalign(3) 可用于动态分配内存,在 POSIX (*nix) 系统上具有更高的对齐要求。)

最严格的类型由max_align_t定义,它在stddef.h中定义。按照标准:

A fundamental alignment is represented by an alignment less than or equal to the greatest alignment supported by the implementation in all contexts, which is equal to _Alignof (max_align_t).

因此 max_align_t 的对齐方式 至少 与每个标量类型的对齐方式一样大,并且在大多数实现中,它的对齐方式将等于最大的标量类型 - 但这种相等性 不是 标准所要求的。

标准进一步要求(强调我的):

The order and contiguity of storage allocated by successive calls to the aligned_alloc, calloc, malloc, and realloc functions is unspecified. The pointer returned if the allocation succeeds is suitably aligned so that it may be assigned to a pointer to any type of object with a fundamental alignment requirement and then used to access such an object or an array of such objects in the space allocated (until the space is explicitly deallocated).

因此,分配函数返回的任何指针的对齐方式至少max_align_t.

的对齐方式一样严格