You are on page 1of 10

-----------------------------------------

Memory Allocation
---------------------------------------

Static Memory Allocation


---------------------------------------
Allocation of memory for variables, during compilation time itself is known as static memory allocation.
Once the memory is allocated during compile time, the allocated memory cannot be expanded nor be
compressed to accommodate more or less data during the program execution. In this method of memory
allocation, the size of memory to be allocated is known before compile time and is fixed. It cannot be
altered during execution time.

Eg:
int x[10];

During compilation of the given statement, the compiler will allocate 10*4(size of integer)=40 bytes of
memory for the array variable “x”. If more bytes have to be allocated to the same array variable during
execution time, it is not possible or if the memory occupied has to be reduced, even that is not possible.
Hence, this is disadvantages of static memory allocation. The memory allocated will be de-allocated once
the program execution comes to end.

This disadvantage can be overcome by using “Dynamic Memory Management”.

Dynamic Memory Allocation


----------------------------------------

Dynamic memory management is carried out in C++ using the two operators “new” and “delete”. These
operators are used to allocate and free allocated memory at run time.

Dynamic memory allocation using “new”

An object can be created using the “new” operator. The “new” operator allocates specific amount of
memory block from the free store during execution time and return the starting address of the block
allocated to a pointer. “new “ is a keyword in C++.

The major advantages of “new” are:-

▪ It automatically returns the address of the correct cast, reducing the programmer’s botheration
to typecast explicitly unlike “C Programming language” memory allocation functions “malloc()”,
“calloc()”, “realloc()” function.

▪ It automatically determines the size of the data object for which the memory is allocated, which
requires no “sizeof” operator.

▪ It makes the syntax very easy to write, read and understand unlike “C Programming language”
memory allocation functions “malloc()”, “calloc()”, “realloc()” function.

1
▪ “new” and “delete” operators can be overloaded.

▪ We can initialize of the object at the time of allocating the memory.

Eg:
int *ptr=new int(10);

Syntax for single integer variable

<data-type> <pointer-variable>=new <data-type>;

Example:

#include<iostream>
using namespace std;
int main()
{
int *ptr;
ptr=new int; // allocation memory for a single integer
*ptr=10;
cout<<"The value of ptr="<<*ptr;

return 0;
}

Syntax for an array

<data-type> <pointer-variable>=new <data-type>[size];

Example: Write a C++ program to accept 10 numbers from the user and print the sum of given
number by using “new” operator.
int main()
{

2
int *ptr,no,i,sum=0;
ptr=new int[10]; //allocating memory for an array
cout<<"Enter the ten numbers:";

for(i=0;i<10;i++)
{
cin>>ptr[i];
}

for(i=0;i<10;i++)
{
sum=sum+ptr[i];
}
cout<<endl<<"sum="<<sum;

return 0;
}

Example: Write a C++ program to accept “N” numbers from the user and print the sum of given
number by using “new” operator.

#include<iostream>
using namespace std;
int main()
{
int *ptr,no,i,sum=0;

cout<<"Enter the size of array:";


cin>>no;

ptr=new int[no]; //allocating memory for an array

cout<<"Enter the number one by one:";


for(i=0;i<no;i++)
{
cin>>ptr[i];
}

for(i=0;i<no;i++)
{
sum=sum+ptr[i];
}
cout<<endl<<"sum="<<sum;

3
return 0;
}

Syntax for a class object

<class-name> <pointer-variable>=new <class-name>;

Example:
#include<iostream>
#include<string.h>
using namespace std;
class Student
{
int rno;
char name[100];
public:
void set_data(int r,char n[])
{
rno=r;
strcpy(name,n);
}

void display()
{
cout<<endl<<rno<<"\t"<<name;
}
};

int main()
{
//Student *ram=new Student; //or
Student *ram;
ram=new Student();
ram->set_data(1001,"Manjeet");
ram->display();

return 0;

4
}

Syntax for an array of class object

<class-name> <pointer-variable>=new <class-name>[size];

Example:
#include<iostream>
#include<string.h>
using namespace std;
class Student
{
int rno;
char name[100];
public:
void set_data()
{
cout<<"Enter the Roll Number:";
cin>>rno;
cout<<"Enter the Name:";
cin>>name;
}

void display()
{
cout<<endl<<rno<<"\t"<<name;
}
};

int main()
{

Student *s;
s=new Student[100];
int i,no;
cout<<"Enter the size of student:";
cin>>no;
cout<<"Enter the student details one by one:\n";
for(i=0;i<no;i++)
{

5
s[i].set_data();
}

for(i=0;i<no;i++)
{
s[i].display();
}

return 0;
}

--------------------------------------------
Dynamic Memory De-allocation
--------------------------------------------
The object created by “new” i.e operator has to be de-allocated once its purpose is over. This is
done by the “delete” operator. “delete” operator destroys the created object to release the memory
space for the future use. “delete” is a keyword in C++.

Syntax to de-allocate s single variable


delete <pointer-variable>;

Example:
#include<iostream>
using namespace std;
int main()
{
int *ptr;
ptr=new int; // allocation memory for a single integer
*ptr=10;
cout<<"The value of ptr="<<*ptr;

delete ptr; // de-allocate the memory


return 0;
}

6
Syntax to de-allocate for an array
delete []<pointer-variable>;

Example:
#include<iostream>
using namespace std;
int main()
{
int *ptr,no,i,sum=0;

cout<<"Enter the size of array:";


cin>>no;

ptr=new int[no]; //allocating memory for an array

cout<<"Enter the number one by one:";


for(i=0;i<no;i++)
{
cin>>ptr[i];
}

for(i=0;i<no;i++)
{
sum=sum+ptr[i];
}
cout<<endl<<"sum="<<sum;

delete []ptr;

return 0;
}

Syntax to de-allocate class object


delete <pointer-variable>;

Example:
#include<iostream>
#include<string.h>
using namespace std;
7
class Student
{
int rno;
char name[100];
public:
void set_data(int r,char n[])
{
rno=r;
strcpy(name,n);
}

void display()
{
cout<<endl<<rno<<"\t"<<name;
}
};

int main()
{
//Student *ram=new Student; //or
Student *ram;
ram=new Student();
ram->set_data(1001,"Manjeet");
ram->display();

delete ram;
return 0;
}

#include <iostream>
#include <iomanip>
using namespace std;
class Search
{

int *a,no,i,sno,flag;
public:
Search()
{
flag=0;

8
}
void set_data()
{
cout<<"Enter the size of array:";
cin>>no;
a=new int[no]; //allocating memory for an array
for(i=0;i<no;i++)
cin>>a[i];
}

void search_number()
{
cout<<"Enter the number to be search:";
cin>>sno;
for(i=0;i<no;i++)
{
if(a[i]==sno)
{
flag=1;
break;
}
}
}

void display()
{
if(flag==1)
cout<<"Number found.";
else
cout<<"Number not found.";
}

~Search()
{
delete []a;
}

};
main()
{
Search sc;
sc.set_data();
sc.search_number();

9
sc.display();
return 0;
}

10

You might also like