1 00:00:01,480 --> 00:00:10,960 The memory lifetime memory allocated by new keywords will remain valid until you call the delete here. 2 00:00:11,200 --> 00:00:18,250 So this means that you may have memory with long lifetimes and the code may be passed around various 3 00:00:18,250 --> 00:00:20,980 functions in your code. 4 00:00:21,070 --> 00:00:22,660 So here, consider this. 5 00:00:22,660 --> 00:00:26,500 Here, let's make p one new integer 4 to 2. 6 00:00:27,680 --> 00:00:32,540 Here an integer key to do something. 7 00:00:33,640 --> 00:00:36,390 This is the function that we will create. 8 00:00:37,620 --> 00:00:37,950 Yes. 9 00:00:39,030 --> 00:00:49,290 Delete P one and p one equals no p t r and here make the do something function here. 10 00:00:50,950 --> 00:00:52,910 Integer here. 11 00:00:53,680 --> 00:00:55,510 Do something. 12 00:01:02,040 --> 00:01:05,040 And in return integer. 13 00:01:06,340 --> 00:01:06,790 Here. 14 00:01:07,990 --> 00:01:08,650 Or make. 15 00:01:08,650 --> 00:01:14,710 Let's create a variable here integer my well and my. 16 00:01:16,310 --> 00:01:18,260 This return to my Val here. 17 00:01:20,490 --> 00:01:21,150 Here. 18 00:01:21,150 --> 00:01:25,830 As you can see here, we cannot be we have to do typecast here. 19 00:01:26,190 --> 00:01:29,970 And, uh, we got to do something here. 20 00:01:30,060 --> 00:01:38,340 And as you can see here, we initialize P one to no TR But what about the P two here? 21 00:01:39,300 --> 00:01:47,040 And these quote creates a pointer and initialize the memory it points to and then passes the pointer 22 00:01:47,040 --> 00:01:51,780 to function, which itself returns the pointer here. 23 00:01:51,930 --> 00:01:54,270 So since the p one. 24 00:01:55,160 --> 00:01:58,640 Signs the P one pointer is no longer needed. 25 00:01:58,730 --> 00:02:05,300 It is deleted and assigned to New Peter so that it cannot be used again. 26 00:02:05,300 --> 00:02:06,950 So this code looks fine. 27 00:02:06,950 --> 00:02:12,380 But the problem is what do you do with the pointer returned by the function? 28 00:02:12,410 --> 00:02:16,400 Imagine here, let's change our function. 29 00:02:16,400 --> 00:02:19,700 Now imagine this following function here. 30 00:02:20,000 --> 00:02:21,770 P here. 31 00:02:23,960 --> 00:02:25,340 Uh, equals ten. 32 00:02:27,460 --> 00:02:28,620 Because it's not p. 33 00:02:28,620 --> 00:02:29,500 P integer. 34 00:02:30,220 --> 00:02:31,810 And return. 35 00:02:31,820 --> 00:02:33,640 P integer. 36 00:02:35,470 --> 00:02:41,410 So in in this code, the calling do something. 37 00:02:41,890 --> 00:02:49,240 We actually we can if we do like this, we're going to get an error and that's why we have to call the 38 00:02:49,240 --> 00:02:54,010 cast and yeah, yeah, it's okay to use this here before the function. 39 00:02:54,310 --> 00:03:01,820 So this calling do something creates a copy of a pointer, but not copy of what it points to. 40 00:03:01,840 --> 00:03:06,880 So this means that when P one pointer is deleted here. 41 00:03:08,600 --> 00:03:11,740 The memory it points to is no longer available. 42 00:03:11,750 --> 00:03:19,370 And so the pointer to here points to the invalid memory. 43 00:03:19,730 --> 00:03:26,750 So this problem can be addressed using a mechanism called resource occlusion initialization. 44 00:03:27,830 --> 00:03:36,990 This means error, i.e. here, which means using the features of C++ objects to manage resources. 45 00:03:37,010 --> 00:03:40,280 So here I'm going to make it here. 46 00:03:40,580 --> 00:03:48,650 Error a e here in C++, it needs classes and in particular copy constructors and distractors. 47 00:03:48,740 --> 00:03:55,160 So a smart pointer class can be used to manage a pointer so that when it copied the memory it points 48 00:03:55,160 --> 00:03:57,480 to is also copied here. 49 00:03:57,500 --> 00:04:03,470 And the destructor is function that is called automatically when the object goes out of scope. 50 00:04:03,500 --> 00:04:07,820 And so the smart pointer can use this free memory. 51 00:04:08,120 --> 00:04:13,790 So don't worry, smart pointers and distractors will be covered in next lectures.