Introduction
Programming languages like C,C++, C# etc., are used to write programming instructions to get desired output. These instructions uphold computer memory space. Instructions are written to operate on feeded information resultant of which is stored in memory. This resultant is further worked on to get desired output, this output obtained is again locked back in memory. Thus, every manipulation of information/programming instructions in the world of algorithmics requires memory.
In C++, all identifiers consume memory as defined in the compiler or as per the grammar of the language. For example, integer data type in c++ requires 4 bytes of memory capable of storing values from -2147483648 to 2147483647. To be more precise 4 bytes of memory means 32 bit size. Each bit can store 0 or 1.
To store integer 5 in memory 3 bits are used and 1 bit for sign (positive or negative) in all 4 bits are required. But C++ compiler will allocate 32 bits to store integer 5. As you can understand 4 bits are used, the remaining 28 bits are wastage.
To overcome this memory wastage Dynamic memory allocation in C++ is used.
Dynamic Memory allocation in C++ with example program
To overcome memory wastage, a good approach is to use memory as per the requirement. This is possible in static programming when the program and its output is pre-decided and it is not going to change for a long period of time.
But in static programming also identifiers are used and scenarios are developed in which memory allocated to identifiers is not used up to its maximum limit.
In case of dynamic programming, information grows and shrinks as per the desired task so it becomes necessary to allocate and use memory dynamically.
Each programming language has its own grammar to implement dynamic memory allocation. In c++, this is achieved by allocating memory at run time or at execution time and defined as Dynamic Memory Allocation.
Mechanics of Dynamic Memory Allocation
In the grammar of C++ a special operator is defined to allocate memory at run time. When used, this operator returns the address at which value of a variable or information will be stored. Makers of C++ grammar have termed this operator as new.
To enhance the effectiveness of Dynamic memory allocation, it is necessary to maintain a record of total memory in use and how much amount of memory is becoming obsolete which was allocated dynamically. Memory which is no longer in use must be made free in C + +; this is done using the delete operator. “delete” operator de-allocates memory allocated by a “new” operator.
Syntax of new operator:
new data-type;
data-type can be a built-in data type, an array or a class or a structure.
Syntax of delete operator:
delete pointer_variable;
pointer_variable is a pointer to the dynamically allocated variable.
Dynamic memory allocation performs their operation using pointers. Examples of dynamic memory allocation for built in datatypes, an array, a class or a structure are as follows:
Example of dynamic memory allocation to integer variables or any other built-in data type at run time.
#include <iostream> using namespace std; int main ( ) { int *ptr = new int; *ptr = 4; cout<<*ptr<<endl; delete ptr; return 0; }
Dynamic memory allocation for arrays:
#include <iostream> using namespace std; int main ( ) { int no_of_subjects, i, sum=0; std::cout<<"For how many subjects you want to enter marks ??"<<std::endl; std::cin>>no_of_subjects; int *marks = new int[no_of_subjects]; std::cout<<"Enter the marks of subjects"<<std::endl; for(i=0; i<no_of_subjects; i++) { std::cin>>*(marks+i); } for(i=0;i<no_of_subjects;i++) { sum += *(marks+i); } std::cout<<"sum is"<<sum<<std::endl; delete[] marks; return 0; }
Dynamic memory allocation for Objects
#include <iostream> using namespace std; class Student { int roll; public: Student() { cout<<"\nConstructor called"<<endl; cout<<"enter roll number of student"; cin>>roll; } void disp ( ) { cout<<"Roll number of student is :"; cout<<"\nRoll_number:"<<roll; } }; int main ( ) { Student *ptr = new Student(); ptr->disp(); delete ptr; return 0; }
Dynamic memory allocation for Structure
#include <iostream> using namespace std; struct emp { string name; }; int main() { int i; emp *e; e = new emp[3]; e[0].name="Ajaya"; e[1].name="Vijay"; e[2].name="Sanjay"; cout<<"Displaying Names of employee"<<endl; for( i=0; i<3; i++) { cout<<"Names:"<<e[i].name<<endl; } delete[ ] e; return 0; }
Dynamic Memory Allocation ethics
Following ethics must be considered in the use of Dynamic Memory Allocation:
- Memory exhaustion
- Ability to know initial conditions
- Use of semaphores
- Memory fragmentation
- Understanding of working of stack and heap data structures
Following are the dependencies of Dynamic Memory Allocation
The Operating System
Memory management techniques are taken care of by the operating system in use, thus to achieve maximum efficiency operating system selection is a critical issue in association with the hardware configuration of the computer system.
Compiler in use:
Compilers of C++ decide the implementation of Dynamic Memory Allocation in use, as the compiler has to talk to the Operating System. If the grammar of the compiler is not able to synchronize with memory management techniques available in the operating system then it directly affects run time complexity of developed programs.
Achieving wisdom in the use of C++ library
To use optimized memory management techniques, programmers must possess critical research skills to achieve sophisticated implementation. Sophisticated implementation is possible when task oriented libraries are developed.
STL in use
STL is used to manage and manipulate collections of objects (blocks of memory).
For example, STL <memory> defines templates to be used in allocation and deallocation of objects. It also has pointer templates to be used in dynamic memory allocation. It supports allocator class to work on heap and garbage collection.
It is in this library that operator new is defined which returns a pointer.
Leave a Reply