为什么要建的sizeof除了char型是依赖编译器在C&放大器; C ++? [英] Why sizeof built in types except char is compiler dependent in C & C++?

查看:235
本文介绍了为什么要建的sizeof除了char型是依赖编译器在C&放大器; C ++?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么C和C基本类型++没有严格的Java一样定义,其中一个 INT 始终是4字节和是8个字节,等等。要我在C和C ++的知识只有字符被定义为1字节,其余的都被不同的编译器有不同的定义。因此,一个 INT C和C ++不必一定是4个字节,只要它比长较长字符。结果
我只是想知道这是为什么呢?它起任何作用?

Why are fundamental types in C and C++ not strictly defined like in Java where an int is always 4 bytes and long is 8 bytes, etc. To my knowledge in C and C++ only a char is defined as 1 byte and everything else is defined differently by different compilers. So an int in C and C++ does not have to necessarily be 4 bytes as long as it's longer than a short and short is longer char.
I was just wondering why is that and does it serve any purpose?

推荐答案

究其原因主要是因为C是移植到更广泛的各种平台。原因有很多,为什么不同的数据类型已经被证明是他们在各种平台上的各种尺寸,但至少从历史上看, INT 已经适应了这个平台的原生字长。在PDP-11是16位(和最初发明了32位数字),而一些嵌入式平台编译器甚至有8位 INT 秒。当32位平台苏醒过来,并开始有32位 INT S,的发明重新present 16位数字。

The reason is largely because C is portable to a much wider variety of platforms. There are many reasons why the different data types have turned out to be the various sizes they are on various platforms, but at least historically, int has been adapted to be the platform's native word size. On the PDP-11 it was 16 bits (and long was originally invented for 32-bit numbers), while some embedded platform compilers even have 8-bit ints. When 32-bit platforms came around and started having 32-bit ints, short was invented to represent 16-bit numbers.

如今,大多数64位架构使用32位 INT 。只不过,使之与最初为32位平台编写的C程序的大型基地兼容,但也出现了64位C编译器与64位 INT 取值为好,其中最重要的一些早期克雷平台。

Nowadays, most 64-bit architectures use 32-bit ints simply to be compatible with the large base of C programs that were originally written for 32-bit platforms, but there have been 64-bit C compilers with 64-bit ints as well, not least of which some early Cray platforms.

此外,在计算的早期,浮点格式和大小一般都远低于标准(IEEE 754没有过来直到1985年),这就是为什么浮动双击是甚至低于整数数据类型定义良好的。他们一般不会甚至presume等特点为无穷大,非数字或符号零的presence。

Also, in the earlier days of computing, floating-point formats and sizes were generally far less standardized (IEEE 754 didn't come around until 1985), which is why floats and doubles are even less well-defined than the integer data types. They generally don't even presume the presence of such peculiarities as infinities, NaNs or signed zeroes.

此外,或许应该说是字符不是定义为1个字节,而是要成为什么的sizeof 为返回1。这不一定是8位。 (为了完整起见,它或许应该在这里添加,也说:字节作为一个术语是并非普遍定义为8位;已经有它的许多年前的定义,并且在ANSI C标准的上下文中,一个字节实际上是定义为能够存储一个字符,无论字符)

Furthermore, it should perhaps be said that a char is not defined to be 1 byte, but rather to be whatever sizeof returns 1 for. Which is not necessarily 8 bits. (For completeness, it should perhaps be added here, also, that "byte" as a term is not universally defined to be 8 bits; there have been many historical definitions of it, and in the context of the ANSI C standard, a "byte" is actually defined to be the smallest unit of storage that can store a char, whatever the nature of char.)

也有这种架构的36位PDP-10和18位PDP-7S的也运行C程序。他们可能是相当罕见的,这些天,但也有助于解释为什么C数据类型不以8位为单位来定义的。

There are also such architectures as the 36-bit PDP-10s and 18-bit PDP-7s that have also run C programs. They may be quite rare these days, but do help explain why C data types are not defined in terms of 8-bit units.

这是否,到最后,真正使语言更轻便而不是像Java语言也许可以辩论,但它肯定是次优在16位处理器上运行Java程序,相当怪异确实在36-位处理器。这也许是公平地说,它使的语言的更便携,但编写的程序在它的可移植性。

Whether this, in the end, really makes the language "more portable" than languages like Java can perhaps be debated, but it would sure be suboptimal to run Java programs on 16-bit processors, and quite weird indeed on 36-bit processors. It is perhaps fair to say that it makes the language more portable, but programs written in it less portable.

编辑:在回答一些评论,我只是想追加,作为舆论一片,那C作为一门语言是不同于像Java /哈斯克尔/ ADA语言是更多或-less由公司或标准组织拥有。有ANSI C,当然,但C比ANSI C以上;这是一个居住社区,并有许多实现是不兼容ANSI,但C不过。理由是使用8位 INT 取值实现无论是C类似争论是否是苏格兰英语,它的主要意义。他们使用8位整数有充分的理由,没有人谁知道Ç不够好,将无法推理这样的编译器编写的程序,和任何人谁对这种架构编写的C程序希望他们的 INT s到为8位。

In reply to some of the comments, I just want to append, as an opinion piece, that C as a language is unlike languages like Java/Haskell/ADA that are more-or-less "owned" by a corporation or standards body. There is ANSI C, sure, but C is more than ANSI C; it's a living community, and there are many implementations that aren't ANSI-compatible but are "C" nevertheless. Arguing whether implementations that use 8-bit ints are C is similar to arguing whether Scots is English in that it's mostly pointless. They use 8-bit ints for good reasons, noone who knows C well enough would be unable to reason about programs written for such compilers, and anyone who writes C programs for such architectures would want their ints to be 8 bits.

这篇关于为什么要建的sizeof除了char型是依赖编译器在C&放大器; C ++?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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