WEBVTT

00:01.070 --> 00:03.150
Allocating memory in code.

00:03.170 --> 00:12.740
So C plus plus defines two operators, the new and the delete operators so that allocate memory from

00:12.740 --> 00:17.330
the free store and release memory back into the free store here.

00:17.840 --> 00:26.300
So the new operator is used with the type to allocate memory and it will return a type pointer to the

00:26.300 --> 00:27.080
memory here.

00:27.080 --> 00:28.540
So let's create one here.

00:28.550 --> 00:29.060
P.

00:33.400 --> 00:40.600
As you can see here, the naive operator will call the default constructor for custom types for every

00:40.600 --> 00:41.980
object it creates.

00:41.980 --> 00:45.880
So the but builtin types, as you know, the integer is builtin type.

00:46.270 --> 00:54.340
This builtin type do not have constructors, so instead a type initialization will occur and this will

00:54.340 --> 00:58.120
usually initialize the object to zero.

00:58.150 --> 01:01.060
In this example a zero integer here.

01:01.060 --> 01:08.020
So in general you should not use memory allocated for built in types without explicitly initializing

01:08.020 --> 01:08.430
it.

01:08.440 --> 01:16.990
So in fact in some ideas, for example, visual cplusplus the debug version of the new version will

01:16.990 --> 01:25.120
initialize memory to a value of zero x CD here in Visual Studio.

01:26.610 --> 01:35.550
So but they they will assign this value hex value to for every byte here.

01:35.670 --> 01:42.420
So as a visual reminder in the debugger that you have not initialized the memory in this case here for

01:42.420 --> 01:49.220
custom types, it is left to the author of the type to initialize allocated memory.

01:49.230 --> 01:56.460
But also it's important too that when you have finished with memory that you return it back to the free

01:56.460 --> 01:59.130
store so that the allocator can reuse it.

01:59.160 --> 02:05.940
You do this by calling the delete operator as shown here.

02:05.970 --> 02:07.680
Delete P.

02:08.910 --> 02:15.000
So when you delete a pointer, the destructor for the object is called here.

02:15.000 --> 02:18.210
So this truck.

02:18.910 --> 02:24.150
There is called so for built in types, this does nothing.

02:24.160 --> 02:30.220
So in this case, because we have integer, it will do nothing because it's built in type.

02:30.220 --> 02:36.580
So it's good practice to initialize a pointer to nullptr here.

02:38.190 --> 02:40.200
No beetles shown here.

02:40.200 --> 02:46.890
So after you have deleted it and if you use the conversion of checking the value of pointer before using

02:46.890 --> 02:47.010
it.

02:47.010 --> 02:50.880
So this will protect your from using a deleted pointer.

02:51.120 --> 03:00.750
And the Cplusplus standard says that the delete operator will have no effect if you delete the pointer

03:00.750 --> 03:04.480
that has value of nullptr.

03:04.530 --> 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.

03:13.320 --> 03:18.840
And as you can see here, the value is never used here because we deleted the P here.

03:19.260 --> 03:25.310
Also, cplusplus allows you to initialize a value at the time you call the new operator.

03:25.320 --> 03:27.960
So here there is a two ways to do it here.

03:27.960 --> 03:32.940
The first is p one new integer.

03:33.620 --> 03:37.580
And 42 in braces here.

03:38.150 --> 03:44.750
The second one, the second example is pretty much the same, but you just use another type of braces

03:44.750 --> 03:46.880
here, Curly braces 42.

03:47.510 --> 03:55.450
So you initialize the new operator and initialize the value of 42 with two different types here.

03:55.460 --> 04:00.080
So for a custom type, the new operator will call a constructor on the type.

04:00.080 --> 04:08.150
So but for built in type, the end result is the same and is carried out by initializing the item to

04:08.150 --> 04:09.350
the value provided.

04:09.350 --> 04:17.330
So you can also use initialize list syntax as shown as well as I will show in next lecture.

04:17.330 --> 04:25.280
So it's important to note that um, initialization is the memory pointed to, not the pointer variable.
