The call stack is a special area in memory that stores information about active function calls, including local variables, return addresses, and parameters. It helps the program keep track of where to return after a function finishes.
Click to reveal answer
beginner
What happens to the call stack when a function is called?
When a function is called, a new stack frame is pushed onto the call stack. This frame contains the function's parameters, local variables, and the return address to continue after the function finishes.
Click to reveal answer
beginner
What is a stack frame?
A stack frame is a block of memory on the call stack that holds data for one function call, including its parameters, local variables, and return address.
Click to reveal answer
beginner
What happens when a function returns in terms of the call stack?
When a function returns, its stack frame is popped off the call stack, and the program continues execution from the return address stored in that frame.
Click to reveal answer
intermediate
What is a stack overflow and when can it happen?
A stack overflow happens when the call stack grows beyond its limit, usually because of too many nested function calls or infinite recursion, causing the program to crash.
Click to reveal answer
What does the call stack store during program execution?
AInformation about active function calls
BGlobal variables only
COnly the program's source code
DOutput data for the user
✗ Incorrect
The call stack stores information about active function calls, including local variables, parameters, and return addresses.
What happens to the call stack when a function finishes executing?
AA new stack frame is pushed
BThe entire stack is cleared
CThe current stack frame is popped
DNothing changes
✗ Incorrect
When a function finishes, its stack frame is popped off the call stack to return control to the caller.
Which of the following can cause a stack overflow?
AToo many nested function calls
BUsing global variables
CDeclaring large arrays globally
DPrinting output to the console
✗ Incorrect
Too many nested function calls or infinite recursion can cause the call stack to exceed its limit, causing a stack overflow.
What is stored in a stack frame?
AOnly the function's name
BFunction parameters, local variables, and return address
CThe entire program's code
DUser input data
✗ Incorrect
A stack frame stores the function's parameters, local variables, and the return address.
When a function calls another function, what happens to the call stack?
AThe current stack frame is removed
BThe program pauses
CThe stack is reset
DA new stack frame is added on top
✗ Incorrect
Calling a function adds a new stack frame on top of the call stack.
Explain how the call stack manages function calls and returns in C.
Think about what happens when a function starts and ends.
You got /4 concepts.
Describe what causes a stack overflow and how it relates to the call stack.
Consider what happens if the call stack grows too big.
You got /4 concepts.
Practice
(1/5)
1. What does the call stack do in a C program?
easy
A. Handles input and output operations
B. Stores global variables permanently
C. Manages memory allocation for dynamic data
D. Keeps track of function calls and local variables
Solution
Step 1: Understand the role of the call stack
The call stack stores information about active function calls and their local variables during program execution.
Step 2: Differentiate from other memory areas
Global variables and dynamic memory are stored elsewhere, not on the call stack.
Final Answer:
Keeps track of function calls and local variables -> Option D
Quick Check:
Call stack = function calls + locals [OK]
Hint: Call stack tracks active functions and locals [OK]
Common Mistakes:
Confusing call stack with heap memory
Thinking global variables are on the stack
Assuming call stack manages I/O
2. Which of the following is the correct way to declare a function in C that will be called and added to the call stack?
easy
A. void myFunction() { /* code */ }
B. function myFunction() { /* code */ }
C. def myFunction() { /* code */ }
D. func myFunction() { /* code */ }
Solution
Step 1: Identify correct C function syntax
In C, functions are declared with a return type, name, and parentheses, e.g., void myFunction().
Step 2: Recognize incorrect syntax
Keywords like function, def, or func are not valid in C.
Final Answer:
void myFunction() { /* code */ } -> Option A
Quick Check:
C function declaration uses return type + name [OK]
Hint: C functions start with return type, no keywords like def [OK]