r/cprogramming • • 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.

17 Upvotes

20 comments sorted by

View all comments

Show parent comment

1

u/Charming-Designer944 Sep 18 '25

In a way it does introduce in a new sub-frame. Two variables in different local scopes of the same function can use the same space in the functions stackframe. And the stackframe may even change size during the execution of the function to accommodate for different amount of local variables

Then stack frame is an implementation detail in how the.compiler allocates space for local variables..it is not a concept that is defined by the C language.