Dynamic memory allocation/de-allocation
In
the C++ programming language, there are two ways that memory gets allocated for
data storage:
1. Static
Memory Allocation
The memory for
defined variables is allocated by the compiler itself. The exact size and data type
of variable to store must be known at the time of compilation. Therefore, standard
array declarations need the constant size.
2. Dynamic
Memory Allocation
The memory allocated for the variables
during the run time. The dynamically allocated space usually placed in a
program segment known as the heap or the free store. In this allocation exact
amount of space or number of items does not have to be known by the compiler in
advance. Therefore, for dynamic memory allocation, pointers are crucial.
We
can dynamically allocate storage space while the program is running, but we
cannot create new variable names during the execution.
Therefore
to implement the concept of dynamic allocation, it requires two steps:
1. Creating
the dynamic space.
2. Storing
its address in a pointer (so that the space can be accessed)
In
the C++, new
operator is used to create the dynamic memory space.
De-allocation is
the process to clean-up the space being used for variables or other data
storage. The compile time variables are automatically de-allocated based on
their known extent (this is the same as scope for "automatic"
variables). It is the programmer's job to de-allocate dynamically created space,
to de-allocate dynamic memory space the delete
operator is available in C++ programming language.
6.5.1
new and delete operators
In
the C++, for allocation and de-allocation of memory dynamically, new and delete operators
are used.
To
allocate space dynamically, use the unary operator new, followed by the type
being allocated.
new int;
// dynamically allocates an int
new double;
// dynamically allocates a double
These
above statements are not very useful by themselves, because the allocated
spaces have no names, and the new operator returns the starting address
of the allocated space, and this address can be stored in a pointer:
int * p;
// declare a pointer p
p = new int;
// dynamically allocate an int and load address into p
double * d;
// declare a pointer d
d = new double; // dynamically allocate a
double and load address into d
Consider
the following example for dynamically allocation and de-allocation of the
variables.
Example
6.4 : Program to allocate and de-allocate the dynamic space using pointers.
#include
< iostream.h >
void
main()
{
int *p1;
p1 = new int; //
dynamic memory allocation
*p1 = 300;
cout << “\n Value inside the p1 = ” << *p1;
delete p1; //free
the dynamically allocated memorys
}
OUTPUT
Value
inside the p1 = 300
Using
the new and delete operators, we can create arrays at runtime by dynamic memory
allocation and de-allocation. The general form for doing this is:
p_var = new array_type[size];
size
specifies the number of elements in the array.
To
free the memory of an array we use:
delete[] p_var;
Let
see the given example for the above explanation.
Example
6.5 : Program to create dynamic array using pointers.
#include
< iostream.h >
void
main()
{
int *p;
p = new int[10]; //
dynamic allocation for array of 10 elements
for(int i = 0; i < 10; i++)
p[i] = i;
cout << “\n content of array are : ” << endl;
for(i = 0; i < 10; i++)
cout << p[i] << endl;
delete[] p; //free the array memories
}
OUTPUT
content
of array are :
0
1
2
3
4
5
6
7
8
9
No comments:
Post a Comment