Memory Management functions malloc(), calloc(), and realloc() with examples

The compiler allocates memory for the local variables on the stack. And there is a possibility that we can allocate memory on the heap using the memory allocation functions

Stack memory allocation:

The stack memory is allocated when the local variable is found by the compiler and freed when the variable goes out of the scope

The memory is de-allocated when the scope of the variable goes out of scope.
The address is allocated statically for the local variables and the function arguments.

Heap memory allocation:

The heap memory is allocated when we allocate the memory using malloc / calloc/ realloc and freed when we call the function “free”.malloc is a function that finds a chunk of free memory of the desired size and returns a pointer to it. free marks the memory associated with a specific address as no longer in use.

How malloc and free functions works?

The malloc() function takes the integer as an argument to specify the bytes of memory to be allocated and void* as a return value. So we need to typecast it as per our requirement. The free() function accepts a pointer to the space for its argument.

Let us see the worked example to understand the malloc and free functions:

The output of the above program is

If the memory is not allocated using the function malloc, it returns NULL.

calloc function :

calloc allocates the requested memory and returns a pointer to it. calloc initializes allocated memory to zero., but using malloc it is not possible to set the allocated memory to zero.
• The calloc() takes two integer arguments. These are multiplied together to specify how much memory to allocate.

Syntax of the calloc function:

Here are the parameters
nitems — This is the count of elements to be allocated.
size — This is the size of elements.
This function returns a pointer to the allocated memory, or it returns NULL if the request fails.

calloc function with example:

Output is;

What is the difference between the malloc & calloc?

As discussed earlier calloc allocates the requested memory and returns a pointer to it. calloc initializes allocated memory to zero., but using malloc it is not possible to set the allocated memory to zero.

realloc function:
the memory allocated by malloc / calloc can be resized using reallco function.

Below are the few points that we need to understand about the realloc function.
• The contents of the object will remain unaltered up to the lesser of the new and old sizes.
• If the new size of the memory object would require movement of the object, the space for the previous instantiation of the object is freed.
• If the new size is larger, the contents of the newly allocated portion of the object are unspecified.
• If the size is zero and ptr isn’t a null pointer, the object pointed to is freed.
• If the space can’t be allocated, the object remains unaltered.
• If ptr is a null pointer, realloc() acts like malloc() for the specified size.

Syntax of the realloc:

Return value:
 realloc() returns a pointer to the new space, or NULL if the request cannot be satisfied.

Example for realloc:

Output is as shown below:

free function:
The free function is used to free the previously allocated memory using malloc / calloc/realloc, it takes the pointer to a heap block and returns it to the free pool for later reuse

syntax of the free is

Points we need to remember about the free is:
call to free() does not need to give the size of the heap block – the heap manager will have noted the size in its private data structures.
The call to free() just needs to identify which block to deallocate by its pointer. If a program correctly deallocates all of the memory it allocates, then every call to malloc() will later be matched by exactly one call to free().