Dynamic Memory Allocation in C++
Understanding
of Dynamic memory allocation is very important for a programmer. It enables the
programmer to make optimal use of the memory available. Let’s understand memory
allocation from scratch.
A typical memory representation of a C program
consists of following sections: text segment, initialized data segment,
uninitialized data segment, stack and heap. We will not go into the details of
the first three sections. However, we will have a look at stack and heap in
detail.
In C/C++
memory can be allocated based on either stack or heap. Memory from the stack is
occupied by variables declared inside the function. The heap is the unused part
of the memory of a program which can be used to allocate memory dynamically during
runtime. The differences between them have been enlisted below.
Stack
|
Heap
|
Memory is allocated in
contiguous block.
|
Memory
is allocated in a random order
|
Allocation and De-allocation
is done automatically by the compiler.
|
Allocation
and De-allocation is done manually by the programmer.
|
There is no flexibility as
memory size is fixed.
|
More
flexible as resizing is possible
|
Access time is faster.
|
Access
time is slower.
|
Shortage of memory is the
main problem.
|
Memory
Fragmentation is the main issue
|
Handling of stack frame is
less costly.
|
Handling
of heap frame is more costly.
|
Implementation is hard.
|
Implementation
is easy.
|
Need for Dynamic Memory Allocation
Many times
we are not aware as to how much memory will be required to store the
information and the size of the exact memory can be determined only at runtime.
The memory can be allocated in the heap for a variable of desired data type
using a special operator. The new
operator returns the address of the memory location allocated. Additionally,
there is also a delete operator
which can be used to free up any memory which is not required. Thus, there is a
lot of flexibility for the programmer as memory can be allocated and
deallocated as and when the programmer wants. This flexibility helps while
working with linked lists, tree, etc.
For normal
variables, the memory is allocated and deallocated automatically. However, for
dynamically allocated memory, it is the programmer’s duty to deallocate the memory
when the memory is not needed. If this is not done, memory is not deallocated
until program terminates and causes memory leak. Programs like daemons and
servers which never terminate, memory leaks can cause serious issues. In order
to avoid this issue, memory allocated on the heap during runtime must always be
deallocated by the programmer without fail.
In C,
malloc() and free() functions are used to allocate and deallocate memory
dynamically during runtime. In C++, these functions are present but it is
better to use new and delete operators instead of malloc() and free()
functions.
Although new and delete operators are slower than malloc() and free()
function respectively, there are certain advantages of using new and delete
operators :
- ·
new
and delete operators can be overloaded
- ·
no
need to calculate size of memory using sizeof() in case of new operator
- ·
new
throws an exception of the type std::bad_alloc whereas malloc() returns a NULL
pointer. However if “nothrow” is used, new returns a NULL pointer
- ·
new
returns a proper typed pointer whereas malloc() returns a void pointer which
has to be typecasted
Consider the following C++ program to illustrate dynamic allocation and
deallocation of memory using new and delete
Code credits : GeeksforGeeks
#include
<iostream>
using namespace std;
int main ()
{
// Pointer initialization to null
int* p = NULL;
// Request memory for the variable
// using new operator
p = new(nothrow) int;
if (!p)
cout <<
"allocation of memory failed\n";
else
{
// Store value at
allocated address
*p = 29;
cout << "Value
of p: " << *p << endl;
}
// Request block of memory
// using new operator
float *r = new float(75.25);
cout << "Value of r: "
<< *r << endl;
// Request block of memory of size n
int n = 5;
int *q = new(nothrow) int[n];
if (!q)
cout <<
"allocation of memory failed\n";
else
{
for (int i = 0; i < n;
i++)
q[i] =
i+1;
cout << "Value
store in block of memory: ";
for (int i = 0; i < n;
i++)
cout
<< q[i] << " ";
}
// freed the allocated memory
delete p;
delete r;
// freed the block of allocated memory
delete[] q;
return 0;
}
|
Output
-Sarvesh
Patki
TY K - 72
References-
[1] GeeksforGeeks https://www.geeksforgeeks.org/
Understood the concept really well!
ReplyDeleteI am glad that it helped you :)
DeleteVery well written and presented!
ReplyDeleteAnushka Bhave : Really useful!
ReplyDeleteThank you
DeleteQuite informative and helpful!
ReplyDeleteI'm glad that it helped you.
DeleteThanks
Delete