Balga in his lecture, I'm going to talk about the mechanics of the app and function in more detail.
I'll let this get started.
All right, let's continue using the example from the previous lecture to keep things simpler this time
I have only created a slice with two elements instead of three.
Here's a question.
What would happen if you appoint one more element to this slice?
OK, let me add one more element to the ages slice.
Notice that it's backing is already full.
I mean, there are two items in the area, but it needs a room for one more element.
So when this happens, the append function needs to allocate a new and a larger array on the memory
like this is because arrays are fixed in that sort of apan function, cannot grow the existing array.
So it needs to allocate an entirely new array.
This is also because allocating a block of memory as fixed blocks are more efficient.
So after allocating the new era, the apparent function creates and returns a new Selous header.
Notice that the previous header points to the purple area, but the newsless header points to the greenery.
So the new slices.
Badgingarra is the green area here, not the purple one.
Also, notice that the new era has four elements, so the newsletter's capacity becomes four.
This means that the apan function allocates a larger array, even though an array with three elements
is enough to store the new element.
There is one more element in the baking array can guess why the apan function does so.
Please post a video and think about it.
It's because a larger area doesn't immediately require a new allocation each time you'll want to appoint
new elements to a slice.
So by allocating a larger array, the apan function does not have to recreate a new beginning.
Or each time you appoint this way, it allocates less often.
So it practically reduces the number of allocations by thinking about the future growth of the slice.
After allocating the new array, the apan function copier's the elements of the previous array to the
new array.
You know, copying things is also costly.
It requires a communication cost between the CPU and the memory.
So it's always better to do this once instead of every time you want to advance.
So this is another reason why the append function creates a larger Venezuela's needs to grow after copying
all the elements that append function assigns the new element to the new era.
And lastly, the apan function assigns zero values to the UN initialized elements of the new era.
There is only one UN initialised element here, so the function assigns zero to it.
All right.
That was how the aband function works.
Now let's take a look at a few coding examples to understand it better.
OK, let me first import the slice package, I'm going to show you the back away, so I'm going to enable
this option.
All right, let me create a needle in slice first.
Lemer printed.
As you can see, this is a new slice, all its fields are zero.
Its point or field is zero two.
So it doesn't have a big Ingrey.
Now, I'm going to expand to odd numbers to it here, the apparent function returns a new slice and
I'm saving it to the NOM's variable back again.
OK, let me print it.
Let's check out the fields of the last slice and the previous one, the previous slice doesn't have
a back injury because it was a needle slice, but the newsless has a non-zero pointer, so it has a
back injury at this memory location and its length and capacity are two, so its capacity is full.
Let's see what happens when I add one more element to it.
So let's expand an even number to it.
Let me print a.
The last apparent creates a larger error and returns a new slice that refers to it.
Let's check out the pointers.
They are different.
So the begging arrays of these slices are located in different member locations.
This means that the last opponent has created a new beginning.
Now let's check out its capacity.
It is far right, but its length is three.
So there is still capacity for one more element.
Let me paint one more element to it.
And let me print it.
As you can see, the Nomsa is still using the previous begging array.
You can understand that by looking at the last two pointers.
Again, they are the same.
By the way, it appears that the apparent function, always a pass to the back of the back injury of
a slice, right.
However, it actually happens at the end of a slice by looking at the length of that slice to show that
to you.
Let's say I want to add two more numbers in the middle of this slice.
How can I do so by using only the append function?
Well, let me first call the apparent function like so now I'm going to create a new slice by slicing
the first two elements of the nums slice.
OK, now I'm going to add seven and nine to it.
Remember, the apparent function expands to the end of a slice depending on its length.
So here it fell apart after the second element.
All right, let me bring it.
As you can see, doing so overrides the second half of the back and correct, I was trying to add two
more numbers into the middle of the slice, but I need to do so without deleting the last two even numbers.
To do that, I need to copy the last two elements before adding seven and nine.
I can do so by appending them again to the numskulls lachsa.
Here I'm spending the last two elements of the numskulls back to the again.
Yes, you can do this.
Why not?
OK, let me print it.
The first offense here duplicates the last two elements, then in the next happened here, it happens
after the first two elements of the numskulls.
So it overrides the middle elements in a good way.
In the end, it happens seven and nine at the middle.
There's one little thing I need to do.
These numbers remain hidden in the back injury.
I need to reveal them.
So how can I make down the part of a slice again?
Please post a video and try to find the solution.
Well, I can easily extend the slice like this, let me also print it.
Well, I have added new numbers at the middle of the slice by using an app and trick, you can find
more app and tricks in the reference links of this lecture.
By the way, so let me summarize first.
I have duplicated the last two elements.
Then I overwritten the middle elements with the new elements.
And at the end I have extended the slice up to six elements.
All right.
That's all for now.
See in the next picture.