In C++, we can efficiently allocate the memory at runtime and deallocate it when not required. With this feature, we get the flexibility of allocation and deallocation of memory as per requirement. In this article on C++ memory management, we will learn about the dynamic allocation of memory and understand its working in detail.
What Is C++ Memory Management?
Memory management can be defined as a process in which management of a computer’s memory occurs, for example assigning memory to programs, variables etc., in such a way that it doesn’t affect the overall performance. Sometimes, the computer’s data can range up to terabytes, so efficient use of memory is necessary to minimize memory wastage and boost up performance.
Why Do We Need Memory Management, and How Does It Work?
Memory management is required to ensure that there is no wastage of memory and that allocation takes place efficiently. The memory that a C++ program uses is divided into different parts. Here, we will discuss two, i.e. stack and heap.
- Stack: In stack, all the variables that are declared inside the function and other information related to the function are stored.
- Heap: Heap is unused memory and the part from where the memory is allocated dynamically when the program runs.
During the declaration of an array, there are times when the correct memory is not determined until runtime. To avoid such scenarios, we usually declare an array of the maximum size. However, because of this full size, some memory remains unused. For example, let us suppose we have declared an array of size 30, and after declaring the array, it turns out that we only need space of 10 size, so the rest of the space is of no use, or we can say it will get wasted.
To avoid such cases, we use memory allocation. We can allocate the memory at runtime from the heap using an operator.
Allocation and Deallocation of Memory
Other programming languages like java, python, etc., don’t need to allocate memory dynamically. In C language, we use the malloc() or calloc() functions to allocate the memory dynamically at run time, and C++ also supports these functions. But, in C++, allocation and deallocation are done manually.
In C++, two operators are used for the allocation and deallocation of memory i.e
- new operator
- delete operator
So, let’s understand the purpose of these two operators.
C++ New Operator
Using the C++ new operator, we can allocate memory at the runtime. The new operator in C++ is used for the dynamic memory allocation; It is used to allocate the memory at runtime on heap memory.
Here, ptr is the pointer of type integer. It is equal to new which is the operator, and int is the data type of the pointer variable.
Now, let’s learn about the delete operator.
C++ Delete Operator
Once the memory is allocated, we can delete the memory when it is not required anymore. The delete operator in C++ is used for the deallocation of memory.
When we no longer need to use the variable, that means when the memory is no longer required, we have to deallocate or release the memory using the delete operator.
Here, delete is the operator, and ptr is the pointer variable.
Now, let’s look at an example where we will be using both of these operators, and find the average of three numbers entered by the user.
Here, in this example, we have declared three pointer variables and a normal variable avg. After the declaration, we allocated the memory dynamically using the new keyword for all three pointer variables.
Then, we will take the input from the user of those three numbers of which they want to find the average. Once the user has entered the numbers, we will find the average of those numbers using the formula avg = (*ptr1+*ptr2+*ptr3)/3. We are using the dereference operator with the pointer because it points to the value of the variable.
After that, we will display the avg variable in which the result is stored. Then, we will delete the memory space that has been allocated or deallocate the memory using the delete operator.
Shown below is the output.
New and Delete Operator for Arrays
New and delete operators can also be used for dynamic memory allocation in arrays. The syntax of new and delete operators for arrays is similar to that of the variables.
Now, let’s see an example where we can learn how to use the new and delete operator for arrays. In this example, the user will enter some elements and then we will display those array elements.
In this example, we will take the size of the array as an input from the user; the number of elements inside the array is equal to the size of the array. Then, we will declare the pointer variable, and allocate the memory for the complete array.
Once the memory allocation is done, we will take the array elements as input from the user.
After that, we will display those elements using the for loop, i.e., iterating from 0 to num. Once the elements are printed, we will deallocate the memory using the delete operator.
Shown below is the output.
Advantages of New Operator Over Malloc Function
- The new operator can be overloaded, whereas the malloc function cannot.
- If there is not enough memory, the new operator throws an exception, whereas the malloc function returns null.
- The return type of the new operator is of the same type for which the memory was allocated. We don't need to do typecasting.
- The new operator automatically computes the size of the data object. We don't need to use the sizeof() operator.
- The new operator allows us to initialize objects while creating memory space for them.
Advance your career as a MEAN stack developer with the Full Stack Web Developer - MEAN Stack Master's Program. Enroll now!
In this tutorial on C++ memory management, we discussed why we need memory management and how memory management works. You also learned about new and delete operators and the advantages of new operator over the malloc function, along with some examples. To learn more about C++ Memory management, click on the following link: C++ Memory management
If you are looking to build a career in software development, check the Post Graduate Program in Full Stack Development by Simplilearn.
If you have any questions or doubts regarding this tutorial on C++ memory management, put them in the comments section. Our experts will help you solve your queries. To learn more about C++ Memory management, click on the following link: C++ Memory management