Dynamic Memory Allocation
Dynamic memory allocation in C/C++ refers to performing memory allocation manually by programmer. Dynamically allocated memory is allocated on Heap and non-static and local variables get memory allocated on Stack.
Applications of Dynamic Memory Allocation
- One use of dynamically allocated memory is to allocate memory of variable size which is not possible with compiler allocated memory except variable length array.
- The most important use is flexibility provided to programmers. We are free to allocate and deallocate memory whenever we need and whenever we don’t need anymore. There are many cases where this flexibility helps. Examples of such cases are Linked List, Tree, etc.
Memory Allocation in C++
C++ supports malloc() and calloc() functions of C language and also has two operators new and delete that perform the task of allocating and freeing the memory in a better and easier way.
new Operator
The new operator denotes a request for memory allocation on the Heap. If sufficient memory is available, new operator initializes the memory and returns the address of the newly allocated and initialized memory to the pointer variable.
Syntax
To allocate memory to any data type, the syntax is:-
pointer-variable=new data-type; |
Here, 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 types including structure and class.
We can also use new operator to initialize memory.
pointer-variable=new data-type(value); |
new operator to allocate block of memory.
pointer-variable=new data-type[size]; |
Where size specifies number of variables in an array.
delete Operator
Since it is programmer’s responsibility to deallocate dynamically allocated memory, programmers are provided delete operator by C++ language.
Syntax
Here, pointer-variable is the pointer that points to the data object created by new.
To free the dynamically allocated array pointed by pointer-variable
delete[] pointer-variable; |
Example
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
//Memory allocation using new operator
int *p=new int;
//Value assignment using new operator
*p=10;
cout<<"p:"<<*p<<"\n";
//Memory allocation and value assignment using new
int *p1=new int(20);
cout<<"p1:"<<*p1<<"\n";
//Array creation using new
float *p2=new float[5];
for(int i=0;i<5;i++)
{
p2[i]=i+10;
}
for(int j=0;j<5;j++)
{
cout<<"p2["<<j<<"]:"<<p2[j]<<"\n";
}
//Memory deallocation of variables assigned by new
delete p;
delete p1;
delete[] p2;
getch();
} |
Output
p:10
p1:20
p2[0]:10.5
p2[1]:11.5
p2[2]:12.5
p2[3]:13.5
p2[5]:14.5 |
Static Members of Class
Static data members are class members that are declared using the static keyword. There is only one copy of the static data member in the class, even if there are many class objects. This is because all the objects share the static data member.
Syntax
static data-type variable-name; |
Properties of static data members
- Static members are only declared in class declaration, not defined. They must be explicitly defined outside the class using scope resolution operator.
- Static members are shared among all objects. That is why they are also known as class members or class fields.
- Static members can be accessed without any object.
Example
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
class Check
{
private:
int x;
public:
//Declared a static variable of Check class
static int objectCount;
//Declared a static method of Check class
static void show();
void setX(int a)
{
x=a;
}
void getX()
{
cout<<"x:"<<x<<"\n";
}
Check()
{
objectCount++;
}
};
//Definition of static variable objectCount
int Check::objectCount=0;
//Definition of static method show
void Check::show()
{
cout<<" x is updated\n";
}
void main()
{
clrscr();
Check ch;
ch.setX(10);
ch.getX();
//Accessing static method show
Check::show();
Check ch1;
ch1.setX(20);
ch1.getX();
Check::show();
Check ch2;
ch2.setX(30);
ch2.getX();
Check::show();
Check ch3;
ch3.setX(40);
ch3.getX();
Check::show();
//Accessing static variable objectCount
cout<<"Number of Objects created:"<<Check::objectCount<<"\n";
getch();
} |
Output
x:10
x is updated
x:20
x is updated
x:30
x is updated
x:40
x is updated
Number of Objects created:4 |
For more Object Oriented Programming Lab Experiments Click Here