Go数组的索引是什么?

huangapple go评论88阅读模式
英文:

What are Go arrays indexed by?

问题

我是一些“内存分配器”类型的代码,使用数组和索引而不是指针。我希望数组的索引大小比指针小。我关心这个问题,因为我将“指针”存储为整数索引,而不是64位指针。

我在Go规范中没有看到关于数组索引的说明。显然,它是某种整数类型。传递非常大的值会导致运行时报错,提示我不能传递负数,所以我猜测它可能被转换为有符号整数。那么它是int32吗?我猜测它不是int64,因为我没有触及最高位(对于负数来说,它将是2的补码)。

英文:

I am some 'memory allocator' type code, by using an array and indexes rather than pointers. I'm hoping that the size of the index of the array is smaller than a pointer. I care because I am storing 'pointers' as integer indexes in an array rather than 64-bit pointers.

I can't see anything in the Go spec that says what an array is indexed by. Obviously it's some kind of integer. Passing very large values makes the runtime complain that I can't pass negative numbers, so I'm guessing that it's somehow cast to a signed integer. So is it an int32? I'm guessing it's not an int64 because I didn't touch the top bit (which would have been 2's compliment for a negative number).

答案1

得分: 4

数组可以由任何整数类型索引。

《Go编程语言规范》中的数组类型部分指出,在数组类型定义中,

> 长度是数组类型的一部分,必须是一个常量表达式,其值为非负整数。

在索引表达式(如a[x])中:

> x必须是一个整数值,且0 <= x < len(a)

但是索引的大小有限制;长度和容量的描述中说:

> 内置函数lencap接受各种类型的参数,并返回一个int类型的结果。实现保证结果始终适合int类型。

因此,数组的声明大小或索引表达式中的索引可以是任何整数类型intuintuintptrint8int16int32int64uint8uint16uint32uint64),但必须是非负的,并且在int类型的范围内(与int32int64大小相同,尽管它是一个不同的类型)。

英文:

Arrays may be indexed by any integer type.

The Array types section of the Go Programming Language Specification says that in an array type definition,

> The length is part of the array's type and must be a constant
> expression that evaluates to a non-negative integer value.

In an index expression such as a[x]:

> x must be an integer value and 0 <= x < len(a)

But there is a limitation on the magnitude of an index; the description of Length and capacity says:

> The built-in functions len and cap take arguments of various types and
> return a result of type int. The implementation guarantees that the
> result always fits into an int.

So the declared size of an array, or the index in an index expression, can be of any integer type (int, uint, uintptr, int8, int16, int32, int64, uint8, uint16, uint32, uint64), but it must be non-negative and within the range of type int (which is the same size as either int32 or int64 -- though it's a distinct type from either).

答案2

得分: 2

这确实是一个非常有趣的问题。我在文档中也没有找到任何直接的规则;相反,我在群组中找到了两个很好的讨论。

第一个讨论中,除了其他很多内容,我找到了一个回答为什么索引被实现为int而不是uint的答案:

> 算法可以从能够表示负偏移等方面受益。如果索引是无符号的,那么在这些情况下你总是需要进行转换。

第二个讨论专门讨论了使用int64来处理大数组的可能性(仅仅是可能性!),并提到了lencap函数的限制(这些限制实际上在文档中已经提到了):

> 内置函数len和cap接受各种类型的参数,并返回int类型的结果。实现保证结果始终适合int类型。

尽管如此,我同意更...官方的观点也不会有坏处。)

英文:

It's a very interesting question indeed. I have not found any direct rules in documentation too; instead I've found two great discussions in Groups.

In the first one, among many things, I've found an answer why indexes are implemented as int - but not uint:

> Algorithms can benefit from the ability to express negative offsets
> and such. If indexes were unsigned you'd always need a conversion in
> these cases.

The second one specifically talks about possibility (but possibility only!) of using int64 for large arrays, mentioning limitations of len and cap functions (which limitations are actually mentioned in the doc):

> The built-in functions len and cap take arguments of various types and
> return a result of type int. The implementation guarantees that the
> result always fits into an int.

I do agree, though, that more... official point of view wouldn't hurt. )

答案3

得分: 1

数组和切片是通过int进行索引的。int被定义为32位或64位的有符号整数。目前最常见的实现(6g)无论在哪种架构下都使用32位整数。然而,计划最终在64位机器上将int设为64位,因此与指针的长度相同。

语言规范定义了3种与实现相关的数值类型:

  • uint:32位或64位
  • int:与uint相同大小
  • uintptr:足够大以存储指针值的未解释位的无符号整数
英文:

Arrays and slices are indexed by ints. An int is defined as being a 32 or 64 bit signed integer. The most common implementation (6g) uses 32 bit integers regardless of the architecture at this point in time. However, it is planed that eventually an int will be 64bit on 64bit machines and therefore the same length as a pointer.

The language spec defines 3 implementation dependent numeric types:

uint     either 32 or 64 bits
int      same size as uint
uintptr  an unsigned integer large enough to store the uninterpreted bits of a pointer value

huangapple
  • 本文由 发表于 2012年7月4日 04:36:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/11318802.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定