Download presentation
Presentation is loading. Please wait.
Published byEdwin Shepherd Modified over 9 years ago
1
Call Stacks John Keyser
2
Stacks A very basic data structure. – Data structure: a container for holding data in a program. – Classes, structs can be thought of as data structures – The vector is a data structure we have already used. Like forming a stack of objects Two basic operations – Push (add something to top of stack) – Pop (remove whatever is on top of stack
3
Example: Push 5 Push 10 Pop Push 8 Push 1 Pop 5 5
4
Example: Push 5 Push 10 Pop Push 8 Push 1 Pop 5 10
5
Example: Push 5 Push 10 Pop Push 8 Push 1 Pop 5 10
6
Example: Push 5 Push 10 Pop Push 8 Push 1 Pop 5 8 8
7
Example: Push 5 Push 10 Pop Push 8 Push 1 Pop 5 8 1 1
8
Example: Push 5 Push 10 Pop Push 8 Push 1 Pop 5 108 1
9
Example: Push 5 Push 10 Pop Push 8 Push 1 Pop 5 8
10
Example: Push 5 Push 10 Pop Push 8 Push 1 Pop 5
11
Functions (aka routines, methods, etc.) When a function is called, it is pushed onto a call stack. – What is pushed on is a “function activation record” – This record contains the information/variables the function uses (effectively the local scope When a function finishes, it is popped off of the call stack. – It might return a value to the prior item in the stack. The call stack can get very deep – many functions are called from one another 11Stroustrup/Programming -- Oct'10
12
Example 7 different functions (A through F) int A() {… B(); C(); …} int B() {… D(); E(); …} int C() {… D(); F(); …} int D() {…} int E() {… G(); …} int F() {…} int G() {…} main() {… A(); …}
13
Example 7 different functions (A through F) int A() {… B(); C(); …} int B() {… D(); E(); …} int C() {… D(); F(); …} int D() {…} int E() {… G(); …} int F() {…} int G() {…} main() {… A(); …} Call Stack A
14
Example 7 different functions (A through F) int A() {… B(); C(); …} int B() {… D(); E(); …} int C() {… D(); F(); …} int D() {…} int E() {… G(); …} int F() {…} int G() {…} main() {… A(); …} Call Stack A B
15
Example 7 different functions (A through F) int A() {… B(); C(); …} int B() {… D(); E(); …} int C() {… D(); F(); …} int D() {…} int E() {… G(); …} int F() {…} int G() {…} main() {… A(); …} Call Stack A B D
16
Example 7 different functions (A through F) int A() {… B(); C(); …} int B() {… D(); E(); …} int C() {… D(); F(); …} int D() {…} int E() {… G(); …} int F() {…} int G() {…} main() {… A(); …} Call Stack A B
17
Example 7 different functions (A through F) int A() {… B(); C(); …} int B() {… D(); E(); …} int C() {… D(); F(); …} int D() {…} int E() {… G(); …} int F() {…} int G() {…} main() {… A(); …} Call Stack A B E
18
Example 7 different functions (A through F) int A() {… B(); C(); …} int B() {… D(); E(); …} int C() {… D(); F(); …} int D() {…} int E() {… G(); …} int F() {…} int G() {…} main() {… A(); …} Call Stack A B E G
19
Example 7 different functions (A through F) int A() {… B(); C(); …} int B() {… D(); E(); …} int C() {… D(); F(); …} int D() {…} int E() {… G(); …} int F() {…} int G() {…} main() {… A(); …} Call Stack A B E
20
Example 7 different functions (A through F) int A() {… B(); C(); …} int B() {… D(); E(); …} int C() {… D(); F(); …} int D() {…} int E() {… G(); …} int F() {…} int G() {…} main() {… A(); …} Call Stack A B
21
Example 7 different functions (A through F) int A() {… B(); C(); …} int B() {… D(); E(); …} int C() {… D(); F(); …} int D() {…} int E() {… G(); …} int F() {…} int G() {…} main() {… A(); …} Call Stack A
22
Example 7 different functions (A through F) int A() {… B(); C(); …} int B() {… D(); E(); …} int C() {… D(); F(); …} int D() {…} int E() {… G(); …} int F() {…} int G() {…} main() {… A(); …} Call Stack A C
23
Example 7 different functions (A through F) int A() {… B(); C(); …} int B() {… D(); E(); …} int C() {… D(); F(); …} int D() {…} int E() {… G(); …} int F() {…} int G() {…} main() {… A(); …} Call Stack A C D
24
Example 7 different functions (A through F) int A() {… B(); C(); …} int B() {… D(); E(); …} int C() {… D(); F(); …} int D() {…} int E() {… G(); …} int F() {…} int G() {…} main() {… A(); …} Call Stack A C
25
Example 7 different functions (A through F) int A() {… B(); C(); …} int B() {… D(); E(); …} int C() {… D(); F(); …} int D() {…} int E() {… G(); …} int F() {…} int G() {…} main() {… A(); …} Call Stack A C F
26
Example 7 different functions (A through F) int A() {… B(); C(); …} int B() {… D(); E(); …} int C() {… D(); F(); …} int D() {…} int E() {… G(); …} int F() {…} int G() {…} main() {… A(); …} Call Stack A C
27
Example 7 different functions (A through F) int A() {… B(); C(); …} int B() {… D(); E(); …} int C() {… D(); F(); …} int D() {…} int E() {… G(); …} int F() {…} int G() {…} main() {… A(); …} Call Stack A
28
Example 7 different functions (A through F) int A() {… B(); C(); …} int B() {… D(); E(); …} int C() {… D(); F(); …} int D() {…} int E() {… G(); …} int F() {…} int G() {…} main() {… A(); …} Call Stack
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.