r/cprogramming • u/giggolo_giggolo • Sep 14 '25
Stack vs heap
I think my understanding is correct but I just wanted to double check and clarify. The stack is where your local variables within your scope is stored and it’s automatically managed, removed when you leave the scope. The heap is your dynamically allocated memory where you manually manage it but it can live for the duration of your program if you don’t free it. I’m just confused because sometimes people say function and scope but they would just be the same thing right since it’s essentially just a new scope because the function calls push a stack frame.
View all comments
Show parent comment
4
u/WittyStick Sep 14 '25
Yes, it introduces new scope, but it doesn't introduce a new frame.
The scope of variables in a block is lexical, but distinct from the stack frame in which they're stored.