Dynamic memory allocation in C
1. malloc()
2. calloc()
3. realloc()
4. free()
static memory
allocation |
dynamic memory
allocation |
memory is allocated at compile time. |
memory is allocated at run time. |
memory can't be increased while executing program. |
memory can be increased while executing program. |
used in array. |
used in linked list. |
Now let's have a quick look at the methods
used for dynamic memory allocation.
malloc() |
allocates single block of requested memory. |
calloc() |
allocates multiple block of requested memory. |
realloc() |
reallocates the memory occupied by malloc() or calloc() functions. |
free() |
frees the dynamically allocated memory. |
malloc() function in C
The malloc() function allocates single block
of requested memory.
It doesn't initialize memory at execution
time, so it has garbage value initially.
It returns NULL if memory is not sufficient.
The syntax of malloc() function is given
below:
ptr=(cast-type*)malloc(byte-size)
int
*ptr = (int *)malloc(5*sizeof(int));
Program
#include<stdio.h>
#include<conio.h>
#include<malloc.h>
struct emp
{
int eno;
char name[20];
long int salary;
};
main()
{
struct emp *ptr;
clrscr();
ptr=(struct emp*)malloc(sizeof(struct emp));
printf("size of emp=%d\n",sizeof(struct emp));
printf("Eno=%d\nname=%s\nsalary=%ld\n",ptr->eno,ptr->name,ptr->salary);
getch();
}
Output
size
of emp=26
eno=0
ename=
salary=0
Calloc() function in C
The calloc() function continuously allocates multiple
block of requested memory.
It initially initialize all bytes to zero.
It returns NULL if memory is not sufficient.
The syntax of calloc() function is given
below:
ptr=(cast-type*)calloc(number, byte-size)
ptr=(int*)calloc(n,sizeof(int));
#include<stdio.h>
#include<conio.h>
struct emp
{
int eno;
char name[20];
long int salary;
};
main()
{
struct emp *ptr;
clrscr();
ptr=(struct emp*)calloc(sizeof(struct emp));
printf("size of emp=%d\n",sizeof(struct emp));
printf("Eno=%d\nname=%s\nsalary=%ld\n",ptr->eno,ptr->name,ptr->salary);
getch();
}
Output
size
of emp=26
eno=0
ename=
salary=0
realloc() function in C
If memory is not sufficient for malloc() or
calloc(), you can reallocate the memory by realloc() function. In short, it
changes the memory size.
Let's see the syntax of realloc() function.
ptr=realloc(ptr, new-size)
#include<stdio.h>
#include<conio.h>
main()
{
int *ptr,i;
clrscr();
ptr=(int*)calloc(5,sizeof(int));
for(i=0;i<5;i++)
{
printf("\n%d",*(ptr+i));
}
ptr=(int*)realloc((void*)ptr,5*sizeof(int));
for(i=0;i<5;i++)
{
printf("\n%d",*(ptr+i));
}
getch();
}
output
0
0
0
0
0
0
0
0
0
0
free() function in C
The memory occupied by malloc() or calloc()
functions must be released by calling free() function. Otherwise, it will
consume memory until program exit.
Let's see the syntax of free() function.
1. free(ptr)
#include<stdio.h>
#include<conio.h>
#include<malloc.h>
struct emp
{
int eno;
char name[20];
long int salary;
};
main()
{
struct emp *ptr;
clrscr();
ptr=(struct emp*)malloc(sizeof(struct emp));
printf("size of emp=%d\n",sizeof(struct emp));
printf("Eno=%d\nname=%s\nsalary=%ld\n",ptr->eno,ptr->name,ptr->salary);
free(ptr);
getch();
}
0 comments:
Post a Comment