为什么size_t无符号? [英] Why is size_t unsigned?

查看:180
本文介绍了为什么size_t无符号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Bjarne Stroustrup在C ++编程语言中写道:

Bjarne Stroustrup wrote in The C++ Programming Language:


无符号整数类型适合将存储视为
位数组。使用无符号而不是int来获得更多的位到
表示正整数几乎从来不是一个好主意。尝试
确保一些值为正,通过声明变量unsigned
通常会被隐式转换规则所取消。

The unsigned integer types are ideal for uses that treat storage as a bit array. Using an unsigned instead of an int to gain one more bit to represent positive integers is almost never a good idea. Attempts to ensure that some values are positive by declaring variables unsigned will typically be defeated by the implicit conversion rules.

size_t 似乎是无符号的,以再获得一位来表示正整数。

size_t seems to be unsigned "to gain one more bit to represent positive integers". So was this a mistake (or trade-off), and if so, should we minimize use of it in our own code?

Scott Meyers的另一篇相关文章是

Another relevant article by Scott Meyers is here. To summarize, he recommends not using unsigned in interfaces, regardless of whether the value is always positive or not. In other words, even if negative values make no sense, you shouldn't necessarily use unsigned.

推荐答案

在具有16位指针的架构上,例如small

On an architecture with 16 bit pointers, such as the "small" model DOS programming, it would be impractical to limit strings to 32 KB.

因为这个原因,C标准需要(通过必要的范围) ptrdiff_t ,签名对应的 size_t 和指针差异的结果类型,有效地为17位。

For this reason, the C standard requires (via required ranges) ptrdiff_t, the signed counterpart to size_t and the result type of pointer difference, to be effectively 17 bits.

这些原因仍然适用于嵌入式编程世界的一部分。

Those reasons can still apply in parts of the embedded programming world.

但是,它们不适用于现代的32位或64位编程,其中一个更重要的考虑是,当它们用于数字(因此,算术运算和幅度比较)时,C和C ++的不幸的隐式转换规则使无符号类型成为错误吸引子。有了20-20事后,我们现在可以看到,采用这些特定转换规则的决定, string(Hi).length() -3 实际上保证,是相当愚蠢和不切实际。然而,该决定意味着在现代编程中,对数字采用无符号类型有严重的缺点,没有优势–除了满足那些发现 unsigned 为自描述类型名称的人的感觉,并且无法想到 typedef int MyType

However, they do not apply to modern 32-bit or 64-bit programming, where a much more important consideration is that the unfortunate implicit conversion rules of C and C++ make unsigned types into bug attractors, when they're used for numbers (and hence, arithmetical operations and magnitude comparisions). With 20-20 hindsight we can now see that the decision to adopt those particular conversion rules, where e.g. string( "Hi" ).length() < -3 is practically guaranteed, was rather silly and impractical. However, that decision means that in modern programming, adopting unsigned types for numbers has severe disadvantages and no advantages – except for satisfying the feelings of those who find unsigned to be a self-descriptive type name, and fail to think of typedef int MyType.

总结起来,这不是一个错误。这是一个决定,然后非常理性,实际规划的原因。它与将期望从边界检查的语言(如Pascal到C ++)(这是一个谬误,但是非常常见的一个,即使有些人从未听说过Pascal)无关。

Summing up, it was not a mistake. It was a decision for then very rational, practical programming reasons. It had nothing to do with transferring expectations from bounds-checked languages like Pascal to C++ (which is a fallacy, but a very very common one, even if some of those who do it have never heard of Pascal).

这篇关于为什么size_t无符号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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