codescracker
c

C Memory Management



« Previous Tutorial Next Tutorial »

C language provides several functions for memory allocation and management. These functions can be found in the header file <stdlib.h>.

Dynamic Allocation Functions in C

Here this table lists several functions available in C to perform memory allocation and management

Function Description
void *calloc(int num, int size) used to allocate an array of num elements each of which size in bytes will be size
void *malloc(int num) used to allocate an array of num bytes and leave them initialized.
void free(void *address) This function release a block of memory block specified by address.
void *realloc(void *address, int newsize) used to re-allocate memory extending it upto newsize.

C Dynamic Memory Allocation Example

Here is an example of dynamic memory allocation in C:

/* C Memory Management - This program illustrates
 * the concept of dynamic memory allocation in C
 */

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

void main()
{
   char name[100];
   char *desc;
   int len1, len2;
   clrscr();

   printf("Enter your name : ");
   gets(name);

   desc = malloc(200 * sizeof(char));
   if(desc == NULL)
   {
      fprintf(stderr, "Error - unable to allocate required memory\n");
   }
   else
   {
      strcpy(desc, "a computer programmer.");
   }
   printf("Name = %s\n", name );
   printf("Description: %s, %s\n", name, desc);
   len1 = strlen(desc);
   len2 = strlen("a computer programmer.");
   if(len1 == len2)
   {
      printf("\nDynamically allocated desc's length = %d\n", len1);
   }
   else
   {
      printf("Not possible..!!");
   }
   getch();
}

Here is the sample run of this C program:

c memory management

Same program can be written using calloc(), the only thing you need to replace malloc() with calloc() as follows:

calloc(200, sizeof(char));

Here is another program, showing how to resize and release memory in C:

/* C Memory Management - Dynamic Memory Management Program */

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

void main()
{
   char name[100];
   char *desc;
   clrscr();

   printf("Enter your name : ");
   gets(name);

   desc = malloc(30 * sizeof(char));
   if(desc == NULL)
   {
   fprintf(stderr, "Error - unable in allocating the required memory\n");
   }
   else
   {
      strcpy(desc, "a C programmer.");
   }
   desc = realloc(desc, 100 * sizeof(char));
   if(desc == NULL)
   {
      fprintf(stderr, "Error - unable to allocate required memory\n");
   }
   else
   {
      strcat(desc, "\nHe is getting strong idea about C Memory Management.");
   }
   printf("Name = %s\n", name );
   printf("Description: %s, %s\n", name, desc);
   free(desc);
   getch();
}

Here is the sample output of this C program:

c dynamic memory allocation

On trying the above example without re-allocating extra memory and strcat() function will give an error due to lack of available memory in description

C Online Test


« Previous Tutorial Next Tutorial »



© Copyright 2021. All Rights Reserved.

CodesCracker