Heap memory

What is Heap?

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).

Using dynamic memory

stdlib.h provides with standard library functions to access, modify and manage dynamic memory. Commonly used functions include malloc and free:

  1. // Dynamically allocate 10 bytes
  2. char *buffer = (char *)malloc(10);
  3. strcpy(buffer, "hello");
  4. printf("%s\n", buffer); // prints "hello"
  5. // Frees/unallocates the dynamic memory allocated earlier
  6. free(buffer);

The documentation about ‘malloc’ and ‘free’ says:

  • malloc:

    1. /*
    2. malloc(size_t n)
    3. Returns a pointer to a newly allocated chunk of at least n
    4. bytes, or null if no space is available. Additionally, on
    5. failure, errno is set to ENOMEM on ANSI C systems.
    6. If n is zero, malloc returns a minimum-sized chunk. (The
    7. minimum size is 16 bytes on most 32bit systems, and 24 or 32
    8. bytes on 64bit systems.) On most systems, size_t is an unsigned
    9. type, so calls with negative arguments are interpreted as
    10. requests for huge amounts of space, which will often fail. The
    11. maximum supported value of n differs across systems, but is in
    12. all cases less than the maximum representable value of a
    13. size_t.
    14. */
  • free:

    1. /*
    2. free(void* p)
    3. Releases the chunk of memory pointed to by p, that had been
    4. previously allocated using malloc or a related routine such as
    5. realloc. It has no effect if p is null. It can have arbitrary
    6. (i.e., bad!) effects if p has already been freed.
    7. Unless disabled (using mallopt), freeing very large spaces will
    8. when possible, automatically trigger operations that give
    9. back unused memory to the system, thus reducing program
    10. footprint.
    11. */

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.