/
What is the heap?

What is the heap?

The heap is where we can store things which need to exceed the lifetime of a function call. Typically in C we would allocate a block of memory using the “malloc” command. After we are done using the memory we have to remember to call “free” and give the memory back to the operating system.

If we do not remember to do this, then we are said to have “leaked” memory.

At a high level implementing malloc involves keeping collection of free blocks of memory in the heap - there is time involved in finding these blocks of memory. It’s a lot more work than allocating memory on the stack.

 

Related content

Heap and stack - understanding memory allocation
Heap and stack - understanding memory allocation
More like this
How does malloc work?
How does malloc work?
More like this
C/C++Stack Variables
C/C++Stack Variables
More like this
What is the stack?
What is the stack?
More like this
Smart Pointers
Smart Pointers
More like this
What are threads?
What are threads?
More like this