1 / 12

explicit heap-dynamic variables

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

yair
Télécharger la présentation

explicit heap-dynamic variables

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

  2. 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()); System.out.println(“Counter is ”+c.counter()); } } /* OUTPUT IS: Counter is 1 Counter is 2 */

  3. Notes about example • instance variable k is allocated space in the heap segment • allocation happens each time object is created • 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

  4. implicit heap-dynamic variables • automatic heap-allocation (no explicit allocation as with ‘new’) • 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).

  5. 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 (count1)) (newline) (newline) (display (count2)) (newline) (display (count2)) (newline) /* OUTPUT IS: 1 1 1 2 */

  6. Environment diagrams (drawn on board)

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

  8. Exercise • How do you define a Scheme function which can dynamically generate counter functions, each with its own independent counter?

  9. counterMaker (define counterMaker (lambda () (let ((k 0)) (lambda () (set! k (+ k 1)) k )))) (define c1 (counterMaker)) (define c2 (counterMaker)) (c1) (c1) (c1) (c2) (c2) (c1) (c2)

  10. 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.” • Two basic approaches: • static scoping • dynamic scoping

  11. Static scope • Static scoping (also called lexical scoping): the scope of a variable is determined by the static (lexical) structure of the program.

  12. See example (on board)

More Related