I
I
Inter Carpenter2016-09-24 02:10:49
go
Inter Carpenter, 2016-09-24 02:10:49

What is cut capacity?

Tell me what is the capacity of a slice in Go?
Let's say

slice := make([]int, 10, 15)
  fmt.Printf("len: %d, cap: %d\n", len(slice), cap(slice))
  fmt.Println(slice)

Displays
len: 10, cap: 15
[0 0 0 0 0 0 0 0 0 0]

And
slice := make([]int, 10, 30)
  fmt.Printf("len: %d, cap: %d\n", len(slice), cap(slice))
  fmt.Println(slice)

Outputs the same array [0 0 0 0 0 0 0 0 0 0]
I thought that this is the maximum possibility of writing to the array, but when I tried to write slice[11]=0
I got a panic =(
I read articles about slices but did not understand what it is capacity and what it is for

Answer the question

In order to leave comments, you need to log in

3 answer(s)
X
xotkot, 2016-09-24
@Byrger

if in a simple and short way, then for a slice:
capacity (cap) is the allocated memory for elements, if it is exceeded, the size is automatically doubled . length(len) is the initialized memory of elements, to exceed(add) you need to manually use append. By default cap = len There will always be cap >= len Roughly speaking, cap allocates memory, and len initializes all or only part of it.

slice := make([]int, 2, 2) // эвивалентно slice := []int{0,0}
fmt.Printf("len: %d, cap: %d\n", len(slice), cap(slice))
fmt.Println(slice)

slice = append(slice, 3)

fmt.Printf("len: %d, cap: %d\n", len(slice), cap(slice))
fmt.Println(slice)

will output:
len: 2, cap: 2
[0 0]

len: 3, cap: 4
[0 0 3]

ps
if specifically according to your example, then in the first and second cases you have only 10 elements initialized, which the fmt.Println(slice) command displays.
And the panic at slice[11]=0 will be due to the fact that this 11th element is not initialized, although the memory is allocated for it, it is necessary to use append here.

V
Vitaly Pukhov, 2016-09-24
@Neuroware

A slice is a data structure that describes the multiple division of an array and is stored separately from a variable. A slice is not an array. A slice describes a part of an array.
If we take the buffer array from the previous section, then we can create a slice that describes elements from 100 to 150 (to be precise, from 100 to 149 inclusive) by slicing the array:
var slice []byte = buffer[100:150 ]
In this piece of code, to be precise, we have used a full variable declaration. The variable slice is of type []byte, read as "slice of bytes" and is created from the buffer array by slicing from element 100 (inclusive) to element 150 (exclusive).
That is, in short, if I correctly understood the question, you must first create an array, a regular one, then you can select a slice inside it, and only after that you can work with the slice.

E
evnuh, 2016-09-24
@evnuh

https://blog.golang.org/slices read and everything will become clear.
In short, the capacity of a slice is the length of the array that stores the elements of the slice. Simply put, these are Bytes of RAM allocated for storing your data. It is always greater than or equal to the length of the slice.
The slice itself is a pointer to any of the elements in this array (not necessarily the first one) + length. This is an artificial limitation, in fact it is a segment of the array in which something is written. When a new element is added to the slice, the slice will increase its length by 1, indicating that the amount of data in the array has increased by 1, and the capacity of the array itself will not change if it was enough to store the new element. If the entire array was full, then the slice will simply allocate a new array in memory of a larger capacity (usually twice the previous capacity) and transfer all the data there along with the newly added element. In this case, obviously, the slice itself will not change its length, because The number of data has not changed, but will only change the pointer to the initial element, because The data array has been allocated a new one at a new address in memory.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question