What is call stack?

Table of contents

No heading

No headings in the article.

Important points about call stack-

  • every time a function is called, it gets created in the stack memory.
  • All the local variables belonging to that function are part of the stack memory.
  • Once the function call is over the stack frame is cleared off.
  • The currently executing function is always at the top of the stack.

let's try to understand each point in detail.

1) every time a function is called, it gets created in the stack memory.

for example when you call the main function

int main(){
int x;
return 0;
}

the main function is created in the stack

Untitled-2022-05-31-1216.png

2) all the local variables that are belonging to that function are part of that stack frame in this case, the variable x is now part of that stack frame you can see on the image

if we call another function for example

int sum(int n){
sum=n*2;
}
int main(){
int x;
sum(x);
return 0;
}

it will be created on the top of the stack when called

Untitled-2022-05-31-12164.png

3) Once the function call is over the stack frame is cleared off.

for example, once the execution of the sum function is completed the control flow will come to the main function and its stack frame is cleared.

Untitled-2022-05-31-1216.png

4)The currently executing function is always at the top of the stack.

when the sum function is executing it was on the top of the stack and when the main function is running it is on the top if you create a new function and call it , it will be on the top till the execution of the function

that's it !!

KissThankYouGIF.gif Thank you for reading I hope It helps in some way

Did you find this article valuable?

Support Rajeev 🚀 by becoming a sponsor. Any amount is appreciated!