Comment on page
Heap is a memory region allotted to every program. Unlike stack, heap memory can be dynamically allocated. This means that the program can 'request' and 'release' memory from the heap segment whenever it requires. Also, this memory is global, i.e. it can be accessed and modified from anywhere within a program and is not localized to the function where it is allocated. This is accomplished using 'pointers' to reference dynamically allocated memory which in turn leads to a small degradation in performance as compared to using local variables (on the stack).
stdlib.hprovides with standard library functions to access, modify and manage dynamic memory. Commonly used functions include malloc and free:
// Dynamically allocate 10 bytes
char *buffer = (char *)malloc(10);
printf("%s\n", buffer); // prints "hello"
// Frees/unallocates the dynamic memory allocated earlier
The documentation about 'malloc' and 'free' says:
- malloc:/*malloc(size_t n)Returns a pointer to a newly allocated chunk of at least nbytes, or null if no space is available. Additionally, onfailure, errno is set to ENOMEM on ANSI C systems.If n is zero, malloc returns a minimum-sized chunk. (Theminimum size is 16 bytes on most 32bit systems, and 24 or 32bytes on 64bit systems.) On most systems, size_t is an unsignedtype, so calls with negative arguments are interpreted asrequests for huge amounts of space, which will often fail. Themaximum supported value of n differs across systems, but is inall cases less than the maximum representable value of asize_t.*/
- free:/*free(void* p)Releases the chunk of memory pointed to by p, that had beenpreviously allocated using malloc or a related routine such asrealloc. It has no effect if p is null. It can have arbitrary(i.e., bad!) effects if p has already been freed.Unless disabled (using mallopt), freeing very large spaces willwhen possible, automatically trigger operations that giveback unused memory to the system, thus reducing programfootprint.*/
It is important to note that these memory allocation functions are provided by the standard library. These functions provide a layer between the developer and the operating system that efficiently manages heap memory. It is the responsibility of the developer to 'free' any allocated memory after using it exactly once. Internally, these functions use two system calls sbrk and mmap to request and release heap memory from the operating system. This post discusses these system calls in detail.