C Programming: Dynamic memory allocation

‘C’ Programming provides functions to create and release memory dynamically. There are two types of functions ‘C’ provides:

  • One type of functions allocates memory in the heap.
    • malloc
    • calloc
    • realloc
  • Other type of function(s) release the memory from the heap.
    • free

Now you will get the doubt; why release memory functions are required? Why don’t ‘C’ release the memory automatically whenever not required; like JAVA does?

Your question is valid. But ‘C’ is not supporting Garbage Collection like JAVA does; hence it is the responsibility of the programmer to release the memory whenever the memory is not required. Once the memory is released properly, the Operating System utilize the memory for other processes, if required.

What is heap? When the program started running; Operating System allocates some memory for it and start executing the program. How much memory does Operating System allocates? Its depending on the size of the program. If the size of the program is more, Operating System will allocate more memory. If the size of the program is less, Operating System allocates less memory. Program size is depending on its data. As you aware, when you declare a variable in your program; it is required to allocate a memory for it. The memory is depending on the type of the variable. Usually all these variables and other data is stored within the program memory area; which is called stack memory. Heap memory is the memory resides outside of the program area. As this is not within the program memory; accessing heap memory will take a little bit more time compare to accessing stack memory.

All the functions we mentioned at the starting of this article are deals with the heap memory. Lets look at the allocating functions first.

Following are the functions deals with allocating memory on the heap:

First one we should discuss is malloc function. The syntax of the function is:

void* malloc(size_t size);

where size is the size in bytes we want to allocate the memory. Once it is successfully allocated the memory, this function will returns the address of the allocated memory. If it fails to allocate the memory; simply it returns NULL. The allocated memory is not initialized. That means the values in allocated memory are garbage values.

Second one is calloc function. It returns the allocated memory address upon successful allocation. Otherwise it will return a NULL. The syntax of the function is:

void* calloc(size_t n, size_t size);

Unlike malloc function, calloc allocates memory and initialize the memory with the value 0. calloc allocates ‘n’ elements with each element of size size bytes. Fr eg: if you want to allocate 10 elements, each element has a size of 8 bytes; you can call the function as calloc(10, 8); It will allocate the memory, initialize the memory with the value 0 and returns the allocated memory address.

Another memory allocation function is realloc. realloc function allocates or reallocates the memory. The syntax of realloc function is:

void* realloc(void *ptr, size_t size);

where *ptr is the pointer to the previously allocated memory or it may be NULL. size is the size of the memory in bytes. Does it allocate and reallocate the memory? Yes. If we pass ptr value as NULL, realloc function allocates the memory and returns the allocated memory address. If the ptr value is previously allocated memory address, it will reallocate the memory depending on the value we pass through its second argument size. If the size value is less than the previously allocated size; realloc will shrink the previously allocated memory. If the size value is greater than the previously allocated size; realloc will expanded the previously allocated memory. On success realloc returns the allocated memory. If the memory location changed; address of previously allocated memory is no longer valid.

Now we have to look into the function which deals in freeing up the allocated memory on the heap:

free is the function ‘C’ use to free or release the memory which was allocated through any of the memory allocated function which we discussed above. The syntax of the function is:

void free(void *ptr);

where ptr is the previously allocated valid memory address. The memory address must be valid; other wise your program will display a core dump. free frees the memory and the freed memory may allocate by the Operating System for other processes.

Lets put altogether in a simple example:

// sample.c
#include <malloc.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void main()
{
   char *ptr = (char *)malloc(20);
   if ( ptr == NULL )
   {
      printf("ERROR: Unable to allocate memory through malloc().\n");
      exit(0);
   }

   printf("ptr Content: %s\n", ptr);

   char *ptrnew = (char *)realloc(ptr, 50);
   if ( ptrnew == NULL )
   {
      free(ptr);

      printf("ERROR: Unable to re-allocate memory through realloc().\n");
      exit(0);
   }

   printf("ptrew Content: %s\n", ptrnew);

   free(ptrnew);

   char *ptrinit = (char *)calloc(20, sizeof(char));
   if ( ptrinit == NULL )
   {
      printf("ERROR: Unable to allocate memory through calloc().\n");
      exit(0);
   }

   printf("ptrinit Content: %s\n", ptrinit);

   free (ptrinit);
}

Compile and run the program. Observe the results. The memory allocated by malloc & realloc may produce garbage data; where as calloc will not display anything because calloc initialized the memory with 0.

// Malin

Leave a Reply