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 C++ 头文件/宏冲突问题解决
- ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
- ¥50 安卓adb backup备份子用户应用数据失败
- ¥20 有人能用聚类分析帮我分析一下文本内容嘛
- ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
- ¥30 python代码,帮调试
- ¥15 #MATLAB仿真#车辆换道路径规划
- ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
- ¥15 数据可视化Python
- ¥15 要给毕业设计添加扫码登录的功能!!有偿