Download presentation
Presentation is loading. Please wait.
1
CS 330 Programming Languages 10 / 24 / 2006 Instructor: Michael Eckmann
2
Michael Eckmann - Skidmore College - CS 330 - Fall 2006 Today’s Topics Questions / comments? Continue with Chapter 5 –Variables continued –Binding –Scope
3
Variables Michael Eckmann - Skidmore College - CS 330 - Fall 2006 What's a variable? Is it the name of a memory location? Yes. But it is more. Name, address, value, type, size And two more things: –lifetime (how long the name is attached to the memory location), –scope (where in the code can it be referenced)
4
Variables Michael Eckmann - Skidmore College - CS 330 - Fall 2006 Lifetime (how long the name is attached to the memory location), Scope (where in the code can it be referenced) example: public int factorial(int num) { int result = 1; while (num > 1) { result *= num; num--; } return result; }
5
Variables Michael Eckmann - Skidmore College - CS 330 - Fall 2006 Lifetime (how long the name is attached to the memory location), Scope (where in the code can it be referenced) example: public void meth1() { //.... } public void meth2() { String s = “Hey”; //.... meth1(); }
6
Variables Michael Eckmann - Skidmore College - CS 330 - Fall 2006 Memory address –variables are associated with a memory address –may be possible for same name to be associated with different addresses at different times during execution at different places in the code –can anyone give an example of these? –the address of a variable is sometimes called its l-value Aliases –anyone want to define alias in terms of names and addresses?
7
Variables Michael Eckmann - Skidmore College - CS 330 - Fall 2006 Aliases –more than one name associated to the same memory address. –example: 2 variables x and y both refer to same address, so if I change the value of x, y's value changes too because it refers to the same address –effects on readability or writability? –An example of aliases in Java are that they are created when passing certain kinds of parameters (e.g. a reference to a class, or an array reference) Can aliases occur anywhere else in Java? Type –What does a type do? Size Value –sometimes referred to as a variable's r-value. Why do you think?
8
Variables Michael Eckmann - Skidmore College - CS 330 - Fall 2006 Type –determines the kind and range of possible values –and the operations that can be performed on variables of the type Size –how much memory --- determined by the type Value –contents of the variable at any given time during run-time –sometimes referred to as a variable's r-value.
9
Binding Michael Eckmann - Skidmore College - CS 330 - Fall 2006 Binding is an association between an attribute and an entity an operation and a symbol Times when binding takes place Language (compiler) design time Language implementation time Compile time Link time (when your compiled code is linked to other libraries) Load time (when the program is executed it gets loaded into memory) Run time (when the program starts executing after being loaded into memory)
10
Binding Michael Eckmann - Skidmore College - CS 330 - Fall 2006 Example from the text illustrates some of these ideas: int count; //... count = count + 5; –The text discusses these assuming the code is in C. Even if we don't know C we should be able narrow down when the bindings below occur. type of count is bound when? set of possible values of an int is bound when? meaning of + operator is bound when? internal representation of literal 5 is bound when? the value of count is bound when?
11
Binding Michael Eckmann - Skidmore College - CS 330 - Fall 2006 The answers for the C language are: type of count is bound when? compile time set of possible values of int is bound when? compiler- implementation time –what does that imply? meaning of + operator is bound when? compile-time –what does that imply? internal representation of literal 5 is bound when? compiler-design time the value of count is bound when? run-time
12
Binding Michael Eckmann - Skidmore College - CS 330 - Fall 2006 Examples of bindings that occur at –link time a method/function call bound to the specific code in the library in which it is defined –load time global variable may be bound to specific memory storage at load time
13
Binding Michael Eckmann - Skidmore College - CS 330 - Fall 2006 Binding attributes to variables –Static vs. Dynamic binding Static binding – first occurs before run time and stays same throughout program execution note: load-time is occurs before run-time Dynamic binding Either it first occurs during run time OR it can change during execution it is dynamic
14
Binding Michael Eckmann - Skidmore College - CS 330 - Fall 2006 Type bindings Variable declaration Explicit (what we're used to -- declare a variable by giving it a name and a type.) Implicit – associate a type based on naming convention and declaration happens on first appearance –I, J,..., N are implicitly Integer types in Fortran –(sort of occurs in Perl, where the first char of a variable name specifies its type, e.g. @ is for arrays, $ for scalars, % for keyed arrays (aka hashes). But scalars can hold integers, floats, or strings.)
15
Binding Michael Eckmann - Skidmore College - CS 330 - Fall 2006 Type bindings Variable declaration When a variable is declared it is given a type and a memory address Declaration vs. definition –In C & C++ there is a difference between declaration and definition –declarations specify types but do not allocate memory –definitions specify type and allocate memory –because C & C++ have a keyword extern which declares a var but does not allocate storage --- it is assumed to be done elsewhere. So, in that way one can define a var without declaring it. –The var needs to be declared somewhere though.
16
Binding Michael Eckmann - Skidmore College - CS 330 - Fall 2006 Dynamic Type Binding (used in JavaScript and PHP among others) - allows any variable to be assigned a value of any type. Type is specified through an assignment statement e.g., JavaScript list = [2, 4.33, 6, 8]; list = 17.3; Advantages and Disadvantages to Dynamic Type Binding?
17
Binding Michael Eckmann - Skidmore College - CS 330 - Fall 2006 Dynamic Type Binding Advantage: flexibility (generic program units --- what do you think this means?) Disadvantages: –Decreased reliability - Why? Type error detection by the compiler is difficult or impossible, why? compiler can't detect errors involving incorrect assignment of types. Are these kinds of errors detected in Java? –High cost at run-time due to: dynamic type checking Pure interpretation is required for these languages b/c if types are not known at compile time, machine language instructions cannot be generated. Textbook claims that pure interpreted languages take at least 10 times as long as equivalent machine code.
18
Type Inference Michael Eckmann - Skidmore College - CS 330 - Fall 2006 ML is a language with both functional and imperitive features and can infer types without programmer having to specify them. Two types are real (floating point) and int. Examples (straight out of the text): fun curcumf(r) = 3.14159 * r * r; fun times10(x) = 10 * x; fun square(x) = x * x; type of r is inferred to be real and circumf's return type is also inferred to be real due to the 3.14159 being a real. type of x is inferred to be int and times10's return type is also inferred to be int due to the 10 being and int. default numeric type is int, so square's return type and parameter x are ints. fun squarer(x : real) = x * x; (or fun squarer(x) :real = x * x; )
19
An aside about memory Michael Eckmann - Skidmore College - CS 330 - Fall 2006 How is memory divided up and categorized for executing programs? A typical setup is as follows. I'll draw a diagram on the board. –executable code –static data area used for statically declared objects like globals and constants –stack (grows one way) used for local variable allocation during “procedure / method / subroutine / function” calls. Note: I will constantly use these four words interchangeably. –heap (grows the other way) used for dynamic objects –objects that are allocated at runtime, may change and size not known until run time e.g. linked lists with unknown number of nodes, trees with unknown number of nodes, etc.
20
Binding Storage bindings and lifetime Static variables (lifetime is the total time of execution) e.g. globals, static variables allocated in the static data area Stack-dynamic variables (what's the lifetime of these?) e.g. Local variables to methods allocated on the stack. When are they allocated and deallocated? Explicit heap-dynamic variables e.g. Variables used for data structures that shrink and grow during execution, or those only referenced through pointers or references. Can anyone give examples? allocated / deallocated on the heap Implicit heap-dynamic variables All attributes (type, size, etc.) of these are bound when value is assigned. e.g. The JavaScript example of list a few slides ago. allocated / deallocated on the heap
21
Binding Michael Eckmann - Skidmore College - CS 330 - Fall 2006 Evaluation of these kinds of variables. Think in terms of memory space, cost of execution, reliability, efficiency etc. Static variables What are some advantages and disadvantages?
22
Binding Michael Eckmann - Skidmore College - CS 330 - Fall 2006 Evaluation of these kinds of variables. Think in terms of memory space, cost of execution, reliability, efficiency etc. Static variables What are some advantages and disadvantages? + ability to have history sensitive variables inside a method/function + efficient - addressing is direct + allocation is done before run-time so there is no run-time overhead of allocation and deallocation - reduced flexibility with only static variables you couldn't write recursive routines -why? - could waste memory can't share storage with other variables that do not have to overlap existence.
23
Binding Michael Eckmann - Skidmore College - CS 330 - Fall 2005 Evaluation of these kinds of variables. Think in terms of memory space, cost of execution, reliability, efficiency etc. Stack-dynamic variables What are some advantages and disadvantages?
24
Binding Michael Eckmann - Skidmore College - CS 330 - Fall 2005 Evaluation of these kinds of variables. Think in terms of memory space, cost of execution, reliability, efficiency etc. Stack-dynamic variables What are some advantages and disadvantages? + allows recursion + share memory space with other stack-dynamic variables - slower because bindings at runtime
25
Binding Michael Eckmann - Skidmore College - CS 330 - Fall 2005 Evaluation of these kinds of variables. Think in terms of memory space, cost of execution, reliability, efficiency etc. Explicit heap-dynamic variables (e.g. Java references to objects) What are some advantages and disadvantages?
26
Binding Michael Eckmann - Skidmore College - CS 330 - Fall 2005 Evaluation of these kinds of variables. Think in terms of memory space, cost of execution, reliability, efficiency etc. Explicit heap-dynamic variables (e.g. Java references to objects) What are some advantages and disadvantages? + flexibility / expressivity - pointers/references could be difficult to use correctly - slower at runtime b/c indirect addressing (what is indirect addressing?) - complex implementation of how these variables are stored and accessed in memory - complicated and costly heap management (e.g. Java's garbage collection)
27
Binding Michael Eckmann - Skidmore College - CS 330 - Fall 2005 Evaluation of these kinds of variables. Think in terms of memory space, cost of execution, reliability, efficiency etc. Implicit heap-dynamic variables What are some advantages and disadvantages?
28
Binding Michael Eckmann - Skidmore College - CS 330 - Fall 2005 Evaluation of these kinds of variables. Think in terms of memory space, cost of execution, reliability, efficiency etc. Implicit heap-dynamic variables What are some advantages and disadvantages? + extremely flexible, generic code (same code works for many types) easy to write - slower - reduced error detection, therefore reduced safety
29
Type Checking / Strong Typing Michael Eckmann - Skidmore College - CS 330 - Fall 2006 Type checking --- checks that the operands of an operation are of compatible types –what does compatible type mean? If all bindings of variables to types are static then type checking can be done before run-time. If any bindings of variables to types are dynamic then type checking is required at run-time. This is Dynamic Type Checking. Strong Typing – defined by the text as --- a language is strongly typed if type errors are always detected (whether at compile-time or run-time).
30
Type Checking / Strong Typing Michael Eckmann - Skidmore College - CS 330 - Fall 2006 Strong Typing – defined by the text as --- a language is strongly typed if type errors are always detected (whether at compile-time or run-time). According to this definition, what would you say about Java --- is it strongly typed?
31
Type Checking / Strong Typing Michael Eckmann - Skidmore College - CS 330 - Fall 2006 Strong Typing – defined by the text as --- a language is strongly typed if type errors are always detected (whether at compile-time or run-time). According to this definition, what would you say about Java --- is it strongly typed? –Yes nearly. Only casting (which is explicitly done by the programmer) could cause an error to go undetected. –Also, type coercion reduces the usefulness of strong typing to some extent. What do you think I mean by this?
32
Scope Michael Eckmann - Skidmore College - CS 330 - Fall 2006 Scope is the range of statements in which a variable is visible (which means where it can be referenced.) Static scope Static – scope of variables can be determined prior to run-time. It is a spatial relationship. –Nesting of functions/methods (p. 229) Blocks (p. 230) –Answers the question – When we reference a name of a variable which variable is it?
33
Scope Michael Eckmann - Skidmore College - CS 330 - Fall 2006 Dynamic scope Scope of variables are determined at run-time. It is a temporal relationship, based on calling sequence. –Advantage: convenience –Disadvantage: poor readability –Also answers the question – When we reference a name of a variable which variable is it? -- but we may get a different answer vs. if the language used static scoping.
34
Scope Michael Eckmann - Skidmore College - CS 330 - Fall 2006 Dynamic scope example from Perl: $var = 10; sub1(); # will print Yikes sub2(); # will print 10 sub sub1 { local $var = “Yikes”; sub2(); } sub sub2 { print $var. “\n”; }
35
Scope vs. lifetime Michael Eckmann - Skidmore College - CS 330 - Fall 2006 Scope is typically spatial, lifetime is always temporal. They are related in many cases, but two examples where they are clearly different concepts: static variables can be declared in a function in C++ & Java. These variables are used to retain values between subsequent calls. e.g. If you wanted to know how many times you called the constructor for some class (i.e. how many instances of that class were created) during the program you could use a static variable that has one added to it each time in that constructor. What's the scope of a variable like this? What's its lifetime?
36
Scope vs. lifetime Michael Eckmann - Skidmore College - CS 330 - Fall 2006 Many languages do not allow you to reference variables of a function that calls a particular function. e.g. Function1() { // do some stuff here } Function2() { int x; Function1(); } What's the scope of x? What's its lifetime?
37
Constants and initialization Michael Eckmann - Skidmore College - CS 330 - Fall 2006 What good are named constants? –Enhances readability. Anything else? Initialization –Binds a value to a variable when that variable is bound to storage (i.e. when memory is allocated.) –Occurs once for static variables –Occurs each time code is executed for dynamic variables (either stack-dynamic or heap-dynamic.)
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.