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 aslicedescriptor 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
structwith a pointer to an underlyingbytearray with lengthlen. Sincestringsare 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
byteslice. 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.
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报