Memory management in c is done using calloc( ), malloc( ), realloc( ) and free( ) functions. These functions can allocate, reallocate, deal locate and free memory during runtime.
Allocation, deallocation of memory during runtime is known as dynamic memory management. It means calloc( ), malloc( ), realloc( ) and free( ) are the functions which are used for dynamic memory management.
List of memory management in c (functions with tasks):
|malloc( )||Allocate request size of bytes & return a pointer to the first byte of the allocated space. And contains garbage values.|
|calloc( )||Allocate space for an array of elements, initialize them to zero and returns a pointer to the first byte of allocated space|
|realloc( )||Modify the size of previously allocated space|
|free( )||Free the previously allocated space|
If required memory is not available in the heap or the function fails to locate the requested memory, then the memory allocation function returns a NULL pointer.
Memory management in c functions:
The malloc function reserve a block of memory of specified size & returns a pointer of type void this means that we can assign it to any type of pointer. Malloc allocates a block of contiguous bytes. The allocation fails if the space in the heap is not sufficient to satisfy the request. The space allocated by malloc is not initialized. The value of allocated space will be undetermined(i.e. garbage).
Syntax of malloc: malloc(size);
How to use malloc: For using malloc we use the following format.
ptr_name = (cast type *)malloc(byte_size);
during the execution of above statement malloc will request the heap to provide memory for storing data of cast type. If heap provides memory malloc will reserve it and will return the first byte of memory to the ptr_name.
The calloc function reserve multiple block of storage for n elements & returns a pointer of type void. calloc allocates multiple block of contiguous bytes. The allocation fails, if the space in the heap is not sufficient to satisfy the request. The space allocated by malloc is initialized to zero.
Syntax of calloc: calloc(n,element_size);
How to use calloc: For using calloc we use the following format
ptr_name = (cast type *)calloc(n,element_size);
during the execution of above statement calloc will request the heap to provide memory for storing n elements of element_size. If heap provides memory, calloc will reserve it and will return the first byte of memory to the ptr_name.
Usually realloc() function is used to modify the size of previously allocated memory. The realloc function deallocates the old memory space and returns a pointer to a new object. The content of the new object will be same as that of the old object prior to deallocation, if the new object is greater then the old object.
Syntax of realloc: ralloc(ptr,new_size);
How to use realloc: For using realloc we use the following format.
ptr_name = (cast type *)realloc(ptr_name,new_size);
during the execution of above statement calloc will dealloctes the old object(memory) pointer by the ptr_name and returns a pointer to a new object that has the size specified by new_size.
This function is used to free the memory that is allocated to pointer by calloc, malloc or realloc functions.
Syntax: free( ptr_name);
How to use: free(ptr_name);
The above statement will free the memory segment whose address is hold by ptr_name.
Memory Segments Management in c:
There are four memory segments where c program is stored.
- Data Segment: Variables that are defined outside all the functions(global variables) and variables defined inside a function (static variables) whose lifetime remains till the execution of the complete program are stored in the data segment.
- Stack Segment: Variable that is defined inside the block/local scope without using static storage class are stored in stack segment. It means all the automatic variables are stored in the stack segment. Stack always grows up.
- Heap Segment: This memory segment is used not to actually store the variables but to store the objects pointed to by pointer variables. It means the memory which is allocated to the pointers by calloc(), malloc() and realloc functions belong to the heap. Heap always grows down.
- Code Segment: whole program code is stored in code.
Related article: (Data structure)
- introduction to the data structure
- what is a pointer in c
- searching in the data structure
- sorting in the data structure
- linked list with operation and application
- queue and application of queue
- stack and application of stack
If you find any error in memory management in c then you can simply comment below with your problem.
Thank you so much!