Capacity Where can I find a good definition of capacity?

Where can I find a good definition of capacity?

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.

4 Likes

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

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?

3 Likes

That really helped

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?

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.

1 Like

Yes!

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

Yes. That is correct!

1 Like

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

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

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

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

You’re welcome :wink:

1 Like

Good one. Dynamic?

1 Like

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

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!

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.