COP 4020 Lecture 3 Overview Binding & Lifetime Examples 1/14/02 Binding: An association between an attribute and an entity OR an operation and a symbol. Six possible times when bindings occur: 1) Language design time (like * = multiplication) 2) Language implementation (like the int type) 3) Compile time (like a variable to its type) 4) Link time (like a library subprogram to its corresponding code) 5) Program loaded into memory (Some vars are bound to memory locations) 6) Run time (Other variables are bound to memory locations) A binding is known as static if it occurs befor run time, and dynamic if it occurs during run time. Type bindings ------------- Variable Declarations 1. Explicit Declarations (what you are used to) 2. Implicit Declarations (old Fortran's variables) 3. Dynamic Binding (Type determined in an assignment statement) Advantage: more program flexibility Disadvantage: less error detection, considerable execution cost Generally implemented only in pure interpreters. Storage Binding and Lifetime a. Allocation - binding a memory cell to a variable b. Deallocation - freeinug a memory cell from its binding. Lifetime: The duration of time during execution when a variable is bound to a memory location. A history sensitive variable in a subprogram is statically bound. (An example of this is a static variable in a function in C. It's value is dependent on previous executions of the function during the execution of the program.) Static variables are bound to memory cells before program execution. The drawback to having all variables statically bound is that there is less flexibility. In particular, recursion is not possible and the same storage can not be shared by different variables. (This sounds like not a big deal, but imagine a program that has two huge arrays, which are never valid at the same time. It would be wasteful to use different memory for them entirely.) For the rest of the lecture, we traced the lifetimes of variables in the code examples that are posted on the class web page.