1 / 28

Call Stacks

Call Stacks. John Keyser. 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

derick
Download Presentation

Call Stacks

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  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 • Pop • Pop 5 5

  4. Example: • Push 5 • Push 10 • Pop • Push 8 • Push 1 • Pop • Pop • Pop 10 10 5

  5. Example: • Push 5 • Push 10 • Pop • Push 8 • Push 1 • Pop • Pop • Pop 10 5

  6. Example: • Push 5 • Push 10 • Pop • Push 8 • Push 1 • Pop • Pop • Pop 8 8 5

  7. Example: • Push 5 • Push 10 • Pop • Push 8 • Push 1 • Pop • Pop • Pop 1 1 8 5

  8. Example: • Push 5 • Push 10 • Pop • Push 8 • Push 1 • Pop • Pop • Pop 1 10 8 5

  9. Example: • Push 5 • Push 10 • Pop • Push 8 • Push 1 • Pop • Pop • Pop 8 5

  10. Example: • Push 5 • Push 10 • Pop • Push 8 • Push 1 • Pop • Pop • 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 Stroustrup/Programming -- Oct'10

  12. Example7 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. Example7 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. Example7 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 B A

  15. Example7 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 D B A

  16. Example7 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 B A

  17. Example7 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 E B A

  18. Example7 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 G E B A

  19. Example7 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 E B A

  20. Example7 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 B A

  21. Example7 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. Example7 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 C A

  23. Example7 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 D C A

  24. Example7 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 C A

  25. Example7 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 F C A

  26. Example7 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 C A

  27. Example7 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. Example7 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

More Related