C - Dynamic Memory Allocation: Prev Next
C - Dynamic Memory Allocation: Prev Next
Prev Next
Dynamic memory allocation in C:
The process of allocating memory during program execution is called dynamic memory allocation.
Dynamic memory allocation functions in C:
C language offers 4 dynamic memory allocation functions. They are,
1. malloc()
. calloc()
!. realloc()
4. free()
S.no Function Syntax
1 malloc () malloc (num"er #si$eof(int))%
calloc () calloc (num"er, si$eof(int))%
! realloc () realloc (pointer&name, num"er # si$eof(int))%
4 free () free (pointer&name)%
1. malloc() function in C:
malloc () function is used to allocate space in memory during the execution of the program.
malloc () does not initiali$e the memory allocated during execution. 't carries gar"age (alue.
malloc () function returns null pointer if it couldn)t a"le to allocate re*uested amount of memory.
+xample program for malloc() function in C:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main()
{
char *mem_allocation;
/* memory is allocated dynamically */
mem_allocation = malloc( 2 * si!eo"(char) );
i"( mem_allocation== #$%% )
{
&rint"('(ouldn)t able to allocate re*uested memory+n');
,
else
{
strc&y( mem_allocation-'"resh2re"resh.com');
,
&rint"('.ynamically allocated memory content / ' +
'0s+n'- mem_allocation );
"ree(mem_allocation);
,
,utput:
Dynamically allocated memory content : freshrefresh.com
. calloc() function in C:
calloc () function is also li-e malloc () function. .ut calloc () initiali$es the allocated memory to $ero.
.ut, malloc() doesn)t.
+xample program for calloc() function in C:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main()
{
char *mem_allocation;
/* memory is allocated dynamically */
mem_allocation = calloc( 2- si!eo"(char) );
i"( mem_allocation== #$%% )
{
&rint"('(ouldn)t able to allocate re*uested memory+n');
,
else
{
strc&y( mem_allocation-'"resh2re"resh.com');
,
&rint"('.ynamically allocated memory content / ' +
'0s+n'- mem_allocation );
"ree(mem_allocation);
,
,utput:
Dynamically allocated memory content : freshrefresh.com
!. realloc() function in C:
realloc () function modifies the allocated memory si$e "y malloc () and calloc () functions to ne/
si$e.
'f enough space doesn)t exist in memory of current "loc- to extend, ne/ "loc- is allocated for the
full si$e of reallocation, then copies the existing data to ne/ "loc- and then frees the old "loc-.
4. free() function in C:
free () function frees the allocated memory "y malloc (), calloc (), realloc () functions and returns
the memory to the system.
+xample program for realloc() and free() functions in C:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main()
{
char *mem_allocation;
/* memory is allocated dynamically */
mem_allocation = malloc( 2 * si!eo"(char) );
i"( mem_allocation == #$%% )
{
&rint"('(ouldn)t able to allocate re*uested memory+n');
,
else
{
strc&y( mem_allocation-'"resh2re"resh.com');
,
&rint"('.ynamically allocated memory content / ' +
'0s+n'- mem_allocation );
mem_allocation=realloc(mem_allocation-1*si!eo"(char));
i"( mem_allocation == #$%% )
{
&rint"('(ouldn)t able to allocate re*uested memory+n');
,
else
{
strc&y( mem_allocation-'s&ace is e2tended u&to ' +
'1 characters');
,
&rint"('3esi!ed memory / 0s+n'- mem_allocation );
"ree(mem_allocation);
,
,utput:
Dynamically allocated memory content : freshrefresh.com
0esi$ed memory : space is extended upto 111 characters
Difference "et/een static memory allocation and dynamic memory allocation in C:
S.no Static memory allocation Dynamic memory allocation
1
'n static memory allocation, memory is allocated /hile
/riting the C program. 2ctually, user re*uested memory
/ill "e allocated at compile time.
'n dynamic memory allocation, memory is
allocated /hile executing the program. That
means at run time.