All You Need to Know About C++ Memory Management

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.

Want a Top Software Development Job? Start Here!

Full Stack Developer - MERN StackExplore Program
Want a Top Software Development Job? Start Here!

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.

Want a Top Software Development Job? Start Here!

Full Stack Developer - MERN StackExplore Program
Want a Top Software Development Job? Start Here!

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.

Syntax: 

CPP_memory_management_Example2

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.

Syntax:          

CPP_memory_management_Example3.

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.

CPP_memory_management_Example4

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.

CPP_memory_management_Example7

Want a Top Software Development Job? Start Here!

Full Stack Developer - MERN StackExplore Program
Want a Top Software Development Job? Start Here!

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.

Syntax:

CPP_memory_management_Example6

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. 

CPP_memory_management_Example5

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.

CPP_memory_management_Example8

Want a Top Software Development Job? Start Here!

Full Stack Developer - MERN StackExplore Program
Want a Top Software Development Job? Start Here!

Advantages of New Operator Over Malloc Function

  1. The new operator can be overloaded, whereas the malloc function cannot.
  2. If there is not enough memory, the new operator throws an exception, whereas the malloc function returns null.
  3. 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.
  4. The new operator automatically computes the size of the data object. We don't need to use the sizeof() operator.
  5. The new operator allows us to initialize objects while creating memory space for them.

Conclusion

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

About the Author

Ravikiran A SRavikiran A S

Ravikiran A S works with Simplilearn as a Research Analyst. He an enthusiastic geek always in the hunt to learn the latest technologies. He is proficient with Java Programming Language, Big Data, and powerful Big Data Frameworks like Apache Hadoop and Apache Spark.

View More
  • Disclaimer
  • PMP, PMI, PMBOK, CAPM, PgMP, PfMP, ACP, PBA, RMP, SP, and OPM3 are registered marks of the Project Management Institute, Inc.