DMA malloc in Pointer.
DMA is a Dynamic Memory Memory Allocation using to declare memory address.
DMA have four function :::
1. malloc()
2. calloc()
3. realloc()
4. free()
* Malloc Example code *
#include<bits/stdc++.h>
#include<stdio.h>
#include<stdlib.h>
using namespace std;
int main()
{
int i,n,min,max,j; // for allocate memory ....
cout<<"Enter the number of items\n";
cin>>n;
int *ptr; // for pointer.......
ptr=(int *) malloc(n* sizeof(int)); // allocation of the memory n*4 bytes of int
if(ptr==NULL)
{
cout<<"Memory not allocated\n";
exit(0);
}
else {
cout<<"Memory successfully allocatedd\n";
for(int i=0;i<n;++i){
scanf("%d",(ptr+i));
}
}
cout<<"The elements of pointer are \n";
for(int i=0;i<n;++i){
printf("%d\t",ptr[i]);
}
max=ptr[0];
for(i=0;i<n;++i){
if(max<ptr[i]){
max=ptr[i];
}
}
cout<<max<<"\n";
min=ptr[0];
for(i=1;i<n;++i){
if(min>ptr[i]){
min=ptr[i];
}
}
cout<<min<<"\n";
return 0;
}
Comments
Post a Comment