Static memory allocation
Memory is allocated before program execution. Allocated memory size cannot be changed. The allocated memory can be known using address of operator. It uses the data structure called stack for implementing static allocation. There is no memory reusability.
Dynamic Memory Allocation
Memory is allocated during program execution. The size of the allocated memory can be changed. The functions calloc() and malloc() are used to deal with dynamic memory allocation in C language. It uses the data structure called heap for implementing dynamic allocation. There is memory reusability and memory can be freed when not required.
Dynamic memory can change (grow/shrink) based on requirement(insertion or deletion). Static memory is fixed memory.
Memory for variables in your program will be allocated only when the program is running. But compiler won’t allocate memory.
Programmer can decide whether he want static memory or dynamic memory
There are some pre defined functions in stdlib.h for dynamic memory allocation
- malloc()
- calloc()
- realloc()
- free()
malloc()
It allocates memory at run time. The following is the syntax of malloc() function
datatype *pid=(int *) malloc(NumofValues*size(datatype));
we have to specify the number of values that we want to allocate for the specified size. The malloc() allocates memory based on the specification and returns the address of first byte of allocated memory
calloc()
This is also used to allocate at run time. But along with memory allocation it initializes the memory elements with zero.
Syntax:
datatype *pid=(datatype *)calloc(numofitems,sizeof(datatype));
realloc()
It is used to reuse or extend the memory previously allocated with malloc() or calloc().
datatype *pid=(datatype *)realloc(previous pointer type,new number of elements*sizeof(datatype));
free()
it is used to destroy the memory allocated explicitly which was allocated dynamically.
// C program to implement static memory allocation
#include <stdio.h>
#include <stdlib.h>
int main()
{
int size;
printf(“Enter limit of the text: \n”);
scanf(“%d”, &size);
char str[size];
printf(“Enter some text: \n”);
scanf(” “);
gets(str);
printf(“Inputted text is: %s\n”, str);
return 0;
}
// C program to illustrate Dynamic Memory Allocation
#include <stdio.h>
#include <stdlib.h>
int main()
{
int size, resize;
char* str = NULL;
printf(“Enter limit of the text: \n”);
scanf(“%d”, &size);
str = (char*)malloc(size * sizeof(char));
if (str != NULL) {
printf(“Enter some text: \n”);
scanf(” “);
gets(str);
printf(“Inputted text by allocating”
“memory using malloc() is: “
“%s\n”,
str);
}
free(str);
str = (char*)calloc(50, sizeof(char));
if (str != NULL) {
printf(“Enter some text: \n”);
scanf(” “);
gets(str);
printf(“Inputted text by allocating “
“memory using calloc() is: “
“%s\n”,
str);
}
printf(“Enter the new size: \n”);
scanf(“%d”, &resize);
str = (char*)realloc(str, resize * sizeof(char));
printf(“Memory is successfully “
“reallocated by using “
“realloc() \n”);
if (str != NULL) {
printf(“Enter some text: \n”);
scanf(” “);
gets(str);
printf(“Inputted text by reallocating”
” memory using realloc()is: “
“%s\n”,
str);
}
free(str);
str = NULL;
return 0;
}