Académique Documents
Professionnel Documents
Culture Documents
The process of allocating memory during program execution is called dynamic memory
allocation.
Although, C language inherently does not have any technique to allocate memory
dynamically, there are 4 library functions under "stdlib.h" for dynamic memory allocation.
1. malloc()
2. calloc()
3. realloc()
4. free()
Function Description
malloc() allocates requested size of bytes and returns a void pointer pointing to the first
byte of the allocated space
calloc() allocates space for an array of elements, initialize them to zero and then return
a void pointer to the memory
Memory is allocated before the execution of Memory is allocated during the execution of
the program begins (During Compilation). the program.
Variables remain permanently allocated. Allocated only when program unit is active.
In this type of allocation Memory cannot be In this type of allocation Memory can be
resized after the initial allocation. dynamically expanded and shrunk as
necessary.
It is less efficient than Dynamic allocation It is more efficient than Static allocation
strategy. strategy.
Synatax:
void *ptr;
Here, ptr is pointer of cast-type. The malloc() function returns a pointer to an area of
memory with size of byte size.
Example
int *x;
free(x);
This statement will allocate 200 bytes and the pointer points to the address of first byte of
memory.
Example Program
Write a C program to find the sum of n elements using dynamic memory allocation
function malloc().
#include <stdio.h>
#include<conio.h>
#include <stdlib.h>
void main()
{
int num, i,num, *ptr, sum = 0;
clrscr();
printf("Enter number of elements: ");
scanf("%d", &num);
ptr = (int*) malloc(num * sizeof(int)); //memory allocated using malloc
if(ptr == NULL)
{
printf("Error! memory not allocated.");
exit(0);
}
printf("Enter elements of array: ");
for(i = 0; i < num; ++i)
{
scanf("%d", (ptr + i));
sum = sum + (*(ptr + i));
}
printf("Sum = %d", sum);
free(ptr);
getch();
}
Synatax:
void *ptr;
Example 1:-
int *x;
free(x);
Example 2:-
struct employee
char *name[35];
int salary;
};
emp *e1;
Example Program
Write a C program to find the sum of n elements using dynamic memory allocation
function calloc().
#include <stdio.h>
#include<conio.h>
#include <stdlib.h>
void main()
{
int num, i,num, *ptr, sum = 0;
clrscr();
printf("Enter number of elements: ");
scanf("%d", &num);
ptr = (int*) calloc(num, sizeof(int)); //memory allocated using calloc()
if(ptr == NULL)
{
printf("Error! memory not allocated.");
exit(0);
}
printf("Enter elements of array: ");
for(i = 0; i < num; ++i)
{
scanf("%d", (ptr + i));
sum = sum + (*(ptr + i));
}
printf("Sum = %d", sum);
free(ptr);
getch();
}
Syntax
int *x;
Example Program
#include <stdio.h>
#include <stdlib.h>
#include<conio.h>
void main()
{
char *str;
clrscr();
/* Reallocating memory */
str = (char *) realloc(str, 30);
strcat(str, "of engineering");
printf("String = %s, Address = %u\n", str, str);
free(str);
getch();
}
Its prototype is
void free(void *ptr);
The free() function releases the memory pointed to by ptr. This memory must have been
allocated with malloc(), calloc(), or realloc(). If ptr is NULL, free() does nothing.
Example
int *x;
free(x);