不同机器上 C 数据类型的大小sizeof(复数长双精度)
有人知道在不同机器上比较 C 数据类型大小的网站或论文吗?我对一些“大型”机器的价值感兴趣,例如 System z 等。
和: 任何机器上最大的本机数据类型是否存在字节上限,并且它始终是complex long double
类型?
编辑:我不确定,但是 SIMD 寄存器数据是否也利用了 CPU 的缓存?将存储在特殊单元中且不使用 L1/L2/L 缓存的数据类型不符合我的兴趣。仅检查类型 {char、short、int、long、long long、float、double、long double、_Bool、void *}(以及 _Complex)。
does anybody know a website or a paper where the sizes of C data types were compared on different machines? I'm interested in values of some 'big' machines like a System z or the like.
And:
Is there an upper bound of bytes that the biggest native datatype on any machine can have and is it always of the type complex long double
?
Edit: I'm not sure about but does the SIMD register data also take advantage of the CPU's cache? Data types that will be stored in a special unit and do not use the L1/L2/L cache are out of my interesst. Only the types {char, short, int, long, long long, float, double, long double, _Bool, void *} (and with _Complex) will be examined.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
C 数据类型大小不依赖于机器平台。这取决于编译器的实现。同一硬件平台上的两个不同编译器可能会以不同方式实现基本类型,从而导致完全不同的大小。
您还应该考虑到这样一个事实,即
size_t
等标准类型不能保证由用户可访问的类型(例如unsigned int
)表示。size_t
可以通过未知大小和范围的特定于实现的无符号类型来实现,这是非常合法的并且可能的。此外,理论上(并且迂腐地)C 语言在大小方面没有“最大”类型。 C 语言规范绝对不保证基本类型的相对大小。 C 语言仅保证每种类型可表示值的相对范围。例如,语言保证
int
的范围不小于short
的范围。但是,由于[几乎]任何类型都可以包含任意数量的填充位,因此理论上short
类型的对象大小可能大于int
类型的对象大小。当然,这将是一种非常奇特的情况。但实际上,您可以预期
long long int
是最大的整型类型,而long double
是最大的浮点类型。如果您愿意,您还可以将复杂类型纳入考虑范围。The C data type size does not depend on the machine platform. It depends on the compiler implementation. Two different compilers on the same hardware platform might implement basic types differently, resulting in completely different sizes.
You should also take into account the fact that such standard types as
size_t
are not guaranteed to be represented by user-accessible types likeunsigned int
. It is quite legal and possible thatsize_t
might be implemented through an implementation-specific unsigned type of unknown size and range.Also, theoretically (and pedantically) C language has no "biggest" type in terms of size. C language specification makes absolutely no guarantees about the relative sizes of the basic types. C language only makes guarantees about the relative ranges of the representable values of each type. For example, the language guarantees that the range of
int
is not smaller than the range ofshort
. However, since [almost] any type can contain an arbitrary amount of padding bits, theoretically the object size of typeshort
might be greater than that of typeint
. This would be, of course, a very exotic situation.In practice though, you can expect that
long long int
is the biggest integral type andlong double
is the biggest floating point type. You can also include the complex types into the consideration, if you wish to do so.您提到“本机”数据类型,但请注意,
complex
不是由 C 规范定义的,因此不是本机类型。 C 的本机类型为char
、int
、float
、double
和void
>。数据类型的大小通常由底层平台和编译器决定。 C 标准定义了这些类型的最小范围,并定义了一些相对关系(
long int
必须至少与常规int
一样长,等等)。如果不进行测试,就没有简单的方法可以确定任何类型的绝对大小。当使用新平台并且我不知道特定的数据类型大小时,我编写了一个简短的应用程序,用于转储所有标准 C 类型的
sizeof
结果。还有像stdint.h
这样的标头,可以为您提供可以信任的特定大小的数据类型。数据类型的大小没有上限。 C 标准将
char
定义为“足够大以存储执行字符集的任何成员”。这将本机类型的大小部分绑定到机器体系结构,因此执行字符大小高达 1MB 的理论机器的sizeof(char)
等于 1MB。实际上,您可能找不到出现这种情况的机器。You mention "native" datatypes, but note that
complex
is not defined by the C spec and thus isn't a native type. The native types for C arechar
,int
,float
,double
, andvoid
.The size of a data type is generally determined by the underlying platform as well as the compiler. The C standard defines the minimum range for these types and defines a few relative relationships (
long int
must be at least as long as a regularint
, etc). There's no easy way to determine the absolute size of any type without testing it.When working with a new platform and I don't know the particular data type sizes, I write up a short app that dumps the result of
sizeof
for all the standard C types. There are also headers likestdint.h
that give you data types that you can trust to be a certain size.There is no upper bound as to the size of a data type. The C standard defines
char
to be "large enough to store any member of the execution character set". This partially binds the size of the native types to the machine architecture, and so a theoretical machine that had execution characters up to 1MB in size would havesizeof(char)
equal to 1MB. Practically speaking, you probably won't find a machine where this is the case.本机类型的棘手之处在于某些体系结构和编译器可能会扩展它们。例如,大多数针对现代 Intel 硬件的编译器都提供 __m128 数据类型,它是 SIMD 寄存器的宽度。 AVX 将具有 256 位 SIMD 宽度,Larrabee 512 位,并且每个在编译器中都有相应的本机类型。
The tricky thing about native types is that some architectures and compilers may extend them. For example, most compilers targeting modern Intel hardware offer a
__m128
datatype, which is the width of a SIMD register. AVX will have a 256-bit SIMD width, Larrabee 512bit, and each has a corresponding native type in the compiler.x86 机器上的
long double
类型为 80 位(10 字节),http: //en.wikipedia.org/wiki/Long_double,尽管现在这或多或少是一种过时的形式。 x64 不支持。The
long double
type on x86 machines is 80 bits (10 bytes), http://en.wikipedia.org/wiki/Long_double, although this is an more-or-less obsolete form now. x64 doesn't support it.