Heap Definition

Learn about the heap memory area and its usage.

We'll cover the following

Introduction

Recall that the result of the code compilation process is an executable file. It contains various sections, such as the following:

  • Static sections
    • They physically exist inside the executable file. At runtime, they are loaded into memory.
    • Examples: .text, .data, .rodata, and .bss
  • Dynamic sections
    • They don’t physically exist inside the executable file. The executable file only stores a reference to memory areas.
    • The operating system allocates these memory areas and alters the references inside the executable file to point to the allocated memory.
    • Examples: .stack and .heap

The stack and the heap share the same memory area but start at different ends. The stack starts at the top and grows downwards, and the heap begins at the bottom and grows upwards.

The stack grows downwards during push operations, which makes room for a variable. Similarly, the heap grows when we request some memory (more on this later). Both the stack and the heap have a maximum size, which ensures that they can’t grow indefinitely and overlap.

See the following memory drawing:

Get hands-on with 1200+ tech skills courses.