1 00:00:01,110 --> 00:00:03,270 Allocating memory in code. 2 00:00:03,270 --> 00:00:13,650 So C++ defines to operators the new and the delete operators so that allocate memory from the free store 3 00:00:13,650 --> 00:00:17,310 and release memory back into the free store here. 4 00:00:18,030 --> 00:00:26,370 So the new operator is used with the type to allocate memory and it will return a type pointer to the 5 00:00:26,370 --> 00:00:27,130 memory here. 6 00:00:27,150 --> 00:00:29,340 So let's create an here p. 7 00:00:33,530 --> 00:00:40,670 As you can see here, the naive operator will call the default constructor for custom types for every 8 00:00:40,670 --> 00:00:42,050 object it creates. 9 00:00:42,050 --> 00:00:49,280 So the built in types, as you know, the integer is built and type this boolean type to not have constructors. 10 00:00:49,280 --> 00:00:58,210 So instead a type initialization will occur and this will usually initialize the object to zero. 11 00:00:58,220 --> 00:01:01,120 In this example, a zero integer here. 12 00:01:01,130 --> 00:01:08,090 So in general you should not use memory allocated for built in types without explicitly initializing 13 00:01:08,090 --> 00:01:08,540 it. 14 00:01:08,540 --> 00:01:17,630 So in fact, in some ideas, for example, visual C++, the debug version of the new version will initialize 15 00:01:17,630 --> 00:01:25,220 memory to a value of zero x CD here in Visual Studio. 16 00:01:26,770 --> 00:01:35,530 So but they they will assign this one hex value to for every byte here. 17 00:01:35,890 --> 00:01:42,490 So as a visual reminder in the debugger that you have not initialize the memory in this case here for 18 00:01:42,490 --> 00:01:49,150 custom types, it is left to the author of the type to initialize allocated memory. 19 00:01:49,390 --> 00:01:56,500 But also it's important to that when you have finished with memory that you return it back to the free 20 00:01:56,500 --> 00:01:59,230 store so that the allocator can reuse it. 21 00:01:59,260 --> 00:02:05,980 You do this by calling the delete operator as shown here. 22 00:02:06,010 --> 00:02:07,900 Delete P. 23 00:02:09,130 --> 00:02:15,060 So when you delete a pointer, the destructor for the object is called here. 24 00:02:15,070 --> 00:02:18,280 So this track. 25 00:02:18,920 --> 00:02:20,930 Third is called. 26 00:02:20,930 --> 00:02:24,250 So for ten types, this does nothing. 27 00:02:24,260 --> 00:02:30,170 So in this case, because we have integer, it will do nothing because it's built in type. 28 00:02:30,350 --> 00:02:34,810 So it's good practice to initialize a pointer to null. 29 00:02:34,820 --> 00:02:36,620 Peter here. 30 00:02:38,260 --> 00:02:40,240 No Peter shown here. 31 00:02:40,330 --> 00:02:46,990 So after you have deleted it and if you use the conversion of checking the value of pointer before using 32 00:02:46,990 --> 00:02:47,080 it. 33 00:02:47,080 --> 00:02:50,890 So this will protect your from using a deleted pointer. 34 00:02:51,340 --> 00:03:01,030 And the C++ standard says that the delete operator will have no effect if you delete a pointer that 35 00:03:01,030 --> 00:03:03,790 has value of null. 36 00:03:03,820 --> 00:03:10,940 Peter So as I write here, the delete here is just useless because the P has no. 37 00:03:10,960 --> 00:03:18,010 Peter If you make it like that and as you can see here, the value is never used here because we deleted 38 00:03:18,010 --> 00:03:18,850 the P here. 39 00:03:19,360 --> 00:03:25,350 Also, C++ allows you to initialize a value at the time you call the new operator. 40 00:03:25,360 --> 00:03:28,030 So here there is a two ways to do it here. 41 00:03:28,030 --> 00:03:32,890 The first is P one new integer. 42 00:03:33,690 --> 00:03:37,590 And 42 in braces here. 43 00:03:38,400 --> 00:03:44,820 The second one, the second example is pretty much the same, but you just use another type of braces 44 00:03:44,820 --> 00:03:46,890 here called Braces 42. 45 00:03:47,880 --> 00:03:55,410 So you initialize the new operator and initialize the value of 42 with two different types here. 46 00:03:55,620 --> 00:04:00,120 So for a custom type, the new operator will call a constructor on the type. 47 00:04:00,120 --> 00:04:02,140 So but for built in type. 48 00:04:02,160 --> 00:04:09,400 The end result is the same and is carried out by initializing the item to the value provided. 49 00:04:09,420 --> 00:04:17,380 So you can also use initialized list syntax as show as will as I will show in next lecture. 50 00:04:17,400 --> 00:04:25,290 So it's important to note that the initialization is the memory pointed to, not the pointer variable.