Introduction to Memory Management in C++
Memory management in C++ is a technique of managing the computer memory and assigning required memory space to the programs for execution. It is almost relatable and is based on the same concept as other Programming languages. It deals with the space and memory assignment in terms of improvisation for the entire computer system and its performance. Arrays play a very vital role in memory management as it helps in storing the data with appropriate spacing alignment, maintaining the timing constraints, and thereby doing efficient resource management by allocating memory using the new keyword.
How does Memory Management work in C++?
- Memory Management is one of the pivotal and important concepts for any programming language so is the case with C++. Concept outline of Memory management primarily revolves around the time-space trade-off and memory allocation concept. It helps in managing the space and memory-related issues with the help of Arrays. Arrays are the data structure that is the main component or can be said that aids memory management concept. How to let’s check the flow. Arrays are the data structure that contains all the data stored in the memory by an efficient allocation of resources with the appropriate time and space as it allocates memory at the time of array declaration and then to avoid the wastage of memory need to use the new operator to allocate memory dynamically at the run-time.
- Sometimes it might happen that the programmer declares an array with the maximum size and then allocates the memory according to the requirement but then in that case there will be some memory space or cells that will remain unused. To avoid such kind of undesirable activity it Is very much needed to keep in mind the memory allocation technique of declaring an array with the new keyword which will take care of this kind of situation.
- Also, any programmer must keep in mind at the time of implementation that how the program flow will be and how the resources can manage the time-space trade efficiently with respect to memory management. Although the memory management operators are there to take care of the memory allocation at the time of runtime but still should be more precautious as it will help them not strive for any kind of memory allocation. If compared with another programming language like C there the Memory Management also happens in a similar fashion just with a minor change in the naming constraint which involves the calloc and malloc functions in C for memory management.
- In C++ memory management, the memory operators make use of the new and delete operators. Unlike C language which makes use of the malloc and calloc functions allocate the memory dynamically at the time of run-time and then makes use of free() function to deallocate the dynamically allocated memory, C++ memory management makes use of the unary operator like new and delete as mentioned earlier to perform the same task of allocating memory and creating free space for the efficient resource allocation.
Memory Management Operators in C++
Memory management is one of the key processing techniques for any programming language which needs to be handled for executing any code base efficiently. It involves certain unary operators for memory management in C++ which are as follows:
- New Operator
- Delete Operator
New Operator
A new operator is used for creating the object which exists and remains in active mode which means the allocation of memory will still be active. This remains in active state i.e. the existence of a new object is there until the delete() operator is called which will be discussed in the next section.
The syntax flow for the new operator with respect to the memory management allocation is as follows:
ptr_var = new data_tp
- ptr_var: This represents the name of the pointer variable.
- new: operator for the creation of the object for allocation.
- data_tp: represents the type of data used while allocation.
Example: This program demonstrates the New Operator which is used for creation of new object for object allocation and memory management as shown in the output.
Code:
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
int no;
cout << "Total_Students_In_One_Batch";
cin >> no;
float* pon_tr;
pon_tr = new float[no];
cout << "Marks_Of_Students" << endl;
for (int k = 0; k < no; ++k)
{
cout << "Student_mrk" << k + 1 << ": ";
cin >> *(pon_tr + k);
}
cout << "\nDisplay the Total marks of Students" << endl;
for (int k = 0; k < no; ++k) {
cout << "Student_mrk" << k + 1 << " :" << *(pon_tr + k) << endl;
}
delete [] pon_tr;
return 0;
}
Output:
Delete Operator
On the other hand, Delete Operator is also a unary operator used for memory management and comes into the picture only when the new operator is used for memory allocation which signifies another fact that the delete operator is fully dependent on the new operator. Once the new operator finishes its work of allocation and tries to free its memory or to remove the unused or excess memory allocated it will immediately call for the Delete Operator.
Syntax:
delete ptr_var;
- delete: This represents the unary operator that needs to be used after calling the new operator.
- ptr_var: This points to the object created by the new unary operator for further deletion.
Example: This program demonstrates the delete unary operator functionality in memory management as shown in the output.
Code:
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
int* p1 = new int;
int* p2 = new int(11);
cout << "display_val_for_p1 = " << *p1 << "\n";
cout << "display_val_for_p2 = " << *p2 << "\n";
delete p1;
delete p2;
return 0;
}
Output:
Conclusion
Memory management in C++ is one of the essential and important concepts of C++ which ensures that memory allocation happens efficiently with the help of memory management operators which involves the unary operator to make the unused memory to be used properly. Wastage of memory is not a good practice for any of the programmers in any programming language.
Recommended Articles
This is a guide to Memory Management in C++. Here we discuss an introduction, how does Memory Management work with examples to implement. You can also go through our other related articles to learn more –