Thursday, July 5, 2018

Dynamic memory allocation in C

The concept of dynamic memory allocation in c language enables the C programmer to allocate memory at runtime. Dynamic memory allocation in c language is possible by 4 functions of stdlib.h header file.

  • malloc()
  • calloc()
  • realloc()
  • free()

Before learning above functions, let's understand the difference between static memory allocation and dynamic memory allocation.

static memory allocationdynamic 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.

1. Malloc() function in c

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

The syntax of malloc() function is given below:





ptr=(cast-type*)malloc(byte-size)







#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main()
{
char *mem_allocation;
/* memory is allocated dynamically */
mem_allocation = malloc( 20 * sizeof(char) );
if( mem_allocation== NULL )
{
printf("Couldn't able to allocate requested memory\n");
}
else
{
strcpy( mem_allocation,"cprogrammingcoding.blogspot.com");
}
printf("Dynamically allocated memory content : " \
"%s\n", mem_allocation );
free(mem_allocation);
}



OUTPUT:

Dynamically allocated memory content : cprogrammingcoding.blogspot.com





#include<stdio.h>
#include<stdlib.h>
int main(){
int n,i,*ptr,sum=0;
printf("Enter number of elements: ");
scanf("%d",&n);
ptr=(int*)malloc(n*sizeof(int)); //memory allocated using malloc
if(ptr==NULL)
{
printf("Sorry! unable to allocate memory");
exit(0);
}
printf("Enter elements of array: ");
for(i=0;i<n;++i)
{
scanf("%d",ptr+i);
sum+=*(ptr+i);
}
printf("Sum=%d",sum);
free(ptr);
return 0;
}



Output:

Enter elements of array: 3

Enter elements of array: 10

10

10

Sum=30

2. Calloc() function in c

calloc () function is also like malloc () function. But calloc () initializes the allocated memory to zero. But, malloc() doesn’t.

The syntax of calloc() function is given below





ptr=(cast-type*)calloc(number, byte-size)



Example 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( 20, sizeof(char) );
if( mem_allocation== NULL )
{
printf("Couldn't able to allocate requested memory\n");
}
else
{
strcpy( mem_allocation,"cprogrammingcoding.blogspot.com");
}
printf("Dynamically allocated memory content : " \
"%s\n", mem_allocation );
free(mem_allocation);
}



Output

Dynamically allocated memory content : cprogrammingcoding.blogspot.com





#include<stdio.h>
#include<stdlib.h>
int main(){
int n,i,*ptr,sum=0;
printf("Enter number of elements: ");
scanf("%d",&n);
ptr=(int*)calloc(n,sizeof(int)); //memory allocated using calloc
if(ptr==NULL)
{
printf("Sorry! unable to allocate memory");
exit(0);
}
printf("Enter elements of array: ");
for(i=0;i<n;++i)
{
scanf("%d",ptr+i);
sum+=*(ptr+i);
}
printf("Sum=%d",sum);
free(ptr);
return 0;
}



Output:

Enter elements of array: 3

Enter elements of array: 10

10

10

Sum=30

3. Realloc() function in c

  • realloc () function modifies the allocated memory size by malloc () and calloc () functions to new size.
  • If enough space doesn’t exist in memory of current block to extend, new block is allocated for the full size of reallocation, then copies the existing data to new block and then frees the old block.

Let's see the syntax of realloc() function.





ptr=realloc(ptr, new-size)



4. Free() function in c

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

Let's see the syntax of free() function.





free(ptr)



Example 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( 20 * sizeof(char) );
if( mem_allocation == NULL )
{
printf("Couldn't able to allocate requested memory\n");
}
else
{
strcpy( mem_allocation,"cprogrammingcoding.blogspot.com");
}
printf("Dynamically allocated memory content : " \
"%s\n", mem_allocation );
mem_allocation=realloc(mem_allocation,100*sizeof(char));
if( mem_allocation == NULL )
{
printf("Couldn't able to allocate requested memory\n");
}
else
{
strcpy( mem_allocation,"space is extended upto " \
"100 characters");
}
printf("Resized memory : %s\n", mem_allocation );
free(mem_allocation);
}



Output

Dynamically allocated memory content : cprogrammingcoding.blogspot.com

Resized memory : space is extended upto 100 characters

Difference between static memory allocation and dynamic memory allocation in c

Static memory allocation Dynamic memory allocation
In static memory allocation, memory is allocated while writing the C program. Actually, user requested memory will be allocated at compile time.In dynamic memory allocation, memory is allocated while executing the program. That means at run time.
Memory size can’t be modified while execution. Example: arrayMemory size can be modified while execution. Example: Linked list

Difference between malloc() and calloc() functions in c

malloc()calloc()
It allocates only single block of requested memoryIt allocates multiple blocks of requested memory
int *ptr;ptr = malloc( 20 * sizeof(int) );For the above, 20*4 bytes of memory only allocated in one block.Total = 80 bytesint *ptr;Ptr = calloc( 20, 20 * sizeof(int) );For the above, 20 blocks of memory will be created and each contains 20*4 bytes of memory.Total = 1600 bytes
malloc () doesn’t initializes the allocated memory. It contains garbage valuescalloc () initializes the allocated memory to zero
type cast must be done since this function returns void pointer int *ptr;ptr = (int*)malloc(sizeof(int)*20 );Same as malloc () function int *ptr;ptr = (int*)calloc( 20, 20 * sizeof(int) );



Instagram



No comments:

Post a Comment