栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Go中切片的最大长度

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Go中切片的最大长度

根据文档,

The elements can be addressed by integer indices 0 throughlen(s)-1
。这意味着切片的最大容量是目标版本上默认整数的大小。

编辑:从源代码看,似乎有一个安全检查,以确保这种大小的切片是完全可能的:

func makeslice(t *slicetype, len64 int64, cap64 int64) sliceStruct {    // NOTE: The len > MaxMem/elemsize check here is not strictly necessary,    // but it produces a 'len out of range' error instead of a 'cap out of range' error    // when someone does make([]T, bignumber). 'cap out of range' is true too,    // but since the cap is only being supplied implicitly, saying len is clearer.    // See issue 4085.    len := int(len64)    if len64 < 0 || int64(len) != len64 || t.elem.size > 0 && uintptr(len) > maxmem/uintptr(t.elem.size) {        panic(errorString("makeslice: len out of range"))    }

因此,在这种情况下,看起来

uintptr(len) > maxmem/uintptr(t.elem.size)
我们不允许这样做。

但是,当我分配

struct{}
不占用内存的空间时,将允许该大小:

func main(){    r := make([]struct{}, math.MaxInt64)    fmt.Println(len(r))}// prints 9223372036854775807


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/406449.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号