将使得普通int 64位打破了很多合理的code的? [英] Would making plain int 64-bit break a lot of reasonable code?
问题描述
直到最近,我会考虑大多数系统实现者/供应商的决定,以保持纯 INT
32位甚至64位的机器某种权宜之计疣。随着现代C99固定大小的类型( int32_t
和 uint32_t的
等)需要那里是一个标准的整数每个大小为8,16,32和64型大多消失,好像 INT
也可以同样进行64位。
Until recently, I'd considered the decision by most systems implementors/vendors to keep plain int
32-bit even on 64-bit machines a sort of expedient wart. With modern C99 fixed-size types (int32_t
and uint32_t
, etc.) the need for there to be a standard integer type of each size 8, 16, 32, and 64 mostly disappears, and it seems like int
could just as well be made 64-bit.
不过,平原 INT
规模最大的实际后果用C来自一个事实,即Ç基本上不会对算术更小的超 - INT
类型。特别是,如果 INT
大于32位,在 uint32_t的
值的算术结果已键入<$大C $ C>符号int ,这是相当令人不安。
However, the biggest real consequence of the size of plain int
in C comes from the fact that C essentially does not have arithmetic on smaller-than-int
types. In particular, if int
is larger than 32-bit, the result of any arithmetic on uint32_t
values has type signed int
, which is rather unsettling.
这是一个很好的理由让 INT
永久固定在32位对现实世界的实现?我倾向于说是的。在我看来,像有可能是一个巨大的类的用途uint32_t的
该休息的时候 INT
大于32位。即使在应用一元减或按位补运算符,除非你投回到 uint32_t的
变得危险。
Is this a good reason to keep int
permanently fixed at 32-bit on real-world implementations? I'm leaning towards saying yes. It seems to me like there could be a huge class of uses of uint32_t
which break when int
is larger than 32 bits. Even applying the unary minus or bitwise complement operator becomes dangerous unless you cast back to uint32_t
.
当然,同样的问题也适用于 uint16_t
和 uint8_t有
当前实现,但每个人似乎都知道并用于把它们当作小于- - INT
。类型
Of course the same issues apply to uint16_t
and uint8_t
on current implementations, but everyone seems to be aware of and used to treating them as "smaller-than-int
" types.
推荐答案
正如你所说,我认为促销规则真的是凶手。 uint32_t的
然后将提升到 INT
键,突然间你已签署的算术几乎人人都预期未签名。
As you say, I think that the promotion rules really are the killer. uint32_t
would then promote to int
and all of a sudden you'd have signed arithmetic where almost everybody expects unsigned.
这将大多隐匿在你做的只是算术和分配回一个 uint32_t的
的地方。但它可能是在你做对比常量的地方是致命的。无论code,它依赖于这样的比较没有做明确的投是合理的,我不知道。铸造像常数(uint32_t的)1
可能会变得非常乏味。我个人至少总是用后缀 U
对,我想是无符号常量,但是这已经是不为可读,因为我想。
This would be mostly hidden in places where you do just arithmetic and assign back to an uint32_t
. But it could be deadly in places where you do comparison to constants. Whether code that relies on such comparisons without doing an explicit cast is reasonable, I don't know. Casting constants like (uint32_t)1
can become quite tedious. I personally at least always use the suffix U
for constants that I want to be unsigned, but this already is not as readable as I would like.
也有记住, uint32_t的
等不能保证存在。甚至没有 uint8_t有
。的,执法是POSIX的延伸。因此,在这个意义上说C作为一门语言是远远不能做出这一举动。
Also have in mind that uint32_t
etc are not guaranteed to exist. Not even uint8_t
. The enforcement of that is an extension from POSIX. So in that sense C as a language is far from being able to make that move.
这篇关于将使得普通int 64位打破了很多合理的code的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!