call stacks

28
Call Stacks John Keyser

Upload: derick

Post on 22-Feb-2016

68 views

Category:

Documents


0 download

DESCRIPTION

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 - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Call Stacks

Call Stacks

John Keyser

Page 2: Call Stacks

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

Page 3: Call Stacks

Example:

• Push 5• Push 10• Pop• Push 8• Push 1• Pop• Pop• Pop 5

5

Page 4: Call Stacks

Example:

• Push 5• Push 10• Pop• Push 8• Push 1• Pop• Pop• Pop 5

10

10

Page 5: Call Stacks

Example:

• Push 5• Push 10• Pop• Push 8• Push 1• Pop• Pop• Pop 5

10

Page 6: Call Stacks

Example:

• Push 5• Push 10• Pop• Push 8• Push 1• Pop• Pop• Pop 5

8

8

Page 7: Call Stacks

Example:

• Push 5• Push 10• Pop• Push 8• Push 1• Pop• Pop• Pop 5

81

1

Page 8: Call Stacks

Example:

• Push 5• Push 10• Pop• Push 8• Push 1• Pop• Pop• Pop 5

108

1

Page 9: Call Stacks

Example:

• Push 5• Push 10• Pop• Push 8• Push 1• Pop• Pop• Pop 5

8

Page 10: Call Stacks

Example:

• Push 5• Push 10• Pop• Push 8• Push 1• Pop• Pop• Pop

5

Page 11: Call Stacks

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

Page 12: Call Stacks

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(); …}

Page 13: Call Stacks

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

Page 14: Call Stacks

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

AB

Page 15: Call Stacks

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

ABD

Page 16: Call Stacks

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

AB

Page 17: Call Stacks

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

ABE

Page 18: Call Stacks

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

ABEG

Page 19: Call Stacks

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

ABE

Page 20: Call Stacks

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

AB

Page 21: Call Stacks

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

Page 22: Call Stacks

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

AC

Page 23: Call Stacks

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

ACD

Page 24: Call Stacks

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

AC

Page 25: Call Stacks

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

ACF

Page 26: Call Stacks

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

AC

Page 27: Call Stacks

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

Page 28: Call Stacks

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