Allocating memory in code.
So C plus plus defines two operators, the new and the delete operators so that allocate memory from
the free store and release memory back into the free store here.
So the new operator is used with the type to allocate memory and it will return a type pointer to the
memory here.
So let's create one here.
P.
As you can see here, the naive operator will call the default constructor for custom types for every
object it creates.
So the but builtin types, as you know, the integer is builtin type.
This builtin type do not have constructors, so instead a type initialization will occur and this will
usually initialize the object to zero.
In this example a zero integer here.
So in general you should not use memory allocated for built in types without explicitly initializing
it.
So in fact in some ideas, for example, visual cplusplus the debug version of the new version will
initialize memory to a value of zero x CD here in Visual Studio.
So but they they will assign this value hex value to for every byte here.
So as a visual reminder in the debugger that you have not initialized the memory in this case here for
custom types, it is left to the author of the type to initialize allocated memory.
But also it's important too that when you have finished with memory that you return it back to the free
store so that the allocator can reuse it.
You do this by calling the delete operator as shown here.
Delete P.
So when you delete a pointer, the destructor for the object is called here.
So this truck.
There is called so for built in types, this does nothing.
So in this case, because we have integer, it will do nothing because it's built in type.
So it's good practice to initialize a pointer to nullptr here.
No beetles shown here.
So after you have deleted it and if you use the conversion of checking the value of pointer before using
it.
So this will protect your from using a deleted pointer.
And the Cplusplus standard says that the delete operator will have no effect if you delete the pointer
that has value of nullptr.
So as I write here, the delete here is just useless because the P has nullptr if we make it like that.
And as you can see here, the value is never used here because we deleted the P here.
Also, cplusplus allows you to initialize a value at the time you call the new operator.
So here there is a two ways to do it here.
The first is p one new integer.
And 42 in braces here.
The second one, the second example is pretty much the same, but you just use another type of braces
here, Curly braces 42.
So you initialize the new operator and initialize the value of 42 with two different types here.
So for a custom type, the new operator will call a constructor on the type.
So but for built in type, the end result is the same and is carried out by initializing the item to
the value provided.
So you can also use initialize list syntax as shown as well as I will show in next lecture.
So it's important to note that um, initialization is the memory pointed to, not the pointer variable.