Dynamic memory allocation

Dynamic memory allocation:

It is a process of allocating memory while program ‘s execution

DYNAMIC MEMORY ALLOCATION FUNCTIONS :

C language has 4 dynamic memory allocation functions. They are,

Function in C Syntax in C
malloc () malloc (number *sizeof(int));
calloc () calloc (number, sizeof(int));
realloc () realloc (ptr_name, number * sizeof(int));
free () free (ptr_name);

1. MALLOC() FUNCTION IN C:

• It is used to allocate space in memory during the execution of the program.
• It does not initialize the memory allocated while execution. It carries garbage value.
It returns null pointer if it couldn’t able to allocate requested amount of memory.

PROGRAM FOR MALLOC() FUNCTION:

Example
#include<stdio.h>

#include<string.h>

#include<stdlib.h>

int main()

{

char *mem_allocation1; /* memory is allocated dynamically */

mem_allocation1 = malloc( 20 * sizeof(char) );

if( mem_allocation1== NULL )

{

printf(“Could not able to allocate requested memory\n”);

}

else

{

strcpy( mem_allocation1,”fresh2refresh.com”);

}

printf(“Dynamically allocated memory contents : ” \ “%s\n”, mem_allocation1 );

free(mem_allocation1);

}

Output

Dynamically allocated memory contents : fresh2refresh.com

2. CALLOC() FUNCTION IN C:

• calloc () function is similar like malloc () function. But calloc () initializes the allocated memory to zero. But, malloc() doesn’t.
PROGRAM FOR CALLOC() FUNCTION :

 

Example
#include<stdio.h>

#include<string.h>

#include<stdlib.h>

int main()

{ char *mem_allocation1; /* memory is allocated dynamically */

mem_allocation1 = calloc( 20, sizeof(char) );

if( mem_allocation1== NULL )

{

printf(“Couldn’t able to allocate requested memory\n”);

}

else

{

strcpy( mem_allocation1,”fresh2refresh.com”);

}

printf(“Dynamically allocated memory contents : ” \ “%s\n”, mem_allocation1 );

free(mem_allocation1);

}

Output

Dynamically allocated memory contents:fresh2refresh.com

3. REALLOC() FUNCTION :

• this function modifies the allocated memory size by malloc () and calloc () functions to new size.
• If enough space does not exist in memory of current block to extend, new block is allocated for the full size of reallocation, then copies the contents of existed data to new block and then frees the previous old block.

4. FREE() FUNCTION IN C:

• This function frees the allocated memory by malloc (), calloc (), realloc () functions and it returns the memory to the system.

Program for Realloc() and Free() Functions :
#include<stdio.h>  #include<string.h>

#include<stdlib.h>

int main()

{char *mem_allocation1; /* memory is allocated dynamically */

mem_allocation1 = malloc( 20 * sizeof(char) );

if( mem_allocation1 == NULL )

{

printf(“Couldn’t able to allocate requested memory\n”);

}

else

{

strcpy( mem_allocation1,”fresh2refresh.com”);

}

printf(“Dynamically allocated memory content : ” \ “%s\n”, mem_allocation1 ); mem_allocation1=realloc(mem_allocation1,100*sizeof(char));

if( mem_allocation1 == NULL )

{

printf(“Couldn’t able to allocate requested memory\n”);

}

else

{

strcpy( mem_allocation1,”space is extended upto ” \ “100 characters”);

}

printf(“Resized memory : %s\n”, mem_allocation1 );

free(mem_allocation1);

}

Output

Dynamically allocated memory content : fresh2refresh.com

Resized memory : space is extended upto 100 characters

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *