1 00:00:01,320 --> 00:00:10,950 The memory lifetime memory allocated by new keyword will remain valid until you call the delete here. 2 00:00:11,040 --> 00:00:18,180 So this means that you may have memory with long lifetimes and the code may be passed around various 3 00:00:18,180 --> 00:00:20,950 functions in your code. 4 00:00:20,970 --> 00:00:22,590 So here, consider this. 5 00:00:22,590 --> 00:00:26,430 Here let's make P one new integer 42. 6 00:00:27,580 --> 00:00:32,500 Here an integer p to do something. 7 00:00:33,290 --> 00:00:36,320 This is this is the function that we will create. 8 00:00:37,580 --> 00:00:43,850 Here and delete P one and P one equals nullptr. 9 00:00:44,660 --> 00:00:49,280 And here make the do something function here. 10 00:00:50,910 --> 00:00:52,950 Integer Here. 11 00:00:53,580 --> 00:00:55,470 Do something. 12 00:01:01,850 --> 00:01:04,970 And here, return integer. 13 00:01:06,270 --> 00:01:06,660 Here. 14 00:01:07,890 --> 00:01:08,580 Or make. 15 00:01:08,580 --> 00:01:10,140 Let's create a variable here. 16 00:01:10,140 --> 00:01:10,950 Integer. 17 00:01:11,610 --> 00:01:15,000 My Val and my Val. 18 00:01:16,270 --> 00:01:18,370 This returned my Val here. 19 00:01:20,360 --> 00:01:21,110 Here. 20 00:01:21,110 --> 00:01:24,000 As you can see here, we cannot be. 21 00:01:24,020 --> 00:01:25,880 We have to do typecast here. 22 00:01:26,120 --> 00:01:29,890 And, uh, we got to do something here. 23 00:01:29,900 --> 00:01:38,330 And as you can see here, we initialized p one to nullptr, but what about the P two here? 24 00:01:39,170 --> 00:01:46,970 And this code creates a pointer and initializes the memory it points to and then passes the pointer 25 00:01:46,970 --> 00:01:51,790 to a function which itself returns the pointer here. 26 00:01:51,800 --> 00:01:54,230 So since the P one. 27 00:01:55,070 --> 00:02:04,180 Since the p one pointer is no longer needed, it is deleted and assigned to nullptr so that it cannot 28 00:02:04,180 --> 00:02:05,230 be used again. 29 00:02:05,230 --> 00:02:06,850 So this code looks fine. 30 00:02:06,850 --> 00:02:12,280 But the problem is what do you do with the pointer returned by the function? 31 00:02:12,310 --> 00:02:14,280 Imagine here. 32 00:02:14,290 --> 00:02:16,330 Let's change our function. 33 00:02:16,330 --> 00:02:19,780 Now imagine this following function here. 34 00:02:19,810 --> 00:02:20,620 P. 35 00:02:21,070 --> 00:02:21,790 Here. 36 00:02:23,890 --> 00:02:25,270 Uh, equals ten. 37 00:02:27,390 --> 00:02:31,740 Because it's not p integer and return. 38 00:02:31,740 --> 00:02:33,690 P integer. 39 00:02:35,280 --> 00:02:41,340 So in in this code the calling do something. 40 00:02:42,210 --> 00:02:46,840 Actually, we can if we do like this, we're going to get an error. 41 00:02:46,860 --> 00:02:54,120 And that's why we have to call the cast and yeah, yeah, it's okay to use this here before the function. 42 00:02:54,120 --> 00:03:01,740 So this calling do something creates a copy of a pointer, but not copy of what it points to. 43 00:03:01,740 --> 00:03:06,900 So this means that when p one pointer is deleted here. 44 00:03:08,470 --> 00:03:11,650 The memory it points to is no longer available. 45 00:03:11,650 --> 00:03:15,940 And so the pointer p to here. 46 00:03:16,210 --> 00:03:19,450 Points to the invalid memory. 47 00:03:19,540 --> 00:03:26,740 So this problem can be addressed using a mechanism called resource acquisition initialization. 48 00:03:27,670 --> 00:03:36,910 This means error i e here, which means using the features of C plus plus objects to manage resources. 49 00:03:36,910 --> 00:03:46,690 So here I'm going to make it here, error here in C plus plus it needs classes and in particular copy 50 00:03:46,690 --> 00:03:48,610 constructors and destructors. 51 00:03:48,610 --> 00:03:55,090 So a smart pointer class can be used to manage a pointer so that when it copied, the memory it points 52 00:03:55,090 --> 00:03:57,400 to is also copied here. 53 00:03:57,400 --> 00:04:03,370 And the destructor is function that is called automatically when the object goes out of scope. 54 00:04:03,370 --> 00:04:08,030 And so the smart pointer can use this free memory. 55 00:04:08,030 --> 00:04:13,790 So don't worry, smart pointers and destructors will be covered in next lectures.