Both are parts of memory available to your code to use at runtime.

The stack stores values in order it gets them and removes in the opposite order.

All data pushed onto the stack must have a known fixed size.

Data with the unknown size at compile time or a size that might change must be stored on the heap instead.

The heap is less organized. The memory allocator finds (allocating on the heap) an empty spot big enough, marks it as being in use, and returns a pointer to a memory address. The pointer is then pushed onto the stack because it has a fixed size.

Pushing onto the stack is faster than allocating on the heap.

Accessing data on the stack is also faster, because we don’t need to follow a pointer in that case.