I think I might be asking a very wrong question, but I really tried to understand it by googling, but with no luck.
As we know, we have a stack and heap. Heap for the dynamically allocated ones, stack for local variables and e.t.c.
Let's say I have the following c++ code.
void bla(int v1, int v2, int v3) {
int g = v1 + v2+ v3;
}
void nice(int g){
int z = 20;
int k = 30;
bla(g, z, k);
}
int main(){
cout<<"Hello World";
nice(40);
}
Now, let's imagine there's a stack. I understand that for example values z,k,g
will be stored on stack. But when I call the function nice
which calls bla
where are those stored ? I've read that each function execution causes call stack size to increase by 1. I'd say that even creating local variables also causes call stack to be increased by 1.
So, how are those(callstack
, stack
) related at all ?
Here is my assumption:
When we call nice
, completely new stack
gets created. In there, we store z and k
. When nice
calls bla
, now another stack
gets created for bla
and this second stack stores v1,v2,v3,g
. and so on. each function needs its own callstack
,but we can call this stack
too.