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

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

问题描述

这本书的 C程序设计语言的关于最严格的类型,在8.7节的会谈,例— A存储分配器的:

The book The C Programming Language talks about "the most restrictive type" in section 8.7, Example — A Storage Allocator:

虽然机器有所不同,每台机器有一个最严格的
  输入:如果最严格的类型可存储在特定
  地址,所有其他类型可也。在一些机器上,最
  限制性类型是双击;对他人, INT 就足够了。

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.

在其code时,联盟 使用类型对齐

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?

推荐答案

的CPU通常需要(或更有效地工作,如果)某些类型的数据存储在属于某些倍数地址(乘方的二)值。这个值被称为的对齐的数据。例如,一个CPU可能需要四个字节的整数被存储在该是四的倍数(即它们具有地址的四字节对齐的,或者是上的四个字节对齐)。

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 be 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.

是有道理的的malloc(3)返回满足最严格的类型的对齐要求的地址,因为那意味着返回的内存可以用来存储任何类型。 的malloc()不知道内存将如何使用,所以它无法适应。

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.

(某些类型的数据可能需要比更大对齐的malloc()提供例如,许多86的 SSE说明使用是在16字节对齐的数据载体,而如的malloc()中唯一的glibc保证八字节对齐。 posix_memalign(3)可以用来动态地以更大的对齐要求对POSIX(* nix中)系统。)分配内存

(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天全站免登陆