C中最严格的类型是什么意思? [英] What is meant by the most restrictive type in C?

查看:19
本文介绍了C中最严格的类型是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

The C Programming Language 一书在第 8.7 节中谈到了最严格的类型",示例 —存储分配器:

<块引用>

虽然机器各不相同,但对于每台机器都有一个最严格的限制类型:如果最严格的类型可以存储在特定的地址,所有其他类型也可以.在某些机器上,最限制类型是 double;在其他情况下,intlong 就足够了.

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

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

解决方案

CPU 通常需要(或者如果可以更有效地工作)某些类型的数据存储在某个(二的幂)值的倍数的地址中.该值称为数据的对齐.例如,CPU 可能要求四字节整数存储在四的倍数的地址处(它们具有四字节对齐,或者在四字节上对齐>).

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

malloc(3) 返回满足最严格类型对齐要求的地址是有意义的,因为这意味着返回的内存可用于存储任何类型.malloc() 不知道内存将如何使用,因此无法适应.

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

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

The book The C Programming Language talks about "the most restrictive type" in section 8.7, 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.

In their code, the union header is aligned using the type long.

What is meant by the most restrictive type? Is it perhaps the largest type (e.g., double), or is there another approach?

解决方案

CPUs often require that (or work more efficiently if) certain types of data are stored at addresses that are a multiple of some (power-of-two) value. This value is called the alignment of the data. For example, a CPU might require that four-byte integers are stored at addresses that are a multiple of four (that they have four-byte alignment, or are aligned on four bytes).

By the most restrictive type, they mean the type that has the most restrictive requirements in this area. So if e.g. long double requires eight-byte alignment on some machine, and no other type requires greater alignment than that, then the most restrictive type on that machine would be long double.

It makes sense for malloc(3) to return addresses that satisfy the alignment requirements of the most restrictive type, since that means the returned memory can be used to store any type. malloc() doesn't know how the memory will be used, so it can't adapt.

It's not necessarily the case that larger data types require greater alignment, though alignment requirements tend to increase with increasing size.

(Some types of data might require even greater alignment than malloc() provides. For example, many x86 SSE instructions use data vectors that are aligned on 16 bytes, while e.g. the malloc() in glibc only guarantees eight-byte alignment. posix_memalign(3) can be used to dynamically allocate memory with even greater alignment requirements on POSIX (*nix) systems.)

这篇关于C中最严格的类型是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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