Capacity Where can I find a good definition of capacity?


(Cherolyn Lexvold) #1

Where can I find a good definition of capacity?


(Jay Ts) #2

Hi Cherolyn,

Maybe here? https://en.wiktionary.org/wiki/capacity :grin:

Where did you encounter the term? I assume while studying slices.

Slices are made up of three things:

  1. A pointer to an element of an array. (Don’t worry about that just yet.)
    and two numbers:
  2. The length of the slice (how many elements in it), and
  3. The capacity of the slice. That’s how many elements it can hold. When it gets full, the slice needs to have more space added to it before it can hold more elements. (Actually, it’s the array that the slice points to that needs to be reallocated.)

You can access a slice’s length and capacity using Go’s built-in len() and cap() functions. In the following, notice how after I add things to the slice with append(), the capacity grows, always staying equal to or longer than the length.

package main

import "fmt"

func main() {
    var int_slice []int     // a slice of integers

    // add 3 integers to the slice
    int_slice = append(int_slice,32)
    int_slice = append(int_slice,67)
    int_slice = append(int_slice,99)

    fmt.Println(int_slice)
    fmt.Printf("Length: %d\n",len(int_slice))
    fmt.Printf("Capacity: %d\n",cap(int_slice))

    // add 2 more integers
    int_slice = append(int_slice,22)
    int_slice = append(int_slice,11)
    
    fmt.Println(int_slice)
    fmt.Printf("Length: %d\n",len(int_slice))
    fmt.Printf("Capacity: %d\n",cap(int_slice))
}

https://play.golang.org/p/asm1a_MgKv-

The len() and cap() functions can also be applied to other things. See https://golang.org/ref/spec#Length_and_capacity

If that didn’t answer your question, please ask a more specific question.


(Cherolyn Lexvold) #3

Lol.That’s not the definition I was looking for.
Yes, iI came across it in a discussion about slices.
From your discussion, I believe I’m getting a feel for capacity, but it’s not concrete. I understand length.
“The capacity of a slice is the number of elements for which there is space allocated in the underlying array”. This is getting close. I have to look at arrays again.
An array is a numbered sequence of elements of a single type
This helps.
I think a lot of it is getting used to the language, in which my understanding has begun to develope


(Johan Dahl) #4

Think of a slice as a bookshelf. The length is the number of currently stored books and the capacity is the remaining space. For example if your capacity is exceeded (your try to put in more books than it can hold, with appending for example) will go buy a new bookshelf twice as large. And move all your books to the new one. Each book is one element in the slice and it is numbered from 0 and upwards. Does this make sense?


(Cherolyn Lexvold) #5

That really helped


(Cherolyn Lexvold) #6

I looked at your explanation again, and now I understand it a bit better. I tried copying and pasting what’s in the yellow to the Go Playground, and this is what I got.

https://play.golang.org/p/Zjd47JkfGhG

What did I do wrong?


(Johan Dahl) #7

https://play.golang.org/p/oLVnZ3tGYpT

You pasted your program in the middle so you got two main functions and some extra things. I changed just that and now it works.


(Cherolyn Lexvold) #8

Yes!


(Cherolyn Lexvold) #9

So is length determined by the amount of elements I create in the slice? And is capacity space that the computer allocates for what I have created


(Johan Dahl) #10

Yes. That is correct!


(Cherolyn Lexvold) #11

Cool! Thanks! It’s exciting to reach an understanding!:bulb:


(Inanc Gumus) #12

If you use a slice literal, the length and the capacity will be the same. But, if you use make, the capacity and the length can be different.

Several things determine the length.

What’s the length here?

s := make([]byte, 0, 100)

// It's zero, but the capacity is 100.

How about now?

s = s[:5]

// Now, the length is 5, but the capacity is still 100.

How about now?

s = append(s[:100], 42)

// Now the length is 101, but the capacity is 208.
// Because, there wasn't enough space, so a new underlying array is allocated.

In summary:

  • The capacity is the length of the underlying array (after where the slice starts (slice’s pointer)).
  • Remember, a slice is just a view into the underlying array.
  • Its length is determined by which segment of the underlying array that it points.
  • And yes: “capacity space that the computer allocates -for the underlying array

(Cherolyn Lexvold) #13

This is very clear and helpful, and I will save it.


(Taalhach) #14

Lets start with simple daily life example
Think about a bottle of milk that can store 1000 ml milk and currently there is 700 ml milk in that bottle
Total storage for milk is capacity which is 1000 ml and currently how much milk in that bottle is length which is 700 ml.
lets switch our example
we have a slice of ints with length 700 and capacity of 1000

a:=make([]int, 700,1000)

remember one thing this slice is dynamic


(Inanc Gumus) #15

You’re welcome :wink:


(Cherolyn Lexvold) #16

Good one. Dynamic?


(Taalhach) #17

yep capacity of slice is dynamic which gets double when length increases the capacity


(Cherolyn Lexvold) #18

dy·nam·ic
/dīˈnamik/
adjective

(of a process or system) characterized by constant change, activity, or progress.

capacity of slice is dynamic which gets double when length increases the capacity

Got it! Cool! Thanks!