Wilga.
Let's take a look at what a slice Heather looks like in the Scott.
As you can see, it is just a data structure with three integer fields, can you create a slice?
Go Handschu.
This data structure, its array field, contains the memory location of its begging arrays.
First element here, it says the first field is an array, but is actually a pointer, as I told you.
So in summary, a slice value doesn't contain anything beyond these fields.
That is all about it.
OK, let me show you an example.
First of all, let me first impart my previous last package here, OK?
I will show you the memory addresses.
So I'm going to enable print element IDR option to.
True.
All right.
Let me start by declaring a new era type here.
I will make it a slice type later on.
So this type definition will make the example easier.
OK, now I'm going to create a new era with four elements like this.
Now I'm going to pass it to a function like this, this function doesn't exist yet, so let me declare
it.
It accepts a value of type collection like this.
By the way, I know that you didn't learn about functions and pointers yet.
However, they are necessary here.
So I can show you what's going on behind the scenes.
OK, think of the data parameter as a variable that is already declared inside this function like so
go is science or in other words, copy the past arguments value to this local data variable like so
of course this is not a valid syntax.
I'm using it just to explain it to you how it works.
OK, ok.
Here I'm going to modify the local data area like so here I'm modifying the data value that belongs
to this function.
All right.
Let me also print it.
OK, let's go up a little bit and here they also print the main function, data array value.
All right, ready?
Limerence.
By the way, my package displays the array elements as single items, but there are actually multiple
items.
So here are four elements separated by spaces.
OK, as you can see, the main functions, data array doesn't change, only the change functions local.
Their array changes.
It is because passing a value to our function makes that new copy of it.
So there are two different data areas here.
Remember, an array represents all of its values in a single value, in a packed way.
So here, when I pass the array to the change function, go capias all of its elements entirely.
So the air inside the change function is a new era value.
That is why it doesn't change after this assignment.
By the way, these are the addresses of the data iRace.
This is the location of the change function's data era value, and this is the memory location of the
main functions, data array value.
As you can see, they are located in different separate memory locations.
It's because they are different values.
Each one has its own place in the computer memory.
OK.
All right, enough with our now let me change the collection time to a slice, that's it.
Now, the data variables which are here and here are slices now not.
OK, OK, let me read it again.
This time, all the slicers see the change to the third element, can you guess why?
Please pass the video and try to answer.
Let me explain.
Not that there are still two values here, so when I passed the data slice to the change function here,
it gets copied, thus creating a new slice, whether that is local to the change function.
However, not that code does not copy the slices back and correct it only copies the slice other.
Sure.
You.
Now, I'm going to print the address of the main data slice like this.
And I'm going to print the memory address of the change function status in the same way.
I'll explain the pointers in detail later on, but let me briefly tell you that this ampersands operator
gets the address of a value that is next to it.
OK, limerence.
As you can see, they have different addresses.
If they were the same values, their addresses will be the same.
This means that they are located in different member locations.
So they are different values.
So there are two headers here.
Let's take a look at their pointer.
Feels they contain the same backing arrays address.
This means that they point to the same making.
It doesn't matter whether there are two values or not for these slices.
There is only one back in the memory.
So they always see the same elements of the same back injury.
When an element of that area changes, they all see this change.
Let me show you one last example.
Now I'm going to create an array with the same elements of the data slice, and I'm going to show you
the size of this array.
Here, I'm going to use the answer package's size or function, this function gives me the memory size
of the past value.
OK, by the way, do not use this function in the real code.
It is clearly unsafe.
It lives in unsafe package.
OK, now I'm also going to print the memory size of the data sliced like this.
Let's take a look.
Even though the data slides and the error have the same elements, their sizes are different, the of
size is smaller.
This is because it only stores three integer fields in a slice header.
So on a 64 bit machine, this means twenty four bytes.
So its size is always fixed to twenty four bytes.
Power and arrays, size changes depending on its elements.
It's because it contains all the elements on its own.
Let me add one more element to this area and to the data slice.
As you can see, the slice of size remains fixed at 24 bites, but the area size gets larger.
Now it's eight bites.
So in summary, creating, assigning, slicing and passing a slice to a function is a very cheap operation
is because its size is fixed.
However, it's not like so with an array is because an array will be copied along with all of its items
or elements.
But when you pass a slice on each slice, Heather will be copied.
By the way, do not forget that you create a slice or creates a new era behind the scenes.
So my calculation here doesn't include the slices begging array size hoer since it doesn't contain its
carry on its own.
So it doesn't matter what its size this is because it doesn't carry its Badgingarra along with itself.
It's just referring to it through its pointer field in the memory.
All right.
That's all for now.
Thank you for watching so far.
Seeing the next picture by.