Question: 1. Explain the operators used for dynamic memory allocation with examples.
Dynamic
Memory Allocation
The
dynamic memory allocation is done using an operator new. The syntax of dynamic memory allocation using new is
Name_of_the_variable
data type;
int *p;
p=new
int;
We
can allocate the memory for more than one element. For instance if we want to
allocate memory of size in for 5 elements we can declare.
int *p;
p=new
int[5];
In
this case, the system dynamically assigns space for five elements of type int
and returns a pointer to the first element of the sequence, which is assigned
to p. Therefore, now, p points to a valid block of memory with space for five
elements of type int.

The
program given below allocates the memory for any number of elements and the
memory for those many number of elements get deleted at the end of the program.
#include<iostream>
using namespace std;
void main ()
{
int i,n;
int *p:
clrscr();
cout <<
"How many numbers would you like to type? ";
cin >> i;
p = new int[i];//dynamic
memory allocation
if (p = = 0)
cout <<
"Error: memory could not be allocated":
else
{
for (n=0; n<i;
n++)
{
cout <<
"Enter The Number: ";
cin>> p[n];
}
cout << "You have entered: ";
for (n=0; n<i; n++)
cout<<"" <<p[n];
delete[] p://dynamic memory deallocation
}
getch();
}
How many numbers would
you like to type? 7
Enter The Number: 10
Enter The Number: 20
Enter The Number: 30
Enter The Number: 40
Enter The Number: 50
Enter The Number: 60
Enter The Number: 70
You have entered: 10
20 30 40 50 60 70
The
use of dynamic memory allocation avoids the wastage of memory. Because the
programmer can allocate the memory as per his need using new operator and when
that memory block is not needed it is deallocated.
1. Explain the
operators used for dynamic memory allocation with examples.
Data Structures using C PlusPlus: Chapter 1: Data Abstraction and Overloading : Tag: Data Structure, C++, C Programing : Data Structures using C ++ Program - Dynamic Memory Allocation
Data Structures using CPlusPlus
CS25C05 2nd Semester ECE Dept | 2025 Regulation | 2nd Semester 2025 Regulation
English Essentials II
EN25C02 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation
Tamils and Technology தமிழர்களும் தொழில்நுட்பமும்
UC25H02 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation
Linear Algebra
MA25C02 2nd Semester | 2025 Regulation
Electron Devices
EC25C01 2nd Semester ECE Dept | 2025 Regulation | 2nd Semester 2025 Regulation
Data Structures using CPlusPlus
CS25C05 2nd Semester ECE Dept | 2025 Regulation | 2nd Semester 2025 Regulation
Circuits and Network Analysis
EC25C02 2nd Semester ECE Dept | 2025 Regulation | 2nd Semester 2025 Regulation
Re-Engineering for Innovation
ME25C05 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation
Engineering Drawing - Laboratory
ME25C01 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation
Data Structures using CPlusPlus - Laboratory
CS25C05 2nd Semester ECE Dept | 2025 Regulation | 2nd Semester 2025 Regulation
Devices and Circuits Laboratory
EC25C03 2nd Semester ECE Dept | 2025 Regulation | 2nd Semester 2025 Regulation