You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
One of the main differences between a nil and an empty slice regards allocations. Initializing a nil slice doesn’t require any allocation, which isn’t the case for an empty slice.
Correct me if I am wrong, but AFAIK both slices will have no memory allocations.
Slices are represented in memory as 3 words, defined by the slice header:
The empty slice itself doesn't need memory allocation, but when Initializing it, we also need to initialize the slice header, which contains a pointer(array) that needs memory allocations. zerobase is the base address for all 0-byte allocations, but itself is not 0.
Item 22 says:
Correct me if I am wrong, but AFAIK both slices will have no memory allocations.
Slices are represented in memory as 3 words, defined by the slice header:
A
nil
slice is actually a zero value of the slice header:An empty slice is just a slice with
len
andcap
equal to 0 and anarray
pointing to 'zero-sized' array:The value of the array is the address of the
runtime.zerobase
, the base address for all 0-byte allocationsThe text was updated successfully, but these errors were encountered: