Allocating memory in code.
So C++ defines to 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 an 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 built in types, as you know, the integer is built and type this boolean type to 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 C++, 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 one hex value to for every byte here.
So as a visual reminder in the debugger that you have not initialize 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 to 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 track.
Third is called.
So for ten 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 null.
Peter here.
No Peter 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 C++ standard says that the delete operator will have no effect if you delete a pointer that
has value of null.
Peter So as I write here, the delete here is just useless because the P has no.
Peter If you make it like that and as you can see here, the value is never used here because we deleted
the P here.
Also, C++ 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 called 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 initialized list syntax as show as will as I will show in next lecture.
So it's important to note that the initialization is the memory pointed to, not the pointer variable.