01 03 Memory
01 03 Memory
C++ also supports these functions, but C++ also defines unary operators such
as new and delete to perform the same tasks, i.e., allocating and freeing the
memory.
New operator
A new operator is used to create the object while a delete operator is used to delete
the object. When the object is created by using the new operator, then the object will
exist until we explicitly use the delete operator to delete the object. Therefore, we can
say that the lifetime of the object is not related to the block structure of the program.
Syntax
The above syntax is used to create the object using the new operator.
Example 1:
1. int *p;
2. p = new int;
Example 2:
1. float *q;
2. q = new float;
In the above case, the declaration of pointers and their assignments are done
separately. We can also combine these two statements as follows:
o We can assign the value to the newly created object by simply using the assignment
operator. In the above case, we have created two pointers 'p' and 'q' of type int and
float, respectively. Now, we assign the values as follows:
1. *p = 45;
2. *q = 9.8;
We assign 45 to the newly created int object and 9.8 to the newly created float object.
o We can also assign the values by using new operator which can be done as follows:
In the above statement, we have created an array of type int having a size equal to 8
where p[0] refers first element, p[1] refers the first element, and so on.
Delete operator
When memory is no longer required, then it needs to be deallocated so that the
memory can be used for another purpose. This can be achieved by using the delete
operator, as shown below:
1. delete pointer_variable;
In the above statement, 'delete' is the operator used to delete the existing object,
and 'pointer_variable' is the name of the pointer variable.
In the previous case, we have created two pointers 'p' and 'q' by using the new
operator, and can be deleted by using the following statements:
1. delete p;
2. delete q;
The dynamically allocated array can also be removed from the memory space by
using the following syntax:
1. delete [ ] pointer_variable;
Both the malloc() and new in C++ are used for the same purpose.
They are used for allocating memory at the runtime.
But, malloc() and new have different syntax.
malloc() is a standard library new is an operator
function that is predefined in
a stdlib header file
What is new?
What is malloc()?
Dangling pointer is a pointer pointing to a memory location that has been freed (or
deleted).
free() function
Delete operator
Syntax
If we allocate the memory to the pointer using the new operator, and now we want to
delete it.
It is either used to delete the array or non-array objects which are allocated by
using the new keyword.
The delete is an operator that de- the free() is a function that destroys the
allocates the memory dynamically memory at the runtime
The delete operator is used to delete the free() function is used to delete the
the pointer, which is either allocated pointer that is either allocated using
using new operator or a NULL pointer malloc(), calloc() or realloc() function or
NULL pointer.
the delete operator destroys the the free() function does not call the
allocated memory, then it calls the destructor; it only frees the memory from
destructor of the class in C++, the heap.