PREV NEXT
C++ Dynamic Memory Allocation:
What is memory allocation?
The memory in C++ can be allocated in two ways:
Static Memory Allocation:
In static memory allocation the memory is allocated for the named variables by the compiler. The size and data type of the storage must be known at the compile time.
Dynamic Memory Allocation:
In the dynamic memory allocation the memory is allocated during run time. The space which is allocated dynamically usually placed in a program segment which is known as heap. In this, the compiler does not need to know the size in advance.
What is Dynamic Memory Allocation?
- In C++, dynamic memory allocation means performing memory allocation manually by programmer. It is allocated on the heap and the heap is the region of a computer memory which is managed by the programmer using pointers to access the memory.
- The programmers can dynamically allocate storage space while the program is running but they cannot create a new variable name.
Dynamic memory allocation requires two criteria:
- Creating the dynamic space in memory.
- Storing its address in a pointer
What is the use of Dynamic Memory Allocation?
- Dynamic Memory Allocation is to allocate memory of variable size which is not possible with compiler allocated memory except variable length arrays.
- The most important use of dynamic memory allocation is the flexibility as the programmers are free to allocate and deallocate memory whenever we need and when we don’t.
How memory is allocated and deallocated in C++?
In C++, we can allocate and deallocate memory by using two operators new and delete operator respectively which perform the task of allocating and deallocating the memory. Let us have a discussion on these two operators.
New Operator
The new operator signifies a request for memory allocation on the heap and if the sufficient memory is available then the new operator initializes the memory and returns the address of the newly allocated variable and initialized memory to the pointer variable.
Syntax:
pointer-variable = new data type;
Here, the pointer-variable is the pointer of type data type. Data type could be any built in data type including array or any user defined data type including class and structure.
Delete Operator
The delete operator is used to deallocate the memory created by new operator at runtime. Once the memory is no longer needed it should be free so that the memory becomes available again for other request of dynamic memory.
Syntax:
delete pointer-variable;
In the delete operator the pointer-variable is the pointer that points to the data object created by the new.
Let us have a look at the example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
#include <iostream> using namespace std; int main () { // Pointer initialization to null int* ptr = NULL; // using new operator ptr = new(nothrow) int; if (!ptr) cout << "No Space\n"; else { // value stored at allocated address *ptr = 10; cout << "Value of ptr: " << *ptr << endl; } // using new operator float *r = new float(50.20); cout << "Value of r: " << *r << endl; // Request block of memory of size a int a = 7; int *q = new(nothrow) int[a]; if (!q) cout << "allocation of memory failed\n"; else { for (int i = 0; i < a; i++) q[i] = i+1; cout << "Value store in block of memory: "; for (int i = 0; i < a; i++) cout << q[i] << " "; } // freed the allocated memory delete ptr; delete r; // freed the block of allocated memory delete[] q; return 0; } |