0% found this document useful (0 votes)
2 views

Lecture 7

The document discusses memory layout in C, focusing on variable types such as local, global, and static, as well as their scope and lifetime. It explains recursion, providing examples and code implementations for factorial, exponentiation, Fibonacci numbers, and other recursive functions. The document emphasizes the importance of understanding stack frames and the recursive process in programming.

Uploaded by

haleemajamil170
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Lecture 7

The document discusses memory layout in C, focusing on variable types such as local, global, and static, as well as their scope and lifetime. It explains recursion, providing examples and code implementations for factorial, exponentiation, Fibonacci numbers, and other recursive functions. The document emphasizes the importance of understanding stack frames and the recursive process in programming.

Uploaded by

haleemajamil170
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 22

Memory layout and

Recursion
Muhammad Saqib Ilyas
Learning goals
• Describe the various memory segments and their
purpose
• Describe recursion in simple but correct terms
• Implement a recursive solution to a simple problem in C
• Visualize how a recursive program runs
Types of variables in C
• What types of variables (not data types) can you think
of?
• Local
• Global
• Static
Spot the local and global variables
int queue_size = 10; int main() {
void foo(int num) { queue_size++;
printf("%d\n", num); foo(3);
return 0;
int x = 0; }
{
int x = 1; What do you expect the output to
printf("%d\n", x); be?
}
printf("%d\n", x);
queue_size--;
}
Static variables
void bar() {
static int count = 0;
printf("%d\n", count);
count++;
} What do you expect the output to
int main() { be?
bar();
bar();
return 0;
}
Scope of a variable
• Portion of the program where the variable is accessible
• Local variables:
• Within the function or block where defined
• Global variables:
• Throughout the program
Lifetime of a variable
• When is a variable created?
• When is it destroyed?
• Local:
• Created: Each time the function is called
• Destroyed: When the function returns
• Local static / Global:
• Created: When the program starts
• Initialized: Once
• Destroyed: When the program ends
How does a computer handle all
this?
x86 memory layout
Local variables Stack n-1

Dynamic variables Heap

Uninitialized Block Started by


Global / static
variables Symbol (BSS)
variables
Initialized
Data
variables

Program
Text 0
instructions

Address
Recursion
• What is recursion?
• The smart-Alec way of answering a question
• Example: n! = n(n-1)!
Example dialog
You haven’t
answered the
What is
What is 2! = 2 x 1question It is 23x
4
2! = 2 x 1!
1!?
4!?
3!?
2!? =2 It 1!
is 1
2!
3!
3! = 3 x 2
3! = 3 x 2!
=6
4! = 4 x 6
4! = 4 x 3!
= 24
Recursion in code
• Recursive step: n! = n x (n – 1)!
• Base case: 1! = 1

unsigned long factorial(unsigned int n) {


if (n == 1)
return 1; What happens if we call factorial() with n set to 3?
return n * factorial(n – 1);
}

n = 3;
printf(“%ld\n”, factorial(n));
Recursion in C
unsigned long factorial(unsigned
int n) {
if (n == 1) n=1
return 1;
unsigned longreturnfactorial(unsigned
n Recursive
* factorial(n – 1);
int n) { } 2 x 1 = call
if (n == 1) 2 n=2

return 1;
unsigned longn factorial(unsigned
return *Recursive
factorial(n – 1);
int n)}{ 3 x 2 = call
if (n == 1) 6 n=3

return 1;
return n * factorial(n – 1);
}
Recursion Implementation
• Each function call creates its own stack frame
• Has independent copies of local variables
• Recursive calls keep “stacking” these stack frames

fact(1)1
return
In this phase, we say
that “the stack is 2return
* fact(1)
2
unwinding”
winding”
3return
* fact(2)
6
Exercise – Exponentiation
Given positive integers a and n, write a Python program to
calculate an

For a hint, the factorial program is given below


unsigned long int factorial(unsigned int n)
{
if (n == 1) {
return 1;
}
return n * factorial(n - 1);
}
Sample solution

unsigned long pow(int a, int n) {


if (n == 0)
return 1;
return a * pow(a, n – 1);
}
Fibonacci numbers
• Write a recursive function in C that finds the n-th
Fibonacci number, given:
• Fib[0] = 0
• Fib[1] = 1
• Fib[n] = Fib[n-1] + Fib[n-2]

https://bit.ly/
3XP7oCy
Product of two positive integers
• Given two positive integers a, and b, write a recursive C
function to calculate a x b.
Sum of digits
• Given a positive integer n, write a recursive C function
to find the sum of its digits.
• Example:
• If the input number is 123, then the answer should be 1 + 2 +
3=6
Binary search
• Given a sorted array, write a recursive function that
returns:
• The index of a target value in the array,
• -1 if that value is not found
Palindrome
• A palindrome is a string that reads the same in both
directions
• Examples:
• Dad
• DAAD
• Write a recursive function that returns:
• 1 if a string is a palindrome
• 0 otherwise
Maximum value in an array
• Given an array of integers, write a recursive function
that returns the maximum value in that array
Recap
• Local variables are allocated on the stack
• Dynamic variables are allocated on the heap
• Static variables have program lifetime and aren’t re-
initialized
• Recursion
• Represent a problem in terms of smaller versions of itself
• Divide and conquer:
• Keep breaking down a problem into smaller ones until it becomes trivial
• Systematically combine the results of smaller problems
• Recursive implementations are deceptively simple

You might also like