For execution of a program it is essential to bring the program in the main memory when a program doesn't fit into the main memory parts of its are brought into the main memory one by one and the full program is executed eventually .
Dynamic allocation is a technique in which a program can acquire storage space in the main memory . in this method the space for the program is allocated from the free space during execution of the program.
The free region of the memory is called the heap.
High stack
Free memory for allocation
Global variable
LOW program
MEMORY ALLOCATION FUNCTIONS ::
MALLOC() ::
This function is used to allocate memory space in bytes to the variables of different data types .This function reserves bytes of determined size and returns the base address to pointer variables .
The prototypes are declared in alloc.h and stdlib.h . the format of the malloc () function is an under .
Pnt = (data type*)malloc(given size);
CALLOC() : this function is useful for allocating multiple blocks of memory. It is declared two arguments .the prototypes are declared in alloc.h and stdlib.h.the format of the malloc function is under
Pnt = (int*) calloc (4,3);
FREE() ::
Function is used to release the memory allocated by memory allocating functions .Thus using this function wastage of memory is prevented .The declaration of the function is as under .
Free (pnt)
NOTE:
Pnt is a pointer .
REALLOC() ::
This function reallocates main memory .The prototypes are declared in alloc.h and stdlib .h attempts are made to shrink or enlarge the previously allocated memory by malloc or calloc () function .
It returns the address of the reallocated block ,which can be different from the original address. If the block cannot be reallocated or size ==0 ,realloc () returns null .
Realloc is used to allocate memory of new size using the previous pointer and size.
Coreleft ::
This function returns a measure of unused memory .If the memory model selected is tiny, small, or medium then follow the function declaration as per statement (a) . if the memory model selected is compact ,large,or huge follow the declaration (b).
Unsigned coreleft (void);
Unsigned long coreleft (void);
Write a program to allocate a memory.
#include<stdio.h>
int main (){
int *ptr;
printf("The size of int %d\n",sizeof(int));
printf("The size of float %d\n",sizeof(int));
printf("The size of char %d\n",sizeof(int));
return 0;
}
Output:
The size of int 4
The size of float 4
The size of char 4
Write a program to create a dynamic memory allocation using malloc function .
#include<stdio.h>
int main (){
int *ptr;
ptr = (int *)malloc(6 * sizeof(int));
for (int i=0;i<6;i++){
printf("Enter the value of %d elements : \n",i);
scanf("%d",&ptr[i]);
}
for (int i=0;i<6;i++){
printf(" the value of %d elements is : \n",ptr[i]);
}
return 0;
}
Output :
Enter the value of 0 elements :
10
Enter the value of 1 elements :
20
Enter the value of 2 elements :
30
Enter the value of 3 elements :
40
Enter the value of 4 elements :
50
Enter the value of 5 elements :
60
the value of 10 elements is :
the value of 20 elements is :
the value of 30 elements is :
the value of 40 elements is :
the value of 50 elements is :
the value of 60 elements is :
Write a program using calloc function.
#include<stdio.h>
int main (){
int *ptr;
ptr = (int *)calloc(6 , sizeof(int));
for (int i=0;i<6;i++){
printf("Enter the value of %d elements : \n",i);
scanf("%d",&ptr[i]);
}
for (int i=0;i<6;i++){
printf(" the value of %d elements is : \n",ptr[i]);
}
return 0;
}
Output :
Enter the value of 0 elements :
1
Enter the value of 1 elements :
2
Enter the value of 2 elements :
3
Enter the value of 3 elements :
4
Enter the value of 4 elements :
5
Enter the value of 5 elements :
6
the value of 1 elements is :
the value of 2 elements is :
the value of 3 elements is :
the value of 4 elements is :
the value of 5 elements is :
the value of 6 elements is :