C Programming language

adplus-dvertising
C Programming Dynamic Memory Allocation Functions
Previous Home Next
malloc() function

The malloc() function allocates a block of memory in bytes. The programmer should explicitly give the block size it requires to use, we can consider the malloc() function as a request to the RAM of the system to allocate memory and if request is granted then it will return a pointer( of type void, means we can assign it any type of pointer) which will point to the first block  of  that memory, if not the malloc() function will return NULL. The malloc() function is available in <malloc.h> header file.

The basic syntax for allocating memory using malloc() function is :

malloc (number of elements * size of each element);

for eg.

int *ptr1;
/*
 * here size refers to the size of the data type like int, float etc., here sizeof (int) will
 * return 2 bytes as integer takes in C 
 */

ptr1 = malloc(7 * sizeof(int));     

As told earlier that malloc() function returns a void type of pointer so we should type cast it properly according to our requirement.

As in above eg. we have taken an int pointer so we can reform the above declaration as :

ptr1 = (int *) malloc (7 * sizeof(int));

After the execution of this statement a block of 14 bytes consecutively will be created in the memory, as integer takes 2 bytes in the memory and we have defined 7 elements of integer type.

Similarly for allocating memory for a structure variable we can do it in following way :

#include <stdio.h>
#include<conio.h>
struct employeeinfo
{  
  int emp_id;
  char name[21];
  char sex;
  float salary;
} ;
struct employeeinfo *ptr1;

The above is the declaration of a structure and it can be allocated memory via malloc() in the following manner :

ptr1 = (struct employeeinfo *) malloc (sizeof(employeeinfo));

When the above statement is executed a block of 28 bytes will created in the memory viz. 2 bytes for integer, 21 bytes for name element , 1 byte for sex element, and 4 bytes for salary element of the structure.

calloc() function

The calloc() function is quite very much similar to malloc() function except for the difference that calloc() takes two arguments against the one argument passed in the malloc() function.

The following shows how we can allocate memory using calloc() function

int ptr1;
ptr1 = (
int
*) calloc( number of element required, size of each element);

Eg. to create a memory  block for 7 elements each requiring 2 bytes of storage, we can allocate memory using calloc() as

ptr1 = (int *) calloc (7, 2);
free() function

The free() function is mainly used for deallocating the memory which has been allocated previously. When your function is finished with dynamically allocated memory you must deallocate it by using free(), as memory is a vital resource and should not be wasted since it is limited resource which should be kept in spare for future use.

You can free an allocated memory by the following syntax :

ptr1 = (int *) calloc (7, 2);
free(ptr1);

In above code we have deallocated a block of 14 bytes from the memory.

realloc() function

This function is used to reallocate or change the size of the memory block that has been created using via malloc() or calloc(). This function is generally used in two situations :

  • When the block of memory allocated does not have sufficient space to perform a certain funtion.
  • When the block of memory allocated have galore space to perform a certain function which will lead to memory wastage.

In other words, we can say that realloc is a precise and efficient way of allocating memory and can be done in following manner :

ptr1 = (int *) realloc(ptr1, new size in bytes);

In the above illustration, we are reallocating the memory allocated to an integer type pointer by calling realloc on ptr1 and specifying its new size in bytes. But please do remember, it must be first allocated memory by using the malloc() function as,

int ptr1;
ptr1 = (
int *) malloc(7 * sizeof(int
));

An example showing how to use above mentioned functions in a program

#include <stdio.h>
#include<conio.h>     /*please do include header files <malloc.h> and <alloc.h> in this program */
#define NULL 0       /* here we have defined the NULL value equals to 0  */
void main()
{
char *msg;         /* declared a character type pointer which is capable of holding 1 byte of memory */
clrscr();
msg = (char *)malloc(21 * sizeof(char));     /* allocated memory to msg via using malloc() function */
strcpy(msg, "hi ! how r U");                 /* copying a random message to msg */
printf("\nThe current message is %s", msg);  /* displaying the message contained in msg */
msg = (char *)realloc(msg, 30);              /* reallocating the memory to msg*/
strcpy(msg, "hey r u there ?");              /*copying a new random message to msg */
printf("\nNow the message is %s", msg);     /* displaying the new message contained in msg */
free(msg);             /* now we are freeing the memory allocated to msg using free() function */
getch();
}

Output : The output of the above program will the following

The current message is hi ! how r U

Now the message is hey r u there

Previous Home Next