WEBVTT 0 00:01.770 --> 00:02.920 Welcome. 1 00:02.950 --> 00:08.880 Now, let's take a look at an example in the coding editor. 2 00:09.030 --> 00:15.640 Now, I'm going to declare an int pointer variable. As you can see, I just need to type a star before 3 00:15.640 --> 00:16.530 the int type. 4 00:16.540 --> 00:22.180 This is what makes it an int pointer type. The zero value of a pointer is nil. 5 00:22.180 --> 00:22.950 Let me show you. 6 00:23.020 --> 00:25.390 I'm going to check whether it is nil or not, 7 00:25.390 --> 00:30.870 by comparing it to nil. If so, I'm going to print its address like so. 8 00:30.880 --> 00:39.230 P is nil, and its address is Person p, new line. You need to use %p verb here to print a memory address 9 00:39.230 --> 00:45.110 of the P variable. As you can see it says the pointer is nil and its address is zero. 10 00:46.010 --> 00:52.670 So far, you've seen the memory addresses as bare integer numbers. But normally, memory addresses are represented 11 00:52.670 --> 00:59.000 with hex numbers. It's because the addressable memory space is very large so it is easier to represent 12 00:59.000 --> 01:01.230 them with fewer digits. 13 01:01.400 --> 01:05.830 Let's store an address of a variable in this pointer variable. To do that, 14 01:05.840 --> 01:09.310 I'm going to declare an int variable because P is an int pointer. 15 01:10.190 --> 01:15.580 OK, now I'm going to assign the counter's address to the variable P. By the way, 16 01:15.590 --> 01:18.910 you can check whether an address is equal to another one. 17 01:19.040 --> 01:26.180 For example let me compare the pointer variable with the counter's address. If they are equal, let's print 18 01:26.180 --> 01:35.620 a message with their addresses. Person p equals Person p new line and I'm going to pass the pointer 19 01:35.630 --> 01:42.380 variable p and the address of the counter variable as you can see it doesn't print the pointer is nil 20 01:42.380 --> 01:48.830 message anymore because the pointer P variable is not nil now it contains the address of the counter. 21 01:48.830 --> 01:51.890 Here it says both addresses are the same. 22 01:51.890 --> 01:55.800 This means that variable p points to the counter variable's address. 23 01:56.100 --> 02:00.920 Okay I'm going to remove these messages and let me set the counter to 100, 24 02:00.950 --> 02:06.730 as an example. Now I'm going to print the value and address of the counter variable like so. I'm going 25 02:06.730 --> 02:10.520 to pass the counter variable and the address of the counter variable. 26 02:10.560 --> 02:13.540 Let me also print the pointer variable first. 27 02:13.760 --> 02:18.540 I'm going to print the memory address that it points to. So I can simply say P. 28 02:18.830 --> 02:22.690 Then I'm going to print its address using the address operator like so. 29 02:22.850 --> 02:28.340 As I said, a pointer variable also has an address because it's a variable. 30 02:28.340 --> 02:35.580 Lastly, I'm going to print the value that it points to like so. As you can see, there are three values that 31 02:35.580 --> 02:42.390 you can get from a pointer variable. Where the pointer variable points. The pointer variable's memory address. 32 02:42.510 --> 02:46.950 And the actual value of the variable that the pointer variable points to. 33 02:46.950 --> 02:51.300 This is the counter's address, and this is where the pointer points to. 34 02:51.930 --> 02:56.620 As you can see, they are equal because the "P" pointer points to the counter. 35 02:56.850 --> 03:00.450 Also notice that, indirection operator returns 100. 36 03:00.450 --> 03:03.020 This is the value of the counter variable. 37 03:03.120 --> 03:09.330 And lastly, let's take a look at the memory address of the pointer variable. The pointer variable is stored 38 03:09.420 --> 03:11.580 in this memory address. 39 03:11.580 --> 03:16.100 Now let's introduce another int variable like so. Down below, 40 03:16.110 --> 03:19.530 I'm going to set its value through the pointer like so. 41 03:19.590 --> 03:20.760 Let me also print it. 42 03:23.660 --> 03:29.480 As you can see the address of the variable v is different than the counter variable('s address). It's because they 43 03:29.480 --> 03:31.130 are different variables. 44 03:31.280 --> 03:36.710 Right now, the value of the variable "V" is the same as the counter variable. Let me print a 45 03:36.710 --> 03:43.840 header here to separate the messages that I'm going to print. Okay. Now I'm going to change the counter 46 03:43.850 --> 03:44.910 variable to 10. 47 03:44.990 --> 03:45.920 Like so. 48 03:46.210 --> 03:49.070 Let me also copy and paste the messages from above. 49 03:51.340 --> 03:57.580 As you can see only the counter changes but the V variable holds the counters previous value. 50 03:57.580 --> 04:01.100 This happens because there are two different variables. 51 04:01.360 --> 04:04.020 Let me print another header here. 52 04:04.030 --> 04:12.830 Now I'm going to show you what happens when you pass a variable to a function without using a pointer. 53 04:12.920 --> 04:15.800 Let me also print the counter. 54 04:15.890 --> 04:23.750 Now let's go ahead and declare the passVal function. It takes an int value, inside I'm going to set "n" 55 04:23.850 --> 04:29.510 to 50, and I'm going to print the value and address of the n variable. 56 04:29.510 --> 04:30.070 All right. 57 04:30.060 --> 04:36.820 Ready. As you can see, only the n changes but the counter stays the same. 58 04:36.820 --> 04:42.250 Let's take a look at the address of the variable n. It is different than the counter's address. 59 04:42.250 --> 04:44.900 This means that there are different variables. 60 04:44.920 --> 04:48.760 That is why the counter doesn't change when I change the variable n. 61 04:48.950 --> 04:54.640 As you will see that is why I need to return the local variables of a function. 62 04:54.640 --> 05:00.910 Now I'm going to return the local variable n to the caller like so. Right above, in the main function, 63 05:00.910 --> 05:05.520 I'm going to assign the returned value to the counter like this. 64 05:05.650 --> 05:10.400 Now the values of counter and n variables are the same. 65 05:10.480 --> 05:12.980 Let me add another header here. 66 05:13.000 --> 05:17.560 Now I'm going to show you what happens when you send a pointer to a function. 67 05:17.650 --> 05:20.840 I'm going to send the address of counter to the pastPtrVal 68 05:20.900 --> 05:25.730 function, like so. Ptr is short for a pointer by the way. 69 05:25.970 --> 05:28.460 Let me also print the counter. 70 05:28.460 --> 05:31.290 OK let's go down and declare the function there. 71 05:33.420 --> 05:40.740 Here I'm going to declare an int pointer parameter like so. Remember when you type a star before a type it 72 05:40.740 --> 05:42.790 becomes a pointer type. 73 05:42.900 --> 05:47.830 So please do not confuse this with the indirection operator OK. 74 05:47.950 --> 05:52.060 Inside I'm going to print the input parameter pn. Here, 75 05:52.100 --> 05:54.200 I'm going to print the value of pn, 76 05:54.440 --> 05:58.010 this is what it stores. And I'm going to print its address. 77 05:58.010 --> 06:01.250 This is where the pn lives in memory. 78 06:01.250 --> 06:05.240 And lastly, I'm going to print the value that it points to. 79 06:05.360 --> 06:11.740 This will be the counter variable's value. Let me duplicate it. Between them 80 06:11.740 --> 06:16.750 I'm going to use the indirection operator to get the counter's value then I'm going to increment the 81 06:16.750 --> 06:22.510 counter like so. By the way, I don't need to type the parentheses here. 82 06:22.510 --> 06:30.960 Go automatically does that for us. As you can see the address of pn is different than the previous variables 83 06:30.990 --> 06:34.880 that you have seen above. pn is a different variable. 84 06:34.890 --> 06:41.760 This means that whenever a function runs it creates a set of new variables that is local to that function. 85 06:41.760 --> 06:44.150 Now let's take a look at the value of pn. 86 06:44.190 --> 06:47.090 It points to the counter's address. 87 06:47.100 --> 06:53.170 The interesting thing here is that the counter variable doesn't belong to the pastPtrVal function. 88 06:53.340 --> 06:56.220 But it can still see the counter's address. 89 06:56.220 --> 07:02.430 This means that the pastPtrVal function can change the counter value using the pointer as well. So a 90 07:02.460 --> 07:07.530 function can change a value indirectly using a pointer. 91 07:07.560 --> 07:10.900 Actually this is exactly what happens here. 92 07:10.920 --> 07:18.510 At first, pn returns the counter's value as 50. Then inside the function, I increment the counter 93 07:18.540 --> 07:25.380 through the pointer. In the end, pn says that the counter is 51 and it is right. 94 07:25.380 --> 07:32.670 Doing so changes the main function's counter variable. It's because both functions work on the same variable, 95 07:32.910 --> 07:39.800 through the pointer. Let me tell you one last thing. Pointers are copied too. 96 07:39.870 --> 07:45.210 So every time a program calls a function, Go creates a new pointer variable. 97 07:45.210 --> 07:51.810 For example let's say whenever this function pulses please assume that the program calls it with the 98 07:51.810 --> 07:54.240 address of the counter variable. 99 07:54.330 --> 07:56.650 Here is the first call to the function. 100 07:56.820 --> 08:00.090 As you can see it creates a new pointer variable. 101 08:00.090 --> 08:06.080 Here is the second call so it creates another pointer variable. 102 08:06.080 --> 08:10.190 Both of these pointer variables point to the same memory location. 103 08:10.340 --> 08:16.670 It's because the program passes the address of the counter to the function so it copies the address 104 08:16.730 --> 08:20.040 into a new int pointer variable. 105 08:20.180 --> 08:26.810 Of course, when the function returns the runtime will delete the allocated pointer variables from memory 106 08:26.900 --> 08:28.030 automatically. 107 08:28.430 --> 08:31.500 Let's say the first call to the function returns. 108 08:31.520 --> 08:35.520 Here goes the first one and the second function returns, 109 08:35.600 --> 08:38.060 the other one will be collected as well. 110 08:38.780 --> 08:45.690 However, when you return pointer variables from a function the runtime won't clean them up. 111 08:46.070 --> 08:49.400 It will clean them up only when they are not being used anymore. 112 08:51.230 --> 08:54.790 Let me call the pastPtrVal function a few times. 113 08:57.350 --> 09:01.220 These are the addresses of pointer variables inside the pastPtrVal 114 09:01.220 --> 09:07.970 function. As you can see, each time I call it, it prints a different address. 115 09:08.010 --> 09:14.660 It's because it's a pointer variable (pn). Although all of these pointer variables point to the same memory 116 09:14.660 --> 09:18.340 location, their addresses are different. 117 09:18.360 --> 09:23.670 For example, if I set the pointer variable to nil here, it won't affect the caller. 118 09:24.790 --> 09:27.510 As you can see the output is the same. 119 09:27.540 --> 09:35.290 It's because here I am only setting the local pointer valuable to nil which is a copy all right. 120 09:35.300 --> 09:38.190 This was why you might want to use pointers. 121 09:38.230 --> 09:43.040 It's because they can work across functions or even across packages. 122 09:43.040 --> 09:49.420 There is only a single memory so every function can see it. If you share an address with a function, 123 09:49.490 --> 09:54.630 it can retrieve and change the value that is stored in that address. 124 09:54.650 --> 10:02.240 This is also why pointers can be dangerous because any part of the code can change any value, 125 10:02.420 --> 10:09.430 if they can get their addresses. However, Go is not that dangerous as the other languages that support 126 10:09.440 --> 10:13.220 pointers because Go automatically manages the memory. 127 10:13.300 --> 10:14.700 Alright that's all for now. 128 10:14.810 --> 10:15.700 See you in the next lecture.