Dynamic Memory Allocation In C++
When
we use any kind of static memory allocation at that time we not define size so
error came on our program in display so we have to solve that problem we use
dynamic allocation operator.
Using of dynamic allocation operator we can use the
size of memory so that time we can
increase or decrease so the means of dynamic is run time so its is known as run time memory allocation.
In
which 2 type operator are used, and they are given:
1.
New
2.
Delete
Fig: Dynamic memory allocation
1.New
For Program execution time use variable for
memory allocated to use new operator.
New
is a type of keyword.
Syntax:
pointer
variable = new datatype [size];
Example:
*p=new
int[10];
Program:
#include<iostream.h>
#include<conio.h>
void
main
{
int *p;
p=new
int[15];
*p=13;
cout<<”&p=”<<&p;
cout<<”*p=”<<*p;
getch();
}
Output:
*p=13&p=0*8f94fff4*p=13
2.Delete
This
is one type keyword. For new keyword through allocated memory to destroy to do work on it.
Syntax:
delete
pointer variable;
Example:
delete
p;
Program:
#include<iostream.h>
#include<conio.h>
void
main
{
int *p;
p=new
int[15];
*p=13;
cout<<”&p=”<<&p;
getch();
}
Output:
&p=0x8f92fff4
No comments:
Post a Comment