WEBVTT 00:02.490 --> 00:03.030 Wilga. 00:04.180 --> 00:07.180 Let's take a look at what a slice Heather looks like in the Scott. 00:08.480 --> 00:14.390 As you can see, it is just a data structure with three integer fields, can you create a slice? 00:14.390 --> 00:15.560 Go Handschu. 00:15.560 --> 00:20.930 This data structure, its array field, contains the memory location of its begging arrays. 00:20.930 --> 00:27.120 First element here, it says the first field is an array, but is actually a pointer, as I told you. 00:27.680 --> 00:32.650 So in summary, a slice value doesn't contain anything beyond these fields. 00:32.930 --> 00:34.070 That is all about it. 00:34.830 --> 00:36.470 OK, let me show you an example. 00:36.740 --> 00:40.670 First of all, let me first impart my previous last package here, OK? 00:41.210 --> 00:43.130 I will show you the memory addresses. 00:43.610 --> 00:47.210 So I'm going to enable print element IDR option to. 00:47.210 --> 00:47.490 True. 00:48.140 --> 00:48.540 All right. 00:48.650 --> 00:51.470 Let me start by declaring a new era type here. 00:52.010 --> 00:53.760 I will make it a slice type later on. 00:53.870 --> 00:56.840 So this type definition will make the example easier. 00:57.800 --> 01:01.370 OK, now I'm going to create a new era with four elements like this. 01:06.940 --> 01:12.850 Now I'm going to pass it to a function like this, this function doesn't exist yet, so let me declare 01:12.850 --> 01:13.060 it. 01:13.750 --> 01:16.930 It accepts a value of type collection like this. 01:17.740 --> 01:21.600 By the way, I know that you didn't learn about functions and pointers yet. 01:21.610 --> 01:23.290 However, they are necessary here. 01:23.320 --> 01:25.720 So I can show you what's going on behind the scenes. 01:25.750 --> 01:32.050 OK, think of the data parameter as a variable that is already declared inside this function like so 01:32.710 --> 01:39.280 go is science or in other words, copy the past arguments value to this local data variable like so 01:40.030 --> 01:42.400 of course this is not a valid syntax. 01:42.550 --> 01:45.620 I'm using it just to explain it to you how it works. 01:45.640 --> 01:47.080 OK, ok. 01:47.080 --> 01:53.680 Here I'm going to modify the local data area like so here I'm modifying the data value that belongs 01:53.680 --> 01:54.470 to this function. 01:55.180 --> 01:55.600 All right. 01:55.790 --> 01:56.830 Let me also print it. 01:58.550 --> 02:04.230 OK, let's go up a little bit and here they also print the main function, data array value. 02:04.670 --> 02:05.470 All right, ready? 02:05.510 --> 02:06.110 Limerence. 02:07.580 --> 02:13.460 By the way, my package displays the array elements as single items, but there are actually multiple 02:13.460 --> 02:13.870 items. 02:14.330 --> 02:16.910 So here are four elements separated by spaces. 02:16.940 --> 02:24.020 OK, as you can see, the main functions, data array doesn't change, only the change functions local. 02:24.020 --> 02:25.180 Their array changes. 02:25.640 --> 02:30.400 It is because passing a value to our function makes that new copy of it. 02:30.980 --> 02:33.230 So there are two different data areas here. 02:34.130 --> 02:39.770 Remember, an array represents all of its values in a single value, in a packed way. 02:40.130 --> 02:46.970 So here, when I pass the array to the change function, go capias all of its elements entirely. 02:47.600 --> 02:51.600 So the air inside the change function is a new era value. 02:52.010 --> 02:54.550 That is why it doesn't change after this assignment. 02:55.660 --> 02:59.110 By the way, these are the addresses of the data iRace. 03:00.190 --> 03:06.940 This is the location of the change function's data era value, and this is the memory location of the 03:06.940 --> 03:09.150 main functions, data array value. 03:09.580 --> 03:14.020 As you can see, they are located in different separate memory locations. 03:14.590 --> 03:16.660 It's because they are different values. 03:16.900 --> 03:19.910 Each one has its own place in the computer memory. 03:20.420 --> 03:20.680 OK. 03:21.650 --> 03:27.700 All right, enough with our now let me change the collection time to a slice, that's it. 03:27.710 --> 03:32.900 Now, the data variables which are here and here are slices now not. 03:33.730 --> 03:35.950 OK, OK, let me read it again. 03:36.880 --> 03:41.780 This time, all the slicers see the change to the third element, can you guess why? 03:41.980 --> 03:43.810 Please pass the video and try to answer. 03:49.120 --> 03:49.810 Let me explain. 03:50.260 --> 03:56.970 Not that there are still two values here, so when I passed the data slice to the change function here, 03:57.010 --> 04:02.390 it gets copied, thus creating a new slice, whether that is local to the change function. 04:03.160 --> 04:10.090 However, not that code does not copy the slices back and correct it only copies the slice other. 04:10.820 --> 04:11.050 Sure. 04:11.350 --> 04:11.500 You. 04:12.800 --> 04:16.550 Now, I'm going to print the address of the main data slice like this. 04:20.490 --> 04:24.510 And I'm going to print the memory address of the change function status in the same way. 04:26.500 --> 04:31.990 I'll explain the pointers in detail later on, but let me briefly tell you that this ampersands operator 04:31.990 --> 04:34.840 gets the address of a value that is next to it. 04:35.470 --> 04:36.400 OK, limerence. 04:37.090 --> 04:39.340 As you can see, they have different addresses. 04:39.700 --> 04:43.540 If they were the same values, their addresses will be the same. 04:43.930 --> 04:47.320 This means that they are located in different member locations. 04:47.770 --> 04:49.390 So they are different values. 04:49.600 --> 04:51.670 So there are two headers here. 04:52.360 --> 04:53.830 Let's take a look at their pointer. 04:53.830 --> 04:57.150 Feels they contain the same backing arrays address. 04:57.550 --> 05:00.530 This means that they point to the same making. 05:01.360 --> 05:06.360 It doesn't matter whether there are two values or not for these slices. 05:06.370 --> 05:08.650 There is only one back in the memory. 05:09.250 --> 05:12.500 So they always see the same elements of the same back injury. 05:13.060 --> 05:17.620 When an element of that area changes, they all see this change. 05:18.190 --> 05:19.910 Let me show you one last example. 05:20.560 --> 05:25.870 Now I'm going to create an array with the same elements of the data slice, and I'm going to show you 05:25.870 --> 05:27.130 the size of this array. 05:29.860 --> 05:36.030 Here, I'm going to use the answer package's size or function, this function gives me the memory size 05:36.040 --> 05:37.210 of the past value. 05:37.290 --> 05:41.020 OK, by the way, do not use this function in the real code. 05:41.300 --> 05:43.050 It is clearly unsafe. 05:43.060 --> 05:44.680 It lives in unsafe package. 05:46.630 --> 05:50.860 OK, now I'm also going to print the memory size of the data sliced like this. 05:52.950 --> 05:53.640 Let's take a look. 05:54.240 --> 05:59.550 Even though the data slides and the error have the same elements, their sizes are different, the of 05:59.620 --> 06:00.610 size is smaller. 06:00.990 --> 06:04.950 This is because it only stores three integer fields in a slice header. 06:05.160 --> 06:08.790 So on a 64 bit machine, this means twenty four bytes. 06:08.940 --> 06:12.360 So its size is always fixed to twenty four bytes. 06:12.630 --> 06:16.590 Power and arrays, size changes depending on its elements. 06:16.840 --> 06:19.710 It's because it contains all the elements on its own. 06:21.380 --> 06:25.520 Let me add one more element to this area and to the data slice. 06:27.460 --> 06:33.410 As you can see, the slice of size remains fixed at 24 bites, but the area size gets larger. 06:33.970 --> 06:35.080 Now it's eight bites. 06:35.680 --> 06:42.580 So in summary, creating, assigning, slicing and passing a slice to a function is a very cheap operation 06:42.970 --> 06:45.040 is because its size is fixed. 06:45.370 --> 06:52.420 However, it's not like so with an array is because an array will be copied along with all of its items 06:52.420 --> 06:53.100 or elements. 06:53.440 --> 06:57.060 But when you pass a slice on each slice, Heather will be copied. 06:57.670 --> 07:03.530 By the way, do not forget that you create a slice or creates a new era behind the scenes. 07:04.000 --> 07:10.720 So my calculation here doesn't include the slices begging array size hoer since it doesn't contain its 07:11.170 --> 07:12.060 carry on its own. 07:12.190 --> 07:18.910 So it doesn't matter what its size this is because it doesn't carry its Badgingarra along with itself. 07:19.120 --> 07:22.310 It's just referring to it through its pointer field in the memory. 07:22.810 --> 07:23.220 All right. 07:23.440 --> 07:24.210 That's all for now. 07:24.250 --> 07:25.510 Thank you for watching so far. 07:25.540 --> 07:26.830 Seeing the next picture by.