Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lifetime “The lifetime of a variable is the time during which the variable is bound to a specific memory location.” [p. 219] “…the lifetime of a variable.

Similar presentations


Presentation on theme: "Lifetime “The lifetime of a variable is the time during which the variable is bound to a specific memory location.” [p. 219] “…the lifetime of a variable."— Presentation transcript:

1 Lifetime “The lifetime of a variable is the time during which the variable is bound to a specific memory location.” [p. 219] “…the lifetime of a variable begins when it is bound to a specific cell and ends when it is unbound from that cell.” [p. 219] Four categories –static –stack-dynamic –explicit heap-dynamic –implicit heap-dynamic

2 Memory organization AVAILABLE MEMORY STATIC code & static data HEAP dynamic data STACK local data: invocation records

3 static variables Bound to a memory location prior to execution. No run-time allocation needs to occur: efficient. Persistent: variable persists throughout execution of a program.

4 Example (C) #include “stdio.h”; int counter() { static int k = 0; return ++k; } int main() { printf(“Counter is %d \n”,counter()); return 0; } /* OUTPUT IS: Counter is 1 Counter is 2 */

5 Notes about example static variable k is allocated space in the static segment allocation happens once k’s lifetime is the that of the entire program k’s value persists from call to call

6 stack dynamic variables “storage bindings are created when their declaration statements are elaborated, but whose types are statically bound” [p. 220] Allocated on the run-time stack

7 Example (C) #include “stdio.h”; int counter() { int k = 0; return ++k; } int main() { printf(“Counter is %d \n”,counter()); return 0; } /* OUTPUT IS: Counter is 1 */

8 Notes about example stack-dynamic variable k is allocated space in the stack segment allocation happens each time function is called k’s lifetime is the that of the function invocation k’s value does not persist from call to call: it is reinitialized on each function execution

9 explicit heap-dynamic variables anonymous (nameless) variables created at runtime, allocated on the heap, and accessible only via indirection (a pointer) Example [p. 221] int *intnode; // Create a pointer... intnode = new int; // Create the heap-dynamic variable... delete intnode; // Deallocate the heap-dynamic variable // to which intnode points

10 Example (Java) public class Count private int k; public Count() { k = 0; } public int counter() { return ++k; } public static void main(String [] args) { Count c = new Count(); System.out.println(“Counter is ”+c.counter()); } /* OUTPUT IS: Counter is 1 Counter is 2 */

11 Notes about example instance variable k is allocated space in the heap segment allocation happens each time object is created is called k’s lifetime is the that of its object k’s value persists from call to call of the method many different independent k’s can co-exist

12 implicit heap-dynamic variables automatic heap-allocation JavaScript, [pg. 214]: list = [10.2, 3.5]; list = 47; Scheme –all allocation is done on heap –cons allocates a pair –environments are allocated on heap (so no runtime stack is needed for function calls).

13 Example (Scheme) (define count1 (lambda () (let ((k 0)) (set! k (+ k 1)) k ))) (define count2 (let ((k 0)) (lambda () (set! k (+ k 1)) k ))) (display (count1)) (newline) (display (count2)) (newline) /* OUTPUT IS: 1 2 */

14 Notes about example in count1 k is initialized each time function is called in count2 k is initialized when the function is defined

15 Scope “The scope of a variable is the range of statements in which the variable is visible. A variable is visible in a statement if it can be referenced in that statement.” Type basic approaches: –static scoping –dynamic scoping


Download ppt "Lifetime “The lifetime of a variable is the time during which the variable is bound to a specific memory location.” [p. 219] “…the lifetime of a variable."

Similar presentations


Ads by Google