C Memory Allocation
In this tutorial, you will learn about the mechanisms for the allocation of memory. As C is a structured language, it consists of a set of fixed rules for programming. Let us assume a situation where we have an array of a certain size. Sometimes thus declared size in the array may be insufficient or sometimes more than necessary. There may arise the situation of changing the size of the array. The Dynamic memory allocation comes into the play in such condition. Dynamic memory allocation is the process of allocation of memory allocation at the run time.
We can implement 4 library functions that come under the library <stdio.h>. These functions will enable the dynamic memory allocation or deallocation druing the program execution. These functions are malloc(), calloc(), realloc(), free().
What is Malloc() function in C?
We can allocate a block of memory using the malloc() function. The malloc() function reserves a block of memory of specified size and returns a pointer of type void. This means that we can assign it to any type of pointer. Following is the syntax of the malloc() function.
ptr = (datatype*) malloc (byte size);
In the above syntax, ptr indicates the pointer to an area of memory with size (byte size). If space is not sufficient then the allocation fails and returns a null pointer.
What is Calloc() function in C?
The process of allocation of memory by using calloc() function is normally a request to the multiple blocks of storage. These multiple blocks of statements are each of the same sizes and then it will set all bytes to 0. We can use this function normally while storing derived datatypes such as arrays and structures. The syntax for using calloc() function is given below.
ptr = (datatype*) calloc (n, byte size);\
The above statement allocates contiguous space for ‘n’ blocks each size of (byte size). All bytes are initialized to’0′ and pointer tot he first byte is returned. If there is no space the program will return the null pointer.
What is realloc() function in C?
Sometimes the memory which is allocated by malloc() and calloc() functions will be insufficient or excess. In both of these situations, we can change the memory size by using the realloc() function. Hence, this process is called reallocation of memory. We can reallocate the memory by using the following syntax.
ptr = realloc (ptr, new size);
What is free() function in C?
During the compile time, the storage of the variable can be further allocated or released automatically by the system. But, in the case of dynamic memory allocation, it is our responsibility to release the space when we do not require the storage. The release of the storage becomes significant when the storage is limited. This function can be done by using the library function free(). The following is the syntax for using the free() function.
The following links will take you to the examples using the functions above. These examples will certainly make you understand more about all these functions for dynamic memory allocation in C. Following links will implement each of the above functions.