What Is the Difference Between Stack and Heap?

When you’ve been coding for some time, you’ll start to notice that people use these two terms called “stack” and “heap”. And you’re confused thinking, “What is the difference between stack and heap?”

When you see these words, start thinking “memory”. The stack and the heap are two different kinds of memory that your program uses while it’s running. It’s important to know the differences because it affects the way that you code your program.

This is why I recommend that every programmer should learn C. It gives you a good foundation for understanding how the stack and the heap are used in your program. C is considered a low level language, but it’s actually the highest level language that allows you to have manual control over the stack and the heap.

Even if you’re planning to only code in the higher level languages, it’s still important to have a basic understanding and good intuition of the stack and the heap. It makes you a better developer and helps you write more efficient code. It also allows you to debug issues if you run into memory issues.

So in this video, I’ll be going over:

  • The differences between the stack and the heap so that you’re not confused anymore when you read about it online.
  • One example in C showing you how you can manually manage your memory on the stack and the heap.
  • A second example in Python which shows how understanding the stack and the heap really helps you write more efficient code.

Thank you for watching, and happy coding! 💻🧡
-Henrik