+ All Categories
Home > Documents > CS 330 Programming Languages 10 / 23 / 2007 Instructor: Michael Eckmann.

CS 330 Programming Languages 10 / 23 / 2007 Instructor: Michael Eckmann.

Date post: 18-Jan-2018
Category:
Upload: sharyl-park
View: 218 times
Download: 0 times
Share this document with a friend
Description:
Binding Michael Eckmann - Skidmore College - CS Fall 2007 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
28
CS 330 Programming Languages 10 / 23 / 2007 Instructor: Michael Eckmann
Transcript
Page 1: CS 330 Programming Languages 10 / 23 / 2007 Instructor: Michael Eckmann.

CS 330Programming Languages

10 / 23 / 2007

Instructor: Michael Eckmann

Page 2: CS 330 Programming Languages 10 / 23 / 2007 Instructor: Michael Eckmann.

Michael Eckmann - Skidmore College - CS 330 - Fall 2007

Today’s Topics• Questions / comments?• Chapter 5

– names– bindings– type checking– scope

Page 3: CS 330 Programming Languages 10 / 23 / 2007 Instructor: Michael Eckmann.

Binding

Michael Eckmann - Skidmore College - CS 330 - Fall 2007

• 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

Page 4: CS 330 Programming Languages 10 / 23 / 2007 Instructor: Michael Eckmann.

Binding

Michael Eckmann - Skidmore College - CS 330 - Fall 2007

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

Page 5: CS 330 Programming Languages 10 / 23 / 2007 Instructor: Michael Eckmann.

Binding

Michael Eckmann - Skidmore College - CS 330 - Fall 2007

• Type bindings• Variable declaration

• When a variable is declared it is given a type (and a size) 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 declare a var without defining it.

– The var needs to be defined somewhere though.

Page 6: CS 330 Programming Languages 10 / 23 / 2007 Instructor: Michael Eckmann.

Binding

Michael Eckmann - Skidmore College - CS 330 - Fall 2007

• 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?

Page 7: CS 330 Programming Languages 10 / 23 / 2007 Instructor: Michael Eckmann.

Binding

Michael Eckmann - Skidmore College - CS 330 - Fall 2007

• Dynamic Type Binding• Advantage: flexibility (generic program units --- what do you think this

means?)• Disadvantages:

– Decreased reliability - Why? – High cost at run-time due to what?

Page 8: CS 330 Programming Languages 10 / 23 / 2007 Instructor: Michael Eckmann.

Binding

Michael Eckmann - Skidmore College - CS 330 - Fall 2007

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

Page 9: CS 330 Programming Languages 10 / 23 / 2007 Instructor: Michael Eckmann.

Type Inference

Michael Eckmann - Skidmore College - CS 330 - Fall 2007

• ML is a language with both functional and imperative features and can infer types without the programmer having to specify them.

• Two types are real (floating point) and int. Examples (straight out of the text):

fun circumf(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; )

Page 10: CS 330 Programming Languages 10 / 23 / 2007 Instructor: Michael Eckmann.

An aside about memory

Michael Eckmann - Skidmore College - CS 330 - Fall 2007

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

Page 11: CS 330 Programming Languages 10 / 23 / 2007 Instructor: Michael Eckmann.

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

Page 12: CS 330 Programming Languages 10 / 23 / 2007 Instructor: Michael Eckmann.

Binding

Michael Eckmann - Skidmore College - CS 330 - Fall 2007

• 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?

Page 13: CS 330 Programming Languages 10 / 23 / 2007 Instructor: Michael Eckmann.

Binding

Michael Eckmann - Skidmore College - CS 330 - Fall 2007

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

Page 14: CS 330 Programming Languages 10 / 23 / 2007 Instructor: Michael Eckmann.

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?

Page 15: CS 330 Programming Languages 10 / 23 / 2007 Instructor: Michael Eckmann.

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

Page 16: CS 330 Programming Languages 10 / 23 / 2007 Instructor: Michael Eckmann.

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?

Page 17: CS 330 Programming Languages 10 / 23 / 2007 Instructor: Michael Eckmann.

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)

Page 18: CS 330 Programming Languages 10 / 23 / 2007 Instructor: Michael Eckmann.

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?

Page 19: CS 330 Programming Languages 10 / 23 / 2007 Instructor: Michael Eckmann.

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

Page 20: CS 330 Programming Languages 10 / 23 / 2007 Instructor: Michael Eckmann.

Type Checking / Strong Typing

Michael Eckmann - Skidmore College - CS 330 - Fall 2007

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

Page 21: CS 330 Programming Languages 10 / 23 / 2007 Instructor: Michael Eckmann.

Type Checking / Strong Typing

Michael Eckmann - Skidmore College - CS 330 - Fall 2007

• 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?

Page 22: CS 330 Programming Languages 10 / 23 / 2007 Instructor: Michael Eckmann.

Type Checking / Strong Typing

Michael Eckmann - Skidmore College - CS 330 - Fall 2007

• 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?

Page 23: CS 330 Programming Languages 10 / 23 / 2007 Instructor: Michael Eckmann.

Scope

Michael Eckmann - Skidmore College - CS 330 - Fall 2007

• 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. 226) • Blocks (p. 227)

– Answers the question – When we reference a name of a variable which variable is it?

Page 24: CS 330 Programming Languages 10 / 23 / 2007 Instructor: Michael Eckmann.

Scope

Michael Eckmann - Skidmore College - CS 330 - Fall 2007

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

Page 25: CS 330 Programming Languages 10 / 23 / 2007 Instructor: Michael Eckmann.

Scope

Michael Eckmann - Skidmore College - CS 330 - Fall 2007

• Dynamic scope example from Perl:$var = 10;sub1(); # will print Yikessub2(); # will print 10sub sub1{

local $var = “Yikes”;sub2();

}sub sub2{

print $var . “\n”;}

Page 26: CS 330 Programming Languages 10 / 23 / 2007 Instructor: Michael Eckmann.

Scope vs. lifetime

Michael Eckmann - Skidmore College - CS 330 - Fall 2007

• 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?

Page 27: CS 330 Programming Languages 10 / 23 / 2007 Instructor: Michael Eckmann.

Scope vs. lifetime

Michael Eckmann - Skidmore College - CS 330 - Fall 2007

• 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?

Page 28: CS 330 Programming Languages 10 / 23 / 2007 Instructor: Michael Eckmann.

Constants and initialization

Michael Eckmann - Skidmore College - CS 330 - Fall 2007

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


Recommended