Stack is like your local variables, it only lives within the current scope and once that goes out of scope then it’s gone. A heap is your dynamically allocated memory, I lives for aslong as your program is running or until you free it. Is my understanding correct? I’ve seen the stack be related to functions aswell but then functions are essentially their own scope because of the stack frame right? I just want to make sure I am understanding the difference
#Difference between stack vs heap
32 messages · Page 1 of 1 (latest)
When your question is answered use !solved to mark the question as resolved.
Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.
the heap is basically what you allocate with malloc/realloc
though from the perspective of C itself (as a language), there is no real difference. memory is either static (it exists for the entire lifetime of the application -- this is typically the DATA section), automatic (typically local variables, function argument included -- it is stack but it could also be register, in practice), or none of the previous (typically the memory returned by malloc/realloc -- aka your heap)
But variables in my functions and scopes are on the heap unless I dynamically allocate them, so I am confused on how they’re the same
Stack is like your local variables, it only lives within the current scope and once that goes out of scope then it’s gone.
yea the program manages the memory for you
A heap is your dynamically allocated memory, I lives for aslong as your program is running or until you free it.
yes, if you forget to free it, the OS will do it for you on program exit/termination
I’ve seen the stack be related to functions aswell but then functions are essentially their own scope because of the stack frame right?
when you call a function, the program creates a stack frame, which contains
- return address to the function that called it
- arguments for the called functions
- local variables
they are typically on the stack
Sorry I meant stack yes
So I am confused on what you mean by they’re the same thing
never said that -- I said from the perspective of the language, there is no difference between stack/heap : what matters is the life time management
automatic memory vs non automatic
as said above, what is not automatically handled, typically the resulf of malloc/realloc, needs to be freed, and that typically coincides with the "heap" in a modern computer
you could imagine a computer that works with the same memory for both automatic and non automatic storage. WASM does that, typically
Okay yea, you said that there’s no difference, but then what does that mean? You say there’s no difference and then state that it’s automatically vs non automatic memory
there is a difference between the stack and the heap, but not from the perspective of C
from the perspective of C, some memory is automatically managed (aka it is collected when scope ends and recycled), and there is memory that is not automatically managed: you have responsibility to free it in order to dispose it
now in practice, in modern computers, automatic memory is the stack, and the rest is the heap
though it is really not accurate, because it does not take into account DATA section of the program, nor registers, for example...
no , variables in your functions (and your whole function) are on the stack unless you explicitly use malloc/realloc to get memory from heap
Yea okay. So the stack is function and scope?
im not sure what u mean by that
whenever your function executes , it goes into a stack frame (on the stack) and all the varaiables etc in it are created on the stack
basically yes, but again it poorly depicts the situation
you might have some local variables that do not go in the stack but directly on registers
you might have some variables that are in a scope but declared static, and go in the DATA section
there is no "obvious" rule for what goes in the stack and what goes in the heap, because those concepts do not exist in C. the language speaks about automatic memory, static lifetime, ... for example. it does not talk about stack, heap, ...
Wel I’m just confused now, mainly because I guess I never really looked into terminology
A function is basically just a new scope right
"scope" is more like a concept used to describe a small "environment"
a simple {} makes a scope. the scope starts at { and ends at }
it doesnt have to be a function , a scope can be anything
so lets get that terminology out of confusion
maybe tell us what you understand what stack and heap is and maybe we can tell you what you are missing
I think OP's first description was "accurate" enough
among other things, yes
but if you do this for example:
void f(void) {
// new scope
static int counter = 0;
printf("Counter value is %d\n", counter);
counter++;
}
int main(void) {
f();
f();
f();
f();
}
you'll see that counter variable is only visible within your function scope (it cannot be reached from the outside). however, that one object never dies and is not reallocated when the scope begins again: the object lives forever and is incremented all over again
that object does not really go on the stack. it actually lives in the DATA section, typically