Hello and welcome.
Let us continue our discussion on memory allocation in Linux systems when the dynamically allocated
memory is utilized and we no more require the allocated memory, we need to explicitly free the memory.
In order to free the memory, we need to use free function call.
In free function, we pass the pointer to the previously dynamically allocated memory using malloc or
calloc or realloc, and free function frees the memory.
So once the chunk of memory is freed, we cannot use it for any further memory operations.
All the memory that is allocated using malloc or calloc or realloc needs to be freed.
As an important note, the free does not lower the program break.
It adds the freed block of memory to the list of free blocks.
This free block can be used for future dynamic memory allocation calls.
The block of memory being freed can be in the middle of the heap rather than the end of the heap, so
lowering the program break is not done during free.
Now let us discuss one more library function called as alloc A.
Until now we have seen that dynamically mem memory is allocated on the heap.
Now there is a function call called as alloc A which does dynamic memory allocation.
But this memory segment is not allocated on heap.
Rather it is allocated on the stack of the corresponding function.
Let us see the syntax of alloc a.
Here we need to pass the number of bytes required, and it returns the memory location of the allocated
memory on the stack.
Let us say we call a function, and in that function we need to do some dynamic memory allocation.
And instead of malloc or calloc, if we call alloc A, the required memory bytes will be allocated on
the stack frame of the function.
And after performing the required operation, the function returns and this will delete the stack frame.
Hence, freeing is not required as compared to the dynamically allocated memory, such as malloc or
calloc, but care needs to be taken when using malloc, as we may run into issues of stack, as if the.
If we allocate more memory onto the stack, the stack grows and it may cause stack overflow.
We have to use alloc only if the memory allocation is very small, or when it is safe to use other allocations,
such as malloc or calloc.
Now we will have a small look into the manual pages of the function calls that we have discussed.
Let's refer to the manual pages.
These are the below commands we need to type on the terminal for manual page of break.
We have man 2BRK.
Similarly for s break we have man two followed by s b r k and for malloc we have man followed by malloc.
Similarly, for calloc realloc and alloc we can use the manual pages.
Now let's have a small glance of these manual pages so that it will be of useful for us.
I would recommend to further read these manual pages to get a good understanding of the system calls
and library functions that we have discussed.
Man two.
Burke displays the manual pages of the brake and it explains how to use the brake system call.
We can refer to the description its include file required for PRK here.
Now let's open Sbcc manual page.
Man two Sbcc.
We can refer to the description.
Its include files required for Sbcc.
Here.
Then we have manual pages of malloc, calloc, realloc and free in the same page.
So this gives a good description of how to use malloc, calloc realloc and free.
I would recommend to have a glance in the manual pages for more details on these function calls.