I was searching for memory allocations of slices whether static at compile time or dynamic on fly. Since slices are pointers to the array in back and I have studied that for dynamic memory allocation, pointers are crucial. So what is the allocation of slice and how about maps.
1条回答 默认 最新
- dongxilan9351 2018-04-11 11:20关注
type slice struct { array unsafe.Pointer len int cap int }
The slice descriptor is a
struct
.make([]type, len, cap)
returns aslice
descriptor with a pointer to an underlying array with the giventype
,len
(length), andcap
(capacity). The slice descriptor and/or its underlying array are allocated on the stack and/or the heap.type string struct { array unsafe.Pointer len int }
The string descriptor is a
struct
with a pointer to an underlyingbyte
array with lengthlen
. Sincestrings
are immutable, the capacity is redundant; it is always equal to the length. The string descriptor, from a literal or conversion, and/or its underlying array are allocated on the stack and/or the heap.Consider a string as a special form of a
byte
slice. For example, slice expressions apply to both.The Go Programming Language Specification
Slice expressions construct a substring or slice from a string, array, pointer to array, or slice. There are two variants: a simple form that specifies a low and high bound, and a full form that also specifies a bound on the capacity.
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报
悬赏问题
- ¥15 2024-五一综合模拟赛
- ¥15 如何将下列的“无限压缩存储器”设计出来
- ¥15 下图接收小电路,谁知道原理
- ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
- ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
- ¥15 手机接入宽带网线,如何释放宽带全部速度
- ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
- ¥15 ETLCloud 处理json多层级问题
- ¥15 matlab中使用gurobi时报错
- ¥15 这个主板怎么能扩出一两个sata口