Heap (Computer Systems)
A heap is a region of memory that is dynamically allocated during runtime. This means that a heap is used to store objects created at runtime instead of objects stored on the stack, which are created and destroyed automatically as the program runs. Heaps are often used to store things that need to be accessed by multiple parts of a program or entities that need to be allocated and freed at different points in time.
For implementing a heap, we must consider the following.
- Speed: how fast is the heap?
- Memory Utilization: how concise is the stored memory?
- DX
Dynamic Memory Allocation in C
There are two types: malloc and calloc.
The main difference between malloc and calloc is that malloc allocates a memory block with a specified size.
In contrast, calloc allocates a block of memory for an array of elements and initializes each factor to zero.
calloc also takes the number of elements to assign as a parameter.
On the other hand, when releasing the memory, we can use free(void *ptr)
To indicate the end of the heap, the kernel maintains a brk pointer.
brk pointer will hold the virtual address of the end of the heap.
In Linux, the brk pointer will be adjusted with sbrk(intptr_t increment)