WEBVTT 00:01.500 --> 00:06.450 Balga in his lecture, I'm going to talk about the mechanics of the app and function in more detail. 00:06.660 --> 00:07.780 I'll let this get started. 00:08.980 --> 00:14.700 All right, let's continue using the example from the previous lecture to keep things simpler this time 00:14.740 --> 00:18.030 I have only created a slice with two elements instead of three. 00:18.460 --> 00:19.240 Here's a question. 00:19.510 --> 00:23.290 What would happen if you appoint one more element to this slice? 00:26.740 --> 00:30.040 OK, let me add one more element to the ages slice. 00:31.490 --> 00:34.290 Notice that it's backing is already full. 00:34.370 --> 00:38.900 I mean, there are two items in the area, but it needs a room for one more element. 00:38.930 --> 00:44.630 So when this happens, the append function needs to allocate a new and a larger array on the memory 00:44.630 --> 00:50.840 like this is because arrays are fixed in that sort of apan function, cannot grow the existing array. 00:50.960 --> 00:53.810 So it needs to allocate an entirely new array. 00:54.050 --> 00:59.010 This is also because allocating a block of memory as fixed blocks are more efficient. 00:59.240 --> 01:04.700 So after allocating the new era, the apparent function creates and returns a new Selous header. 01:05.000 --> 01:10.940 Notice that the previous header points to the purple area, but the newsless header points to the greenery. 01:10.970 --> 01:12.020 So the new slices. 01:12.020 --> 01:15.050 Badgingarra is the green area here, not the purple one. 01:15.730 --> 01:21.320 Also, notice that the new era has four elements, so the newsletter's capacity becomes four. 01:21.470 --> 01:27.050 This means that the apan function allocates a larger array, even though an array with three elements 01:27.050 --> 01:29.060 is enough to store the new element. 01:29.210 --> 01:34.190 There is one more element in the baking array can guess why the apan function does so. 01:34.310 --> 01:36.110 Please post a video and think about it. 01:38.260 --> 01:44.170 It's because a larger area doesn't immediately require a new allocation each time you'll want to appoint 01:44.200 --> 01:45.560 new elements to a slice. 01:45.580 --> 01:51.340 So by allocating a larger array, the apan function does not have to recreate a new beginning. 01:51.340 --> 01:55.450 Or each time you appoint this way, it allocates less often. 01:55.600 --> 02:02.020 So it practically reduces the number of allocations by thinking about the future growth of the slice. 02:02.560 --> 02:07.930 After allocating the new array, the apan function copier's the elements of the previous array to the 02:07.930 --> 02:08.500 new array. 02:08.770 --> 02:11.660 You know, copying things is also costly. 02:11.680 --> 02:15.460 It requires a communication cost between the CPU and the memory. 02:15.760 --> 02:20.020 So it's always better to do this once instead of every time you want to advance. 02:20.040 --> 02:27.340 So this is another reason why the append function creates a larger Venezuela's needs to grow after copying 02:27.340 --> 02:31.820 all the elements that append function assigns the new element to the new era. 02:32.140 --> 02:38.540 And lastly, the apan function assigns zero values to the UN initialized elements of the new era. 02:38.740 --> 02:44.020 There is only one UN initialised element here, so the function assigns zero to it. 02:44.470 --> 02:44.920 All right. 02:45.130 --> 02:47.230 That was how the aband function works. 02:47.590 --> 02:51.160 Now let's take a look at a few coding examples to understand it better. 02:52.690 --> 02:58.540 OK, let me first import the slice package, I'm going to show you the back away, so I'm going to enable 02:58.540 --> 02:59.110 this option. 03:00.280 --> 03:03.520 All right, let me create a needle in slice first. 03:04.920 --> 03:05.670 Lemer printed. 03:10.480 --> 03:14.530 As you can see, this is a new slice, all its fields are zero. 03:14.860 --> 03:16.720 Its point or field is zero two. 03:17.260 --> 03:19.000 So it doesn't have a big Ingrey. 03:19.930 --> 03:26.410 Now, I'm going to expand to odd numbers to it here, the apparent function returns a new slice and 03:26.410 --> 03:28.870 I'm saving it to the NOM's variable back again. 03:29.590 --> 03:30.760 OK, let me print it. 03:35.020 --> 03:40.810 Let's check out the fields of the last slice and the previous one, the previous slice doesn't have 03:40.810 --> 03:46.840 a back injury because it was a needle slice, but the newsless has a non-zero pointer, so it has a 03:46.840 --> 03:53.900 back injury at this memory location and its length and capacity are two, so its capacity is full. 03:54.160 --> 03:57.570 Let's see what happens when I add one more element to it. 03:57.730 --> 04:00.040 So let's expand an even number to it. 04:00.830 --> 04:01.900 Let me print a. 04:06.500 --> 04:11.210 The last apparent creates a larger error and returns a new slice that refers to it. 04:12.020 --> 04:13.310 Let's check out the pointers. 04:13.580 --> 04:14.390 They are different. 04:14.480 --> 04:19.190 So the begging arrays of these slices are located in different member locations. 04:19.850 --> 04:22.930 This means that the last opponent has created a new beginning. 04:23.780 --> 04:25.800 Now let's check out its capacity. 04:25.880 --> 04:29.060 It is far right, but its length is three. 04:29.390 --> 04:32.280 So there is still capacity for one more element. 04:33.020 --> 04:34.960 Let me paint one more element to it. 04:36.130 --> 04:37.180 And let me print it. 04:40.970 --> 04:45.000 As you can see, the Nomsa is still using the previous begging array. 04:45.530 --> 04:48.910 You can understand that by looking at the last two pointers. 04:48.920 --> 04:50.480 Again, they are the same. 04:51.570 --> 04:57.120 By the way, it appears that the apparent function, always a pass to the back of the back injury of 04:57.120 --> 04:58.080 a slice, right. 04:58.350 --> 05:05.340 However, it actually happens at the end of a slice by looking at the length of that slice to show that 05:05.340 --> 05:05.870 to you. 05:05.880 --> 05:09.690 Let's say I want to add two more numbers in the middle of this slice. 05:09.980 --> 05:12.900 How can I do so by using only the append function? 05:13.350 --> 05:19.500 Well, let me first call the apparent function like so now I'm going to create a new slice by slicing 05:19.500 --> 05:21.780 the first two elements of the nums slice. 05:22.440 --> 05:25.200 OK, now I'm going to add seven and nine to it. 05:25.440 --> 05:30.540 Remember, the apparent function expands to the end of a slice depending on its length. 05:30.870 --> 05:33.690 So here it fell apart after the second element. 05:33.930 --> 05:35.130 All right, let me bring it. 05:37.860 --> 05:44.160 As you can see, doing so overrides the second half of the back and correct, I was trying to add two 05:44.160 --> 05:50.140 more numbers into the middle of the slice, but I need to do so without deleting the last two even numbers. 05:50.550 --> 05:55.240 To do that, I need to copy the last two elements before adding seven and nine. 05:55.380 --> 05:59.250 I can do so by appending them again to the numskulls lachsa. 06:00.320 --> 06:05.330 Here I'm spending the last two elements of the numskulls back to the again. 06:05.840 --> 06:07.160 Yes, you can do this. 06:07.370 --> 06:07.880 Why not? 06:08.690 --> 06:09.860 OK, let me print it. 06:13.590 --> 06:19.950 The first offense here duplicates the last two elements, then in the next happened here, it happens 06:19.950 --> 06:22.510 after the first two elements of the numskulls. 06:23.010 --> 06:25.910 So it overrides the middle elements in a good way. 06:26.160 --> 06:29.400 In the end, it happens seven and nine at the middle. 06:30.150 --> 06:32.180 There's one little thing I need to do. 06:32.490 --> 06:35.150 These numbers remain hidden in the back injury. 06:35.490 --> 06:36.780 I need to reveal them. 06:37.140 --> 06:39.810 So how can I make down the part of a slice again? 06:39.810 --> 06:42.450 Please post a video and try to find the solution. 06:45.470 --> 06:50.090 Well, I can easily extend the slice like this, let me also print it. 06:52.870 --> 06:58.930 Well, I have added new numbers at the middle of the slice by using an app and trick, you can find 06:58.930 --> 07:01.610 more app and tricks in the reference links of this lecture. 07:01.630 --> 07:04.000 By the way, so let me summarize first. 07:04.000 --> 07:06.040 I have duplicated the last two elements. 07:06.220 --> 07:09.130 Then I overwritten the middle elements with the new elements. 07:09.400 --> 07:12.880 And at the end I have extended the slice up to six elements. 07:13.450 --> 07:13.870 All right. 07:14.000 --> 07:14.740 That's all for now. 07:15.010 --> 07:15.970 See in the next picture.