Docsity

A Simple Stack Example - Programming Concepts Java | CSCI 261, Study notes of Computer Science

Partial preview of the text

Download A Simple Stack Example - Programming Concepts Java | CSCI 261 and more Study notes Computer Science in PDF only on Docsity!

A Simple Stack Example

July 20, 2009

Reading These Slides

Line 19: Call function ’a’

create formal param ’b’ of ’a’

=⇒ main

Below the descriptive text is a call graph of the application, it begins with just OS, which is the operating system (Windows, Linux, ...). The operating system calls main(), and main() will then call other functions. The call graph shows the nesting of function calls that matches the application stack.

Line 19: Call function ’a’

create formal param ’b’ of ’a’

=⇒ main

To the right, <== marks the the C++ statement that is currently executing. Some C++ statements perform multiple CPU instructions, so the marker doesn’t necessarily move with every stack change.

12 / ∗ ∗∗∗ A t e s t f i l e f o r t h e s t a c k 34 ∗∗ (^) /v i s u a l i z a t i o n f ra mewo rk 56 # i n c l u d eu s i n g n am esp ace < i o s t r e a m > s t d ; (^78) v o i d a ( d o u b l e b ) 10 9 { d o u b l e c ; 1112 cc o u t = b (^) <<+ 1. 0 1 0 0 1 0 0 0 1 ;c << e n d l ; (^13) 14 } r e t u r n ; (^1516) i n t main ( ) 17 { 18 i n t x ( 3 ) ; 1920 ac o u t ( x ) ; << x << e n d l ; / / <== (^21) 22 } r e t u r n 0 ;

Line 19: Call function ’a’

create formal param ’b’ of ’a’

=⇒ main

Program Memory ⇓Stack⇓

var: main::x = 3^ frame: main var: a::b = 3^ frame: a

⇑Heap⇑

Call Frames contain “bookkeeping”

information for a function call. Such

as: where return values should be

placed in memory, and which C++

statement in the application should

be returned to when a function

finishes.

Every function call has a call frame.

Line 19: Call function ’a’

create formal param ’b’ of ’a’

=⇒ main

Program Memory ⇓Stack⇓

var: main::x = 3^ frame: main var: a::b = 3^ frame: a

⇑Heap⇑

Formal Parameters are passed by

value or by reference (indicated with

a →) to a function. You will see that

the caller sets these variables up in

memory, and the callee (the function

using the parameters) “discards”

them from the stack.

Line 19: Call function ’a’

create formal param ’b’ of ’a’

=⇒ main

Program Memory ⇓Stack⇓

var: main::x = 3^ frame: main var: a::b = 3^ frame: a

⇑Heap⇑

:: is the C++ scoping operator.

So main::x represents the main

function’s automatic variable x; and

a::b represents a’s double b

parameter.

Line 19: Call function ’a’

create formal param ’b’ of ’a’

Critical to understanding these slides is knowing that the call graph, code listing (with <== line indicator), and the program memory diagram (the stack and the heap), all represent the program state AFTER the program step described in the title has taken place.

That’s a long sentence, read it again. Be sure you understand it.

It means that when elements are CREATED in memory the slides are easy to interpret (because the newly created element in memory can be seen on the slide!).

But, when elements are DESTROYED in memory, you may need to compare the program memory diagram with the figure one slide back , to really see the effect.

OS calls main()

Program Stack ⇓Stack⇓

⇑Heap⇑

12 / ∗ ∗∗∗ A t e s t f i l e f o r t h e s t a c k 34 ∗∗ (^) /v i s u a l i z a t i o n f ra mewo rk 56 # i n c l u d eu s i n g n am esp ace < i o s t r e a m > s t d ; (^78) v o i d a ( d o u b l e b ) 10 9 { d o u b l e c ; 1112 cc o u t = b (^) <<+ 1. 0 1 0 0 1 0 0 0 1 ;c << e n d l ; (^13) 14 } r e t u r n ; (^1516) i n t main ( ) 17 { 18 d o u b l e x ( 3 ) ; 1920 ac o u t ( x ) ; << x << e n d l ; (^21) 22 } r e t u r n 0 ;

OS calls main()

create call frame for main

Program Stack ⇓Stack⇓

frame: main

⇑Heap⇑

12 / ∗ ∗∗∗ A t e s t f i l e f o r t h e s t a c k 34 ∗∗ (^) /v i s u a l i z a t i o n f ra mewo rk 56 # i n c l u d eu s i n g n am esp ace < i o s t r e a m > s t d ; (^78) v o i d a ( d o u b l e b ) 10 9 { d o u b l e c ; 1112 cc o u t = b (^) <<+ 1. 0 1 0 0 1 0 0 0 1 ;c << e n d l ; (^13) 14 } r e t u r n ; (^1516) i n t main ( ) 17 { 18 d o u b l e x ( 3 ) ; 1920 ac o u t ( x ) ; << x << e n d l ; (^21) 22 } r e t u r n 0 ;

Line 18: Declare x

=⇒ main

Program Stack ⇓Stack⇓

var: main::x = 3^ frame: main

⇑Heap⇑

12 / ∗ ∗∗∗ A t e s t f i l e f o r t h e s t a c k 34 ∗∗ (^) /v i s u a l i z a t i o n f ra mewo rk 56 # i n c l u d eu s i n g n am esp ace < i o s t r e a m > s t d ; (^78) v o i d a ( d o u b l e b ) 10 9 { d o u b l e c ; 1112 cc o u t = b (^) <<+ 1. 0 1 0 0 1 0 0 0 1 ;c << e n d l ; (^13) 14 } r e t u r n ; (^1516) i n t main ( ) 17 { 18 d o u b l e x ( 3 ) ; / / <== 1920 ac o u t ( x ) ; << x << e n d l ; (^21) 22 } r e t u r n 0 ;

Line 19: Call function a

=⇒ main

Program Stack ⇓Stack⇓

var: main::x = 3^ frame: main

⇑Heap⇑

12 / ∗ ∗∗∗ A t e s t f i l e f o r t h e s t a c k 34 ∗∗ (^) /v i s u a l i z a t i o n f ra mewo rk 56 # i n c l u d eu s i n g n am esp ace < i o s t r e a m > s t d ; (^78) v o i d a ( d o u b l e b ) 10 9 { d o u b l e c ; 1112 cc o u t = b (^) <<+ 1. 0 1 0 0 1 0 0 0 1 ;c << e n d l ; (^13) 14 } r e t u r n ; (^1516) i n t main ( ) 17 { 18 d o u b l e x ( 3 ) ; 1920 ac o u t ( x ) ; << x << e n d l ; / / <== (^21) 22 } r e t u r n 0 ;

Line 19: Call function a

create formal param b of a

=⇒ main

Program Stack ⇓Stack⇓

var: main::x = 3^ frame: main var: a::b = 3^ frame: a

⇑Heap⇑

12 / ∗ ∗∗∗ A t e s t f i l e f o r t h e s t a c k 34 ∗∗ (^) /v i s u a l i z a t i o n f ra mewo rk 56 # i n c l u d eu s i n g n am esp ace < i o s t r e a m > s t d ; (^78) v o i d a ( d o u b l e b ) 10 9 { d o u b l e c ; 1112 cc o u t = b (^) <<+ 1. 0 1 0 0 1 0 0 0 1 ;c << e n d l ; (^13) 14 } r e t u r n ; (^1516) i n t main ( ) 17 { 18 d o u b l e x ( 3 ) ; 1920 ac o u t ( x ) ; << x << e n d l ; / / <== (^21) 22 } r e t u r n 0 ;

Line 8: Function a entry

=⇒ main =⇒ a

Program Stack ⇓Stack⇓

var: main::x = 3^ frame: main frame: a var: a::b = 3

⇑Heap⇑

12 / ∗ ∗∗∗ A t e s t f i l e f o r t h e s t a c k 34 ∗∗ (^) /v i s u a l i z a t i o n f ra mewo rk 56 # i n c l u d eu s i n g n am esp ace < i o s t r e a m > s t d ; (^78) v o i d a ( d o u b l e b ) / / <== 10 9 { d o u b l e c ; 1112 cc o u t = b (^) <<+ 1. 0 1 0 0 1 0 0 0 1 ;c << e n d l ; (^13) 14 } r e t u r n ; (^1516) i n t main ( ) 17 { 18 d o u b l e x ( 3 ) ; 1920 ac o u t ( x ) ; << x << e n d l ; (^21) 22 } r e t u r n 0 ;