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 Slide: 1 Reading These Slides Slide: 2 Line 19: Call function ’a’ create formal param ’b’ of ’a’ <OS> =⇒ 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. 1 /∗ ∗∗ 2 ∗ A t e s t f i l e f o r t h e s t a c k 3 ∗ v i s u a l i z a t i o n framework 4 ∗ / 5 # i n c l u d e < i o s t r e a m > 6 u s i n g namespace s t d ; 7 8 vo id a ( doub le b ) 9 { 10 doub le c ; 11 c = b + 1 .010010001 ; 12 c o u t << c << e n d l ; 13 r e t u r n ; 14 } 15 16 i n t main ( ) 17 { 18 i n t x ( 3 ) ; 19 a ( x ) ; / / <== 20 c o u t << x << e n d l ; 21 r e t u r n 0 ; 22 } Slide: 5 Line 19: Call function ’a’ create formal param ’b’ of ’a’ <OS> =⇒ main Program Memory ⇓Stack⇓ frame: main var: main::x = 3 frame: a var: a::b = 3 ⇑Heap⇑ To the left is a representation of program memory. At the “top” is the program stack which grows downward. The program heap is located at the other end of memory and grows “upward.” The stack contains one of three types of data: call frames, formal function parameters, and automatic (“local”) variables. Slide: 6 Line 19: Call function ’a’ create formal param ’b’ of ’a’ <OS> =⇒ main Program Memory ⇓Stack⇓ frame: main var: main::x = 3 frame: a var: a::b = 3 ⇑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. Slide: 7 Line 19: Call function ’a’ create formal param ’b’ of ’a’ <OS> =⇒ main Program Memory ⇓Stack⇓ frame: main var: main::x = 3 frame: a var: a::b = 3 ⇑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. Slide: 10 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. Slide: 11 A Simple Example The following is an example of how C++ (and programs in general) keep track of function calls, parameters passed to functions by value, and the local variables of functions. Slide: 12 Line 16: Function main entry <OS> =⇒ main Program Stack ⇓Stack⇓ frame: main ⇑Heap⇑ 1 /∗ ∗∗ 2 ∗ A t e s t f i l e f o r t h e s t a c k 3 ∗ v i s u a l i z a t i o n framework 4 ∗ / 5 # i n c l u d e < i o s t r e a m > 6 u s i n g namespace s t d ; 7 8 vo id a ( doub le b ) 9 { 10 doub le c ; 11 c = b + 1 .010010001 ; 12 c o u t << c << e n d l ; 13 r e t u r n ; 14 } 15 16 i n t main ( ) / / <== 17 { 18 doub le x ( 3 ) ; 19 a ( x ) ; 20 c o u t << x << e n d l ; 21 r e t u r n 0 ; 22 } Slide: 15 Line 18: Declare x <OS> =⇒ main Program Stack ⇓Stack⇓ frame: main var: main::x = 3 ⇑Heap⇑ 1 /∗ ∗∗ 2 ∗ A t e s t f i l e f o r t h e s t a c k 3 ∗ v i s u a l i z a t i o n framework 4 ∗ / 5 # i n c l u d e < i o s t r e a m > 6 u s i n g namespace s t d ; 7 8 vo id a ( doub le b ) 9 { 10 doub le c ; 11 c = b + 1 .010010001 ; 12 c o u t << c << e n d l ; 13 r e t u r n ; 14 } 15 16 i n t main ( ) 17 { 18 doub le x ( 3 ) ; / / <== 19 a ( x ) ; 20 c o u t << x << e n d l ; 21 r e t u r n 0 ; 22 } Slide: 16 Line 19: Call function a <OS> =⇒ main Program Stack ⇓Stack⇓ frame: main var: main::x = 3 ⇑Heap⇑ 1 /∗ ∗∗ 2 ∗ A t e s t f i l e f o r t h e s t a c k 3 ∗ v i s u a l i z a t i o n framework 4 ∗ / 5 # i n c l u d e < i o s t r e a m > 6 u s i n g namespace s t d ; 7 8 vo id a ( doub le b ) 9 { 10 doub le c ; 11 c = b + 1 .010010001 ; 12 c o u t << c << e n d l ; 13 r e t u r n ; 14 } 15 16 i n t main ( ) 17 { 18 doub le x ( 3 ) ; 19 a ( x ) ; / / <== 20 c o u t << x << e n d l ; 21 r e t u r n 0 ; 22 } Slide: 17 Line 8: Function a entry <OS> =⇒ main =⇒ a Program Stack ⇓Stack⇓ frame: main var: main::x = 3 frame: a var: a::b = 3 ⇑Heap⇑ 1 /∗ ∗∗ 2 ∗ A t e s t f i l e f o r t h e s t a c k 3 ∗ v i s u a l i z a t i o n framework 4 ∗ / 5 # i n c l u d e < i o s t r e a m > 6 u s i n g namespace s t d ; 7 8 vo id a ( doub le b ) / / <== 9 { 10 doub le c ; 11 c = b + 1 .010010001 ; 12 c o u t << c << e n d l ; 13 r e t u r n ; 14 } 15 16 i n t main ( ) 17 { 18 doub le x ( 3 ) ; 19 a ( x ) ; 20 c o u t << x << e n d l ; 21 r e t u r n 0 ; 22 } Slide: 20 Line 10: Declare c <OS> =⇒ main =⇒ a Program Stack ⇓Stack⇓ frame: main var: main::x = 3 frame: a var: a::b = 3 var: a::c = GARBAGE ⇑Heap⇑ 1 /∗ ∗∗ 2 ∗ A t e s t f i l e f o r t h e s t a c k 3 ∗ v i s u a l i z a t i o n framework 4 ∗ / 5 # i n c l u d e < i o s t r e a m > 6 u s i n g namespace s t d ; 7 8 vo id a ( doub le b ) 9 { 10 doub le c ; / / <== 11 c = b + 1 .010010001 ; 12 c o u t << c << e n d l ; 13 r e t u r n ; 14 } 15 16 i n t main ( ) 17 { 18 doub le x ( 3 ) ; 19 a ( x ) ; 20 c o u t << x << e n d l ; 21 r e t u r n 0 ; 22 } Slide: 21 Line 11: Calculate c <OS> =⇒ main =⇒ a Program Stack ⇓Stack⇓ frame: main var: main::x = 3 frame: a var: a::b = 3 var: a::c = 4.01001 ⇑Heap⇑ 1 /∗ ∗∗ 2 ∗ A t e s t f i l e f o r t h e s t a c k 3 ∗ v i s u a l i z a t i o n framework 4 ∗ / 5 # i n c l u d e < i o s t r e a m > 6 u s i n g namespace s t d ; 7 8 vo id a ( doub le b ) 9 { 10 doub le c ; 11 c = b + 1 .010010001 ; / / <== 12 c o u t << c << e n d l ; 13 r e t u r n ; 14 } 15 16 i n t main ( ) 17 { 18 doub le x ( 3 ) ; 19 a ( x ) ; 20 c o u t << x << e n d l ; 21 r e t u r n 0 ; 22 } Slide: 22 Line 13: Return to main discard the auto (local) variable a::c <OS> =⇒ main =⇒ a Program Stack ⇓Stack⇓ frame: main var: main::x = 3 frame: a var: a::b = 3 ⇑Heap⇑ 1 /∗ ∗∗ 2 ∗ A t e s t f i l e f o r t h e s t a c k 3 ∗ v i s u a l i z a t i o n framework 4 ∗ / 5 # i n c l u d e < i o s t r e a m > 6 u s i n g namespace s t d ; 7 8 vo id a ( doub le b ) 9 { 10 doub le c ; 11 c = b + 1 .010010001 ; 12 c o u t << c << e n d l ; 13 r e t u r n ; / / <== 14 } 15 16 i n t main ( ) 17 { 18 doub le x ( 3 ) ; 19 a ( x ) ; 20 c o u t << x << e n d l ; 21 r e t u r n 0 ; 22 } Slide: 25 Line 13: Return to main discard a parameter b <OS> =⇒ main =⇒ a Program Stack ⇓Stack⇓ frame: main var: main::x = 3 frame: a ⇑Heap⇑ 1 /∗ ∗∗ 2 ∗ A t e s t f i l e f o r t h e s t a c k 3 ∗ v i s u a l i z a t i o n framework 4 ∗ / 5 # i n c l u d e < i o s t r e a m > 6 u s i n g namespace s t d ; 7 8 vo id a ( doub le b ) 9 { 10 doub le c ; 11 c = b + 1 .010010001 ; 12 c o u t << c << e n d l ; 13 r e t u r n ; / / <== 14 } 15 16 i n t main ( ) 17 { 18 doub le x ( 3 ) ; 19 a ( x ) ; 20 c o u t << x << e n d l ; 21 r e t u r n 0 ; 22 } Slide: 26 Line 13: Return to main discard call frame, return to caller <OS> =⇒ main =⇒ a Program Stack ⇓Stack⇓ frame: main var: main::x = 3 ⇑Heap⇑ 1 /∗ ∗∗ 2 ∗ A t e s t f i l e f o r t h e s t a c k 3 ∗ v i s u a l i z a t i o n framework 4 ∗ / 5 # i n c l u d e < i o s t r e a m > 6 u s i n g namespace s t d ; 7 8 vo id a ( doub le b ) 9 { 10 doub le c ; 11 c = b + 1 .010010001 ; 12 c o u t << c << e n d l ; 13 r e t u r n ; / / <== 14 } 15 16 i n t main ( ) 17 { 18 doub le x ( 3 ) ; 19 a ( x ) ; 20 c o u t << x << e n d l ; 21 r e t u r n 0 ; 22 } Slide: 27 Line 21: Return to OS <OS> =⇒ main Program Stack ⇓Stack⇓ frame: main var: main::x = 3 ⇑Heap⇑ 1 /∗ ∗∗ 2 ∗ A t e s t f i l e f o r t h e s t a c k 3 ∗ v i s u a l i z a t i o n framework 4 ∗ / 5 # i n c l u d e < i o s t r e a m > 6 u s i n g namespace s t d ; 7 8 vo id a ( doub le b ) 9 { 10 doub le c ; 11 c = b + 1 .010010001 ; 12 c o u t << c << e n d l ; 13 r e t u r n ; 14 } 15 16 i n t main ( ) 17 { 18 doub le x ( 3 ) ; 19 a ( x ) ; 20 c o u t << x << e n d l ; 21 r e t u r n 0 ; / / <== 22 } Slide: 30 Line 21: Return to OS discard the auto (local) variable main::x <OS> =⇒ main Program Stack ⇓Stack⇓ frame: main ⇑Heap⇑ 1 /∗ ∗∗ 2 ∗ A t e s t f i l e f o r t h e s t a c k 3 ∗ v i s u a l i z a t i o n framework 4 ∗ / 5 # i n c l u d e < i o s t r e a m > 6 u s i n g namespace s t d ; 7 8 vo id a ( doub le b ) 9 { 10 doub le c ; 11 c = b + 1 .010010001 ; 12 c o u t << c << e n d l ; 13 r e t u r n ; 14 } 15 16 i n t main ( ) 17 { 18 doub le x ( 3 ) ; 19 a ( x ) ; 20 c o u t << x << e n d l ; 21 r e t u r n 0 ; / / <== 22 } Slide: 31 Line 21: Return to OS discard call frame, return to caller <OS> =⇒ main Program Stack ⇓Stack⇓ ⇑Heap⇑ 1 /∗ ∗∗ 2 ∗ A t e s t f i l e f o r t h e s t a c k 3 ∗ v i s u a l i z a t i o n framework 4 ∗ / 5 # i n c l u d e < i o s t r e a m > 6 u s i n g namespace s t d ; 7 8 vo id a ( doub le b ) 9 { 10 doub le c ; 11 c = b + 1 .010010001 ; 12 c o u t << c << e n d l ; 13 r e t u r n ; 14 } 15 16 i n t main ( ) 17 { 18 doub le x ( 3 ) ; 19 a ( x ) ; 20 c o u t << x << e n d l ; 21 r e t u r n 0 ; / / <== 22 } Slide: 32