1 00:00:01,070 --> 00:00:03,150 Allocating memory in code. 2 00:00:03,170 --> 00:00:12,740 So C plus plus defines two operators, the new and the delete operators so that allocate memory from 3 00:00:12,740 --> 00:00:17,330 the free store and release memory back into the free store here. 4 00:00:17,840 --> 00:00:26,300 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,300 --> 00:00:27,080 memory here. 6 00:00:27,080 --> 00:00:28,540 So let's create one here. 7 00:00:28,550 --> 00:00:29,060 P. 8 00:00:33,400 --> 00:00:40,600 As you can see here, the naive operator will call the default constructor for custom types for every 9 00:00:40,600 --> 00:00:41,980 object it creates. 10 00:00:41,980 --> 00:00:45,880 So the but builtin types, as you know, the integer is builtin type. 11 00:00:46,270 --> 00:00:54,340 This builtin type do not have constructors, so instead a type initialization will occur and this will 12 00:00:54,340 --> 00:00:58,120 usually initialize the object to zero. 13 00:00:58,150 --> 00:01:01,060 In this example a zero integer here. 14 00:01:01,060 --> 00:01:08,020 So in general you should not use memory allocated for built in types without explicitly initializing 15 00:01:08,020 --> 00:01:08,430 it. 16 00:01:08,440 --> 00:01:16,990 So in fact in some ideas, for example, visual cplusplus the debug version of the new version will 17 00:01:16,990 --> 00:01:25,120 initialize memory to a value of zero x CD here in Visual Studio. 18 00:01:26,610 --> 00:01:35,550 So but they they will assign this value hex value to for every byte here. 19 00:01:35,670 --> 00:01:42,420 So as a visual reminder in the debugger that you have not initialized the memory in this case here for 20 00:01:42,420 --> 00:01:49,220 custom types, it is left to the author of the type to initialize allocated memory. 21 00:01:49,230 --> 00:01:56,460 But also it's important too that when you have finished with memory that you return it back to the free 22 00:01:56,460 --> 00:01:59,130 store so that the allocator can reuse it. 23 00:01:59,160 --> 00:02:05,940 You do this by calling the delete operator as shown here. 24 00:02:05,970 --> 00:02:07,680 Delete P. 25 00:02:08,910 --> 00:02:15,000 So when you delete a pointer, the destructor for the object is called here. 26 00:02:15,000 --> 00:02:18,210 So this truck. 27 00:02:18,910 --> 00:02:24,150 There is called so for built in types, this does nothing. 28 00:02:24,160 --> 00:02:30,220 So in this case, because we have integer, it will do nothing because it's built in type. 29 00:02:30,220 --> 00:02:36,580 So it's good practice to initialize a pointer to nullptr here. 30 00:02:38,190 --> 00:02:40,200 No beetles shown here. 31 00:02:40,200 --> 00:02:46,890 So after you have deleted it and if you use the conversion of checking the value of pointer before using 32 00:02:46,890 --> 00:02:47,010 it. 33 00:02:47,010 --> 00:02:50,880 So this will protect your from using a deleted pointer. 34 00:02:51,120 --> 00:03:00,750 And the Cplusplus standard says that the delete operator will have no effect if you delete the pointer 35 00:03:00,750 --> 00:03:04,480 that has value of nullptr. 36 00:03:04,530 --> 00:03:13,320 So as I write here, the delete here is just useless because the P has nullptr if we make it like that. 37 00:03:13,320 --> 00:03:18,840 And as you can see here, the value is never used here because we deleted the P here. 38 00:03:19,260 --> 00:03:25,310 Also, cplusplus allows you to initialize a value at the time you call the new operator. 39 00:03:25,320 --> 00:03:27,960 So here there is a two ways to do it here. 40 00:03:27,960 --> 00:03:32,940 The first is p one new integer. 41 00:03:33,620 --> 00:03:37,580 And 42 in braces here. 42 00:03:38,150 --> 00:03:44,750 The second one, the second example is pretty much the same, but you just use another type of braces 43 00:03:44,750 --> 00:03:46,880 here, Curly braces 42. 44 00:03:47,510 --> 00:03:55,450 So you initialize the new operator and initialize the value of 42 with two different types here. 45 00:03:55,460 --> 00:04:00,080 So for a custom type, the new operator will call a constructor on the type. 46 00:04:00,080 --> 00:04:08,150 So but for built in type, the end result is the same and is carried out by initializing the item to 47 00:04:08,150 --> 00:04:09,350 the value provided. 48 00:04:09,350 --> 00:04:17,330 So you can also use initialize list syntax as shown as well as I will show in next lecture. 49 00:04:17,330 --> 00:04:25,280 So it's important to note that um, initialization is the memory pointed to, not the pointer variable.