Dynamic Memory Allocation
Memory Allocation:
when we run a c program its take 4 part in a memory.
when we run a c program its take 4 part in a memory.
- stack
- heap
- data area
- code area
Stack memory : Generally 3 types of data store in stack. Firstly automatic variable, secondly functional parameter and thirdly returned address. All local variable store in a stack memory unless we allocate in heap. stack is allocate and deallocate by computer automatic.
Code area: After compilation new created machine language store in code area. Its size depend on exe file or executable file.
Data area: Three type of thins store in data area.
- Global variable.
- static variable.
- initialize structure
- initialize array
Data area have two part initialize and uninitialized.
Ex.
int x;
int y=10;
int main(){
}
here x store in uninitialized data area and y store in initialize data area.
Heap:
In here programmer can store data manually. suppose you declare an array which can take 100 input. But all the time you dont need to input 100 some time you need to input 30 . if you use 30 input then other memory address will be wasted. And if we declared big array like one thousand value can input in that case it can create stack overflow.so your system process can be hampered. To solve this problem programmers allocate value in heap. we can use malloc() for memory allocation which in stdlib header file.
process: malloc(), calloc(),free()
datatype* pointer_name;
pointer mane =(datatype*)malloc(byte_size);
code:
#include<stdio.h>
#include<stdlib.h>
int main(){
int*number;
int n,i;
number=(int*)malloc(n*sizeof(int));
printf("How Many Phone Number: ");
scanf("%d",&n);
if(number!=NULL){
printf("Memory Allocate Successful\n");
}
else{
printf("Nothing is Allocate\n");
exit(0);
}
for(i=0;i<n;i++){
printf("Number No: %d ",i+1);
scanf("%d",&number[i]);
}
for(i=0;i<n;i++){
printf("number no %d : %d\n",i+1,number[i]);
}
free(number);
return 0;
}
*** every time using dynamic allocation we have to clean the memory space because it can create heap overflow.so we have to use free(pointer_name)
Heap:
In here programmer can store data manually. suppose you declare an array which can take 100 input. But all the time you dont need to input 100 some time you need to input 30 . if you use 30 input then other memory address will be wasted. And if we declared big array like one thousand value can input in that case it can create stack overflow.so your system process can be hampered. To solve this problem programmers allocate value in heap. we can use malloc() for memory allocation which in stdlib header file.
process: malloc(), calloc(),free()
datatype* pointer_name;
pointer mane =(datatype*)malloc(byte_size);
code:
#include<stdio.h>
#include<stdlib.h>
int main(){
int*number;
int n,i;
number=(int*)malloc(n*sizeof(int));
printf("How Many Phone Number: ");
scanf("%d",&n);
if(number!=NULL){
printf("Memory Allocate Successful\n");
}
else{
printf("Nothing is Allocate\n");
exit(0);
}
for(i=0;i<n;i++){
printf("Number No: %d ",i+1);
scanf("%d",&number[i]);
}
for(i=0;i<n;i++){
printf("number no %d : %d\n",i+1,number[i]);
}
free(number);
return 0;
}
*** every time using dynamic allocation we have to clean the memory space because it can create heap overflow.so we have to use free(pointer_name)
Onek valo
ReplyDeletethank you
Delete