Dynamic memory allocation


Sometimes it becomes restrictive to use fixed set of variables in a program. This is needed when we are creating variables at the time of execution. We need to decide the amount of space to be allocated for these variables of different types at execution time. This depends upon the input or the value that is to be stored in the variable. “Dynamic” actually means a constant change.

In C programming language we can define variables dynamically at compile time; these variables cannot be named in the program source code. When dynamic variables are created, they are identified by their address in the memory that is contained by a pointer. Writing flexible programs in C programming language becomes easy using the concept of dynamic memory and pointers.

In C language we have memory management functions which are used to allocate and to free the memory during the execution of program. These functions are defined in stdlib.h library file of C. These functions are as follow:

Function Description
malloc () It is used to allocate the requested size of the bytes and then return the void type pointer to point first byte of the space allocated.
calloc () It is used to allocate space for an array and to initialize it with zero then allocating space to it in memory.
free It is used to release the previous allocated memory.
realloc It is used to modify the size of the memory allocated previously.


Memory allocation process:

In C programming language, the global and static variables or program instructions are stored in permanent storage area. The local variables are stored in stack. The memory space between permanent storage area and stack is called heap area. The heap area is used for the dynamic memory allocation during the execution of the program.


Allocating block of memory:

At runtime to allocate the block of memory we use malloc () function. The malloc () function is use to reserve a block of memory of given size and then it returns the pointer of the given size. In this way we can assign any type of pointer by using the concept of type casting. A NULL pointer is returned if the malloc () function is not able to locate enough space.

Example using malloc () function:

Consider the following example in which we have used the malloc () function:

CODE:

int *y;

y = (int*) malloc (50 * sizeof (int));

free (y);

In the above example we have first declared a pointer of integer data type then we allocated a memory space to the variable. After this the free function is used to release the memory allocated to the variable.

There is another function calloc () which is used to allocate the memory at run time. We mostly use the calloc () function to allocate memory to the data types that are derived. This may include arrays and structures. NULL will be returned if calloc () fails to locate enough space.

Example using calloc ():

Consider the following example in which we have used the calloc function:

CODE:

struct student

{

 char *name;

 int fee;

};

typedef struct student stu;

stu *s1;

s1 = (stu*) calloc (20, sizeof (stu));

Then we have the realloc () function which is used to change the memory size, this memory is already allocated to a variable.

Example using realloc ():

Consider the following example in which we have used the realloc () function:

CODE:

int *y;

y = (int*) malloc (10 * sizeof (int));

y = (int*) realloc (y, 20);

In the above example first the variable y is allocated memory then we changed the memory by using realloc () function.


Difference between malloc and calloc:

The following are some of differences between these two functions:

calloc () malloc ()
This function is used to initialize memory with 0 value. This function is used to initialize memory with garbage value.
The number of arguments to be passed in this function is 2. The number of arguments to be passed in this function is 1.
The following is the syntax of calloc ():

(cast_type *) calloc (blocks , size_of_block);

The following is the syntax of calloc ():

(cast_type *) malloc (Size_in_bytes);