How Dynamically Allocate malloc in Heap Area using brk (keyword) Function? Malloc is a C programming language Standard Library function that allocates memory dynamically. The malloc function allocates the memory based on the request.
The malloc function allocates the memory on the heap and when we use the malloc function we need to pass the size of the memory to be allocated to the program.
But, when the memory is allocated dynamically, the memory is allocated on the stack. The stack is the temporary area of memory that is used to save the data. When we allocate the memory on the stack, the stack is used for the memory allocation, and once the memory is done, it gets released.
Dynamically allocating malloc in heap area: When we talk about heap area, it means memory allocated by the system, which is used for dynamic memory allocation.
Let us see what exactly malloc is and how dynamically allocating malloc in heap area works.
Malloc: It is a function that is used to allocate a piece of memory. It is used in C programming language and it is a built-in function. We use malloc to allocate memory to store data.
Heap Area: It is the region of memory where the program stores its data. It is used for dynamic memory allocation. Heap is an area of memory used by a process, which can be allocated or deallocated dynamically. In other words, we can say that it is a space for storing data. But the problem is, we don’t know how much heap is available to the process. To determine the amount of heap available to the process, we can use the ‘brk’ system call.
The ‘brk’ system call is used to adjust the size of the heap, which is allocated dynamically to a process. We can dynamically allocate memory to the process by calling this system call.
A typical function of malloc is to allocate memory to a process. And when we want to free the allocated memory, we will call the free function.
But this function will return a value of 0 for successful allocation, and a negative value for unsuccessful allocation. When the allocation is successful, the memory is allocated and it returns the address of the first element of the newly created block.
So, how we can dynamically allocate the malloc in Heap Area?
For this, we can use the brk (keyword) function. It is a system call which is available in Linux operating systems. The brk function is used for allocating the memory on the stack.
This system call is used for increasing the size of the stack.
We can use the system call to allocate the memory in the heap.
Here is an example for the brk function:
int main() {
char *x = malloc(sizeof(char));
printf("%d\n", x);
return 0;
}
This example is to allocate the memory dynamically. We can allocate the memory on the stack and can pass the size of the memory to be allocated to the program.
In this case, we have allocated the memory in the stack and the program is executing the code. Once the code finishes the memory gets released and the memory allocated is freed.
So, this was all about “How Dynamically allocate malloc in heap Area using ‘brk’ (keyword) system call”. We can allocate the memory on the stack using the brk system call.
What is the output of the following C program?
#include
#include
int main()
{
int *p = NULL;
p = (int*)malloc(sizeof(int));
*p = 10; free(p);
int * q = (int*)malloc(sizeof(int));
*q = 25;
printf("%d, %d\n", *p, *q); return 0;
}
Output: 25, 25
The memory allocated to malloc is allocated dynamically within the heap space using brk (keyword) method of the system.
- The brk points to the unallocated (unallocated) memory pool. When we invoke the malloc() function in the brk pointer, it allocates memory from 4B to ‘p which is then shifted left, and
- Additionally assigns 10 as the value for that allocated memory.
- The memory allocated by malloc gets free and the contents of it become null or zero while brk is returned to its original position. You can see that ‘p’ remains at that same location. The malloc function was used() for memory allocation to the ‘q’. If we attempt to access the contents of the pointer ‘q’ it returns 25 exactly as we is expected. If we try to look up the contents of the pointer ‘p’ it returns the same information in the form “q”. This isn’t intended from “p” since we’ve already cleared the ‘p’.
However, what actually transpired was that we freed all memory in the heap however, p is still pointing to a memory address or location since p is an integer pointer variable. This “p” is known as a floating pointer.
The dangling pointer within the program is eliminated by making the statement p = NULL immediately following an free(p) expression.
In conclusion, the malloc function is used to allocate the required memory and the ‘brk’ (keyword) is used to increase the allocated memory.