More Recursion
Objectives
We would like to :
- Continue to learn about recursion.
Notes
- We discussed the recursive exponential program and implemented it.
- We discuss the layout of a program in memory
-
Address | Use |
High Memory | Stack |
| Heap |
| Uninitialized data |
| Initialized data |
Low Memory | Text (code) |
- The stack is where functions store information
- The heap is where dynamic memory is stored
- The data segments are where global variables are stored.
- The text segment is where the code is stored.
- There may be other memory used by the OS
- But that is not important now.
- More in operating systems
- Each time a function (including main) is called
- An activation record or stack frame is pushed onto the stack.
- This contains
- The local vriables
- The return address for the function
- Other information.
- More in architecture/operating systems/assembly
- When a function exits, it removes the activation record from the stack
- After it restores any information stored in that recored.
- This explains
- Lifetime and scope of a variable.
- How recursive calls each have their own copy of a variable.
- We looked at the Fibonacci sequence
- Or $fib(n) = \begin{cases}fib(n-1)+fib(n-2), & n > 2\\ 1, & n =1,2\end{cases}$
- And created the program fib.cpp