Memory Allocation Functions :-
The process of allocating memory at runtime is known as dynamic memory allocation. Library routines known as "memory management functions" are used for allocating and freeing memory during execution of a program. These functions are defined in stdlib.h. Dynamic memory allocation in c language is possible by 4 functions of stdlib.h header file.
1. malloc() :- allocates single block of requested memory.
2. calloc() :- allocates multiple block of requested memory.
3. realloc() :- reallocates the memory occupied by malloc() or calloc() functions.
4. free() :- frees the dynamically allocated memory.
Memory Allocation Process :-
Global variables, static variables and program instructions get their memory in permanent storage area whereas local variables are stored in area called Stack. The memory space between these two region is known as Heap area. This region is used for dynamic memory allocation during execution of the program. The size of heap keep changing.
1>malloc()
malloc stands for "memory allocation". The malloc() function allocates single block of requested memory at runtime. This function reserves a block of memory of given size and returns a pointer of type void. This means that we can assign it to any type of pointer using typecasting. It doesn't initialize memory at execution time, so it has garbage value initially. If it fails to locate enough space (memory) it returns a NULL pointer.
syntax
ptr=(cast-type*)malloc(byte-size)
Example
int *x; x = (int*)malloc(100 * sizeof(int));
free(x);
2>calloc()
calloc stands for "contiguous allocation". Calloc() is another memory allocation function that is used for allocating memory at runtime. calloc function is normally used for allocating memory to derived data types such as arrays and structures. The calloc() function allocates multiple block of requested memory. It initially initialize (sets) all bytes to zero.If it fails to locate enough space( memory) it returns a NULL pointer. The only difference between malloc() and calloc() is that, malloc() allocates single block of memory whereas calloc() allocates multiple blocks of memory each of same size. Syntax ptr = (cast-type*)calloc(n/number, element-size); calloc() required 2 arguments of type count, size-type. Count will provide number of elements; size-type is data type size
Example
int*arr; arr=(int*)calloc(10, sizeof(int));
calloc(50, siceof(char));
3>realloc()
If the previously allocated memory is insufficient or more than required, you can change the previously allocated memory size using realloc(). If memory is not sufficient for malloc() or calloc(), you can reallocate the memory by realloc() function. In short, it changes the memory size. By using realloc() we can create the memory dynamically at middle stage. Generally by using realloc() we can reallocation the memory. Realloc() required 2 arguments of type void*, size_type. Void* will indicates previous block base address, size-type is data type size.
Realloc() will creates the memory in bytes format and initial value is garbage.
syntax ptr=realloc(ptr, new-size)
Example
int *x;
x=(int*)malloc(50 * sizeof(int));
x=(int*)realloc(x,100);
4> free()
When your program comes out, operating system automatically release all the memory allocated by your program but as a good practice when you are not in need of memory anymore then you should release that memory by calling the function free(). The memory occupied by malloc() or calloc() functions must be released by calling free() function. Otherwise, it will consume memory until program exit
Syntax:
free(ptr);