DEV Community

Cover image for Oh my God, WTF is Stack
Akira
Akira

Posted on

Oh my God, WTF is Stack

I have a short definition I'm writing.

Is this right?? Close??? Am I getting warmer???

"The stack is a temporary data structure used to temporarily store and retrieve data; and the heap is a temporary storage space that holds objects, etc, that we use in our program, it is dynamic and can change. The stack can also refer to the area allocated in memory where the stack data is stored."

Comments and corrections most welcome!

Top comments (1)

Collapse
 
cameronspickert profile image
Cameron Spickert

This is a bit tricky, because the terms are overloaded. Stacks and heaps are types of data structures, but “the stack” and “the heap” have special meaning in the context of a running program. Further complicating things, both store objects and data.

Here’s how I would differentiate between “the stack” and “the heap”: objects and data stored on the stack are typically only available in the context of a particular function call, like arguments and local variables. Objects and data stored in the heap are available across function calls.

The distinction makes more sense in lower level languages, like C, where memory management is more explicit. In a language like JavaScript, it’s less consequential.

Let me know if that helps, happy to go into more detail if needed!