为什么len()返回一个有符号值?

编程入门 行业动态 更新时间:2024-10-21 10:19:24
本文介绍了为什么len()返回一个有符号值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

Go的内置 len()函数

a>返回一个签名的 int 。为什么不是使用 uint 来代替?

是否有可能 len( )返回负值? 据我所知,答案是否定的:

  • 数组:元素的数量称为长度,永远不会为负数。
  • 切片:在任何时候,以下关系都成立: 0 <= len(s)<= cap(s)
  • 地图地图元素的数量称为其长度。 (我在规范中找不到任何明确将其限制为非负值的任何内容,但我很难理解地图中可能少于0个元素的情况)。
  • Strings 字符串值是一个(可能是空的)字节序列....可以使用内置函数 len()来发现字符串s(它的大小以字节为单位)(同样,很难看出序列的负数字节数)
  • 频道在频道缓冲区中排队的元素数量(同上)

解决方案

len() cap())返回 int code> uint )。所以问题更多的是为什么Go使用带符号整数来为没有负面影响的slice /数组编制索引e索引?。

答案很简单:对于 compute 索引是很常见的,如果完成了这些计算,在无符号整数。一些无辜的代码如 i:= a-b + 7 可能会产生 i == 4294967291 6和10的c $ c> a 和 b 。这样的索引可能会溢出你的片段。大量的索引计算发生在0左右,使用无符号整数很难得到正确的结果,这些错误隐藏在数学上完全合理和合理的公式中。这是既不安全也不方便的。

这是一个基于经验的折衷:下溢往往会发生在使用unsigned int进行索引计算的情况下,整数用于索引计算。

另外:在这些情况下使用无符号整数基本上没有任何好处。

Go's builtin len() function returns a signed int. Why wasn't a uint used instead?

Is it ever possible for len() to return something negative? As far as I can tell, the answer is no:

  • Arrays: "The number of elements is called the length and is never negative."
  • Slices: "At any time the following relationship holds: 0 <= len(s) <= cap(s)"
  • Maps "The number of map elements is called its length". (I couldn't find anything in the spec that explicitly restricts this to a nonnegative value, but it's difficult for me to understand how there could be fewer than 0 elements in a map)
  • Strings "A string value is a (possibly empty) sequence of bytes.... The length of a string s (its size in bytes) can be discovered using the built-in function len()" (Again, hard to see how a sequence could have a negative number of bytes)
  • Channels "number of elements queued in channel buffer (ditto)

解决方案

len() (and cap()) return int because that is what is used to index slices and arrays (not uint). So the question is more "Why does Go use signed integers to index slices/arrays when there are no negative indices?".

The answer is simple: It is common to compute an index and such computations tend to underflow much too easy if done in unsigned integers. Some innocent code like i := a-b+7 might yield i == 4294967291 for innocent values for aand b of 6 and 10. Such an index will probably overflow your slice. Lots of index calculations happen around 0 and are tricky to get right using unsigned integers and these bugs hide behind mathematically totally sensible and sound formulas. This is neither safe nor convenient.

This is a tradeoff based on experience: Underflow tends to happen often for index calculations done with unsigned ints while overflow is much less common if signed integers are used for index calculations.

Additionally: There is basically zero benefit from using unsigned integers in these cases.

更多推荐

为什么len()返回一个有符号值?

本文发布于:2023-10-31 06:09:08,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1545242.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:符号   len

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!