Dynamic memory (Runtime) Allocation and Deallocation operator
-> Programmers can dynamically allocate storage space while the program is running, but programmers cannot create new variable names "on the fly", and for this reason, dynamic allocation requires two criteria:
1. Creating the dynamic space in memory
2. Storing its address in a pointer (so that space can be accessed)
-> Memory de-allocation is also a part of this concept where the "clean-up" of space is done for variables or other data storage. It is the job of the programmer to de-allocate dynamically created space. For de-allocating dynamic memory, we use the delete operator. In other words, dynamic memory Allocation refers to performing memory management for dynamic memory allocation manually.
*Memory in your C++ program is divided into two parts:
stack: All variables declared inside any function takes up memory from the stack.
heap: It is the unused memory of the program and can be used to dynamically allocate the memory at runtime.
-> C++ defined two new operator name "new" and "delete" to perform task of Allocating and freeing the memory in a better and easier way. since, these operators manipulate memory on the free store. In C language "malloc and calloc and free" are function, whereaas "new and delete" are operators in C++.
// C++ program to demonstrate dynamic memory Allocation and deallocation operators.
-> Programmers can dynamically allocate storage space while the program is running, but programmers cannot create new variable names "on the fly", and for this reason, dynamic allocation requires two criteria:
1. Creating the dynamic space in memory
2. Storing its address in a pointer (so that space can be accessed)
-> Memory de-allocation is also a part of this concept where the "clean-up" of space is done for variables or other data storage. It is the job of the programmer to de-allocate dynamically created space. For de-allocating dynamic memory, we use the delete operator. In other words, dynamic memory Allocation refers to performing memory management for dynamic memory allocation manually.
*Memory in your C++ program is divided into two parts:
stack: All variables declared inside any function takes up memory from the stack.
heap: It is the unused memory of the program and can be used to dynamically allocate the memory at runtime.
-> C++ defined two new operator name "new" and "delete" to perform task of Allocating and freeing the memory in a better and easier way. since, these operators manipulate memory on the free store. In C language "malloc and calloc and free" are function, whereaas "new and delete" are operators in C++.
// C++ program to demonstrate dynamic memory Allocation and deallocation operators.
#include<iostream>
using namespace std;
int main()
{
int n;
cout<<"Enter total no. of students:";
cin>>n;
float *marks;
// dynamic memory allocation / Runtime
marks= new float[n];
cout<<"Enter marks:";
for(int i=0;i<n;i++)
{
cin>>*(marks+i); // cin>>marks[i];
}
for(int i=0;i<n;i++)
{
cout<<*(marks+i)<<"\t";
}
delete []marks;
return 0;
}
using namespace std;
int main()
{
int n;
cout<<"Enter total no. of students:";
cin>>n;
float *marks;
// dynamic memory allocation / Runtime
marks= new float[n];
cout<<"Enter marks:";
for(int i=0;i<n;i++)
{
cin>>*(marks+i); // cin>>marks[i];
}
for(int i=0;i<n;i++)
{
cout<<*(marks+i)<<"\t";
}
delete []marks;
return 0;
}
OUTPUT:
Enter total no. of students:3
Enter marks: 44.6 88.61 36.33
44.6 88.61 36.33
Enter total no. of students:3
Enter marks: 44.6 88.61 36.33
44.6 88.61 36.33