1 00:00:05,920 --> 00:00:08,310 In most cases, ownership is clear. 2 00:00:08,320 --> 00:00:16,210 We know exactly which variable owns what value, but sometimes there are cases where a single value 3 00:00:16,240 --> 00:00:19,120 might have or need multiple owners. 4 00:00:19,420 --> 00:00:22,850 For example, the graph data struck. 5 00:00:26,750 --> 00:00:33,890 If we look in here, we see that we have multiple nodes of zero, one, two, three and four, and then 6 00:00:33,890 --> 00:00:37,050 each of these nodes has an edge pointing to it. 7 00:00:37,070 --> 00:00:41,960 Well, Node zero is connected to Node one and four. 8 00:00:41,960 --> 00:00:45,590 So we can say logically that. 9 00:00:46,890 --> 00:00:51,120 No zero had multiple owners of one and four. 10 00:00:51,180 --> 00:00:59,850 So for us to allow multiple ownership, we have RC, which stands for reference counted. 11 00:00:59,880 --> 00:01:07,650 RC is also allocated on the heap and RC is going to track the number of references to evaluate whether 12 00:01:07,650 --> 00:01:09,690 or not a value is still in use. 13 00:01:10,390 --> 00:01:14,770 Naturally, if there are zero references, then the value is cleaned up. 14 00:01:15,010 --> 00:01:22,570 As a side note, there is also Ark RC, which stands for Atomic Reference Counting. 15 00:01:22,600 --> 00:01:29,410 It is the same thing as RC, but ARC is designed for safe sharing between threads, so only use arc 16 00:01:29,410 --> 00:01:31,990 when sharing the pointer between threads. 17 00:01:32,020 --> 00:01:34,510 Otherwise stick to RC. 18 00:01:34,720 --> 00:01:36,750 Now let's see an example of RC. 19 00:01:36,760 --> 00:01:40,960 So the first thing we need to do is actually bring it into scope. 20 00:01:49,270 --> 00:01:51,150 You Standard RC. 21 00:01:51,180 --> 00:01:56,310 Ah, see, that's because I left out. 22 00:01:56,340 --> 00:01:56,880 There we go. 23 00:01:56,900 --> 00:02:00,590 All right, so now we have RC in scope. 24 00:02:00,740 --> 00:02:08,960 So now we'll say let se one equals RC new, create a new reference counter and then we'll say string 25 00:02:08,960 --> 00:02:13,250 from and we'll just say pointer. 26 00:02:15,560 --> 00:02:24,830 And then to create references to our string, we'll say s one dot clone and then we can also clone as 27 00:02:25,370 --> 00:02:30,080 to because it points to the same value on the heap. 28 00:02:31,800 --> 00:02:38,010 And then just to prove that they all point to the same value, we'll print them all out. 29 00:02:40,330 --> 00:02:44,730 We have as one, as two and as three. 30 00:02:45,000 --> 00:02:46,800 And now if we run this. 31 00:02:48,030 --> 00:02:53,790 We see that we have pointer pointer, pointer pointer printed out three times. 32 00:02:54,330 --> 00:03:01,590 So in this we have an RC value pointed to a heap allocated string which is going to have a reference 33 00:03:01,590 --> 00:03:03,330 count associated with it. 34 00:03:03,450 --> 00:03:07,230 Cloning and RC does not copy the value. 35 00:03:07,260 --> 00:03:11,990 It creates another pointer to it and increment the reference count. 36 00:03:12,000 --> 00:03:19,500 So again, in this example, the string pointer has a reference count of three and SW one, SW two and 37 00:03:19,500 --> 00:03:26,100 SW three, which are all on the stack, all point to the string that is located on the heap. 38 00:03:30,090 --> 00:03:36,720 Our data being a string, we are still able to use string methods as usual directly. 39 00:03:36,720 --> 00:03:37,380 And I can. 40 00:03:37,380 --> 00:03:45,420 I'll show this to you by saying s one dot contains and then in here we want to make sure it contains 41 00:03:45,420 --> 00:03:51,330 point and then just to show that all the other ones are able to do it as well. 42 00:03:51,330 --> 00:04:00,990 We'll say contains in this one and we'll say ter so we would expect our output to now be true. 43 00:04:01,020 --> 00:04:05,220 Pointer And true and we have true. 44 00:04:05,220 --> 00:04:08,940 Pointer True, which is what we anticipated. 45 00:04:09,000 --> 00:04:13,530 And that's really again all there is to it, to RC and AAC. 46 00:04:13,530 --> 00:04:18,330 So now in the next lecture we're going to talk about ref cell.