Chapter 5 Variables

Chapter 5 Variables

Topics Binding Chapter 5 Lifetime Scope Variables Constants Chapter 5: Variables 2 Variables: attributes Binding A variable can be thought of as being completely The assignment statement is really an specified by its 6 basic attributes: instance of a more general phenomenon of 1. Name: identifier attaching various kinds of values to names. 2. Address: memory location(s) The association of a name to an attribute is 3. Value: particular value at a moment called binding 4. Type: range of possible values n Assignment statement binds a value to a 5. Lifetime: when the variable is accessible location. 6. Scope: where in the program it can be accessed n Identifiers are bond to locations, types, and other attributes at various points in the translations of a program. Chapter 5: Variables 3 Chapter 5: Variables 4 Binding Binding Binding time. Bindings happen at different 3. Compile time and invisible points. n Example: bind a variable to a type in C or Java Possible binding times 4. Link time n Example: bind a call to a library function to the 1. Language design time function code. n Bind operator symbols to operations 5. Load time n Example: bind * to multiplication n Example: bind a C static variable to a memory 2. Language implementation time cell. n Example: bind floating point type to a representation (IEEE floating-point format) 6. Runtime n Example: bind a nonstaticlocal variable to a n Example: the data type int in Java is bound to a range of values. memory cell Chapter 5: Variables 5 Chapter 5: Variables 6 1 The Concept of Binding Static and Dynamic Binding Consider the following: A binding is static int C; n it occurs before run time and C := C + 5; n It remains unchanged throughout program execution n Some of the bindings and their binding times A binding is dynamic are: n It occurs during execution or The type of C is bound at compiletime. The set of possible values of C is bound at compiler design n It can change during execution of the program time. As binding time gets earlier: The meaning of the operator + is bound at compiletime (when the types of its operands have been determined) n Efficiency goes up The internal representation of the literal 5 is bound at n Safety goes up compiler design time. The value of C is bound at run time. n Flexibility goes down Chapter 5: Variables 7 Chapter 5: Variables 8 Type Bindings Variable Declarations A variable must be bound to a data type before An explicit declaration is a program statement it can be referenced. used for declaring the types of variables. Two key issues in binding a type to an n Example: identifier: int x; n 1. How is the type specified? Advantage: safer, cheaper 2. When does the binding take place? n Disadvantage: less flexible How? – two kinds of declarations: An implicit declaration is a default mechanism 1. Explicit declarations for specifying types of variables (the first 2. Implicit declarations appearance of the variable in the program) When? - three kinds of type bindings: n Example: in FORTRAN, variables beginning with I-N 1. Static type binding are assumed to be of type integer. 2. Dynamic type binding 3. Type inference Chapter 5: Variables 9 Chapter 5: Variables 10 Variable Declarations Variable Declarations Advantages: convenience Implicit declarations leave more room for Disadvantage: reliability (some typographical and error programmer errors cannot be detected. n Example: In FORTRAN variables left Intermediate position: Names for specific types undeclared will be implicitly declared as an must begin with a given character. integer. n Example: in Perl, variables of type scalar, array and hash structures begin with a $, @, or %, respectively. n Advantages: Different namespaces for different type variables @apple vs. %apple vs. @apple The type of a variable is know through its name. Chapter 5: Variables 11 Chapter 5: Variables 12 2 Dynamic Type Binding Dynamic Type Binding The variable is bound to a type when it is n Disadvantages: Compiler’s type error detection is minimized. assigned a value in an assignment If RHS is not compatible with LHS, the type of LHS is changed as statement. opposed to generating an error. n This issue also appears in static type binding languages like C and n JavaScript and PHP C++ n Example: in JavaScript Must be implemented by a pure interpreter rather than a compiler list = { 2, 4, 6, 8 }; n It is not possible to create machine code instructions whose operand types are not known at compile time. list = 17.3; High cost: n Dynamic binding of objects. n Type checking must be done at runtime n Every variable must know its current type n Advantage: flexibility (generic program units) n A variable might have varying sizes because different type values require different amounts of storage. n Must be interpreted. Chapter 5: Variables 13 Chapter 5: Variables 14 Type Inference Type Inference Rather than by assignment statement, types n Illegal: are determined from the context of the fun square(x) = x * x reference. // can’t deduce anything ( a default value could be Type inferencing is used in some assigned) programming languages including ML, n Fixed Miranda, and Haskell. fun square(x : real) = x * x; Example: // use explicit declaration fun square(x) = (x : real) * x; n Legal: fun square(x) : real = x * (x : real); fun circumf(r) = 3.14159 * r * r; // infer r is real fun time10(x) = 10 * x; // infer s is integer Chapter 5: Variables 15 Chapter 5: Variables 16 Storage Bindings & Lifetime Variables: lifetime Allocation is the process of getting a cell from some pool of available cells. Categories of scalar variables by lifetimes: Deallocation is the process of putting a cell n Static back into the pool. n Stack-dynamic The lifetime of a variable is the time during n Explicit heap-dynamic which it is bound to a particular memory cell. n Implicit hep-dynamic n Begin: when the variable is bound to a specific cell n Ends: when the variable is unbound from that cell. Chapter 5: Variables 17 Chapter 5: Variables 18 3 Static Variables Static Variables Bound to memory cells before execution and remains bound to the same memory cell Disadvantages: throughout execution n If a language only has static variables then n Example: all FORTRAN 77 variables Recursion cannot be supported (lack of flexibility). n Example: C static variables Storage cannot be shared among variables Advantages: (more storage required) n Efficiency (direct addressing) n No allocation/deallocation needed (which is run time overhead) n History-sensitive subprogram support (retain values between separate executions of the subprogram) Chapter 5: Variables 19 Chapter 5: Variables 20 Stack-dynamic Variables Stack-dynamic Variables Storage bindings are created for variables in Advantages: the run time stack when their declaration n Allows recursion statement are elaborated (or execution n Conserves storage reaches the code to which declaration is Disadvantages: attached), but types are statically bound. n Run time overhead for allocation and n If scalar, all attributes except address are deallocation. statically bound n Subprogram cannot be history sensitive Example: local variables in C subprograms and Java n Inefficient references (indirect addressing) methods n Limited by stack size. Chapter 5: Variables 21 Chapter 5: Variables 22 Explicit Heap-dynamic Variables Explicit Heap-dynamic Variables Allocated and deallocated by explicit Disadvantages: directives, specified by the programmer, which take effect during execution. n Unreliable (forgetting to delete) n Difficult of using pointer and reference variables n Referenced only through pointers or correctly references n Inefficient. Example: dynamic objects in C++ (via new/delete, malloc/free) Example: Example: all objects in Java (except primitives) int *intnode; // create a pointer Advantages: … intnode = new int // create the heap-dynamic variable n Provides for dynamic storage management … delete intnode; // deallocate the heap-dynamic variable Chapter 5: Variables 23 Chapter 5: Variables 24 4 Implicit Heap-dynamic Variables Summary Table Allocation and deallocation caused by Variable Storage binding time Dynamic storage Type assignment statements and types not Category from binding determined until assignment. Static Before execution Static n Example: All arrays and strings in Perl and JavaScript n Example: all variables in APL Stack-dynamic When declaration is Run-time stack Static Advantage: highest degree of flexibility elaborated (run time) Disadvantages: Explicit heap- By explicit instruction Heap Static dynamic (run time) n Inefficient because all attributes are dynamic (a lot of overhead) Implicit heap- By assignment (run Heap Dynamic dynamic time) n Loss of error detection Chapter 5: Variables 25 Chapter 5: Variables 26 Type Checking Type Checking Generalizes the concept of operands and A compatible type is one that is either: operators to include subprograms and n Legal for the operator, or assignments: n Allowed under language rules to be implicitly n Subprogram is operator, parameters are converted to a legal type by compiler-generated operands. code. n Assignment is operator, LHS and RHS are n This automatic conversion is called coercion operands. Example: adding an int to a float in Java is allowed, Type checking is the activity of ensuring then int is coerced. that the operands of an operator are of compatible types. A type error is the application of an operator to an operand of an inappropriate type. Chapter 5: Variables 27 Chapter 5: Variables 28 Type Checking Strong Typing If all type bindings are A programming language is strongly typed if n Static: nearly all type checking can be static n Type errors are always detected. n Dynamic: type checking must be dynamic n There is strict enforcement of type rules with no Static type checking is less costly (it is better to exceptions.

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    10 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us