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

53
CS 330 Programming Languages 10 / 23 / 2008 Instructor: Michael Eckmann

Transcript of CS 330 Programming Languages 10 / 23 / 2008 Instructor: Michael Eckmann.

CS 330Programming Languages

10 / 23 / 2008

Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS 330 - Fall 2008

Today’s Topics• Questions / comments?• Finish storage bindings (static/stack/heap)• Type Checking• Scope / Lifetime• Data Types

An aside about memory

Michael Eckmann - Skidmore College - CS 330 - Fall 2008

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

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

Binding

Michael Eckmann - Skidmore College - CS 330 - Fall 2008

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

Binding

Michael Eckmann - Skidmore College - CS 330 - Fall 2008

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

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?

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

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?

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)

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?

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

Type Checking / Strong Typing

Michael Eckmann - Skidmore College - CS 330 - Fall 2008

• 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 when?• If any bindings of variables to types are dynamic then type

checking must be done when?

Type Checking / Strong Typing

Michael Eckmann - Skidmore College - CS 330 - Fall 2008

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

Type Checking / Strong Typing

Michael Eckmann - Skidmore College - CS 330 - Fall 2008

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

Type Checking / Strong Typing

Michael Eckmann - Skidmore College - CS 330 - Fall 2008

• 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 is type coercion and why does it reduce the usefulness of strong typing?

Scope

Michael Eckmann - Skidmore College - CS 330 - Fall 2008

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

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

Scope (assume static scoping)

Michael Eckmann - Skidmore College - CS 330 - Fall 2008

procedure Big is X : Integer; procedure Sub1 is begin ... X ... end; procedure Sub2 is X : Integer; begin ... X ... end; begin ... end;

Scope

Michael Eckmann - Skidmore College - CS 330 - Fall 2008

• Static scope example• This is allowed in C and C++

void sub()

{

int count; // is hidden from use inside the while loop b/c count is

... // declared inside loop & that's the only one visible there

while (...)

{

int count;

...

count++;

}

}

• Not allowed in Java or C#

Scope

Michael Eckmann - Skidmore College - CS 330 - Fall 2008

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

Scope (assume dynamic scoping)

Michael Eckmann - Skidmore College - CS 330 - Fall 2008

procedure Big is X : Integer; procedure Sub1 is begin ... X ... // this X could the one in Big or end; // the one in Sub2 procedure Sub2 is X : Integer; begin ... X ... end; begin ... end;

Scope

Michael Eckmann - Skidmore College - CS 330 - Fall 2008

• 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”;

}

Scope

Michael Eckmann - Skidmore College - CS 330 - Fall 2008

• If dynamic scoping is allowed, when would binding of attributes of non-local variable references occur?

• I'll let you read up on the evaluation of Static and Dynamic scope in the text.

Scope vs. lifetime

Michael Eckmann - Skidmore College - CS 330 - Fall 2008

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

Scope vs. lifetime

Michael Eckmann - Skidmore College - CS 330 - Fall 2008

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

Constants and initialization

Michael Eckmann - Skidmore College - CS 330 - Fall 2008

• 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.)– Why allow initialization?

Ch. 6 - Data Types

Michael Eckmann - Skidmore College - CS 330 - Fall 2008

• A Data type

– defines a set of allowable values – defines a set of allowable operations on those values

Ch. 6 - Data Types

Michael Eckmann - Skidmore College - CS 330 - Fall 2008

• How well do the types match real-world problems is a question you might ask when considering if a language has useful types.

• Does the language you're evaluating support/provide the following:

– Built-in types --- those that are built into the language (e.g. the primitive types in Java)

– Standard types (via a standard library) --- (e.g. those like String, etc. that are provided in the Java API)

– User defined types --- example anyone?

– Abstract data types --- example anyone?

– Structured data types

• e.g. Arrays, records

• Why might we care if a language provided a type as a primitive type vs. through some standard library?

Ch. 6 - Data Types

Michael Eckmann - Skidmore College - CS 330 - Fall 2008

• Languages maintain a descriptor at compile-time and/or run-time for each variable

• Descriptor

– collection of attributes about a var stored in memory.

– Static (during compilation process) vs. dynamic (during run time)

– Used for type checking and allocation & deallocation

– What is stored is only what is necessary to determine correct types, where to find the values in memory, etc.

– e.g. Type Name, Address, Length/Size

Primitive Data Types

Michael Eckmann - Skidmore College - CS 330 - Fall 2008

• Not defined in terms of other types

• Used with type constructors to provide structured types

• Integer types

– Signed vs. unsigned

– how to store the binary representation of the integer• Sign-magnitude (sign bit and rest of bits are the abs.

value) --- makes arithmetic hard• Two's complement (positive #'s have 1st bit 0, to

negate a # invert the bits and then add 1.) --- makes arithmetic easier

• One's complement (to negate, invert all the bits.)– problem is there are two representations for 0.

Primitive Data Types

Michael Eckmann - Skidmore College - CS 330 - Fall 2008

• Floating point numbers are approximations to decimal numbers. Why only approximations?

– Fully continuous?

– Precision and range.

– some decimal numbers are repeating bits in binary

• Let's convert 0.1 to binary.

Primitive Data Types

Michael Eckmann - Skidmore College - CS 330 - Fall 2008

• Floating point are approximations to decimal numbers.

– Fully continuous? – Precision and range.

• single precision IEEE format:– 1 sign bit, 8 bits for exponent, 23 bits for

fraction• double precision IEEE format:

– 1 sign bit, 11 bits for exponent, 52 bits for fraction

Primitive Data Types

Michael Eckmann - Skidmore College - CS 330 - Fall 2008

• What if we added floating point versions of decimal numbers that were themselves not represented exactly in binary? (e.g. .1 + .1 + .1 + ...)

Primitive Data Types

Michael Eckmann - Skidmore College - CS 330 - Fall 2008

• How to “fix” problems with floating point?

• Intervals

– Interval arithmetic could guarantee that the result of all operations are within the interval

– We'll have a min and max bound on results.

– example:

• [6.74345, 6.74346] might be an interval value and

• another might be [4.5, 4.5001]

– Interval arithmetic would be defined

• how to add them? how to multiply them? etc.

• Any advantages/disadvantages vs. typical doubles?

• What if your language didn't have interval type, what could you do if you liked this type?

Primitive Data Types

Michael Eckmann - Skidmore College - CS 330 - Fall 2008

• Another way to "fix" the problems with floating point types is:

• Decimal types

– Most languages we use don't have a decimal type.– Cobol does, so does C#.– BCD (Binary Coded Decimal)– stores 1 or 2 digits per byte (why not 3 digits / byte?)– Why have decimal types if they waste storage?

Primitive Data Types

Michael Eckmann - Skidmore College - CS 330 - Fall 2008

• Decimal types

– Why have decimal types if they waste storage?• Because they precisely store decimal values which

floating point types do not

Primitive Data Types

Michael Eckmann - Skidmore College - CS 330 - Fall 2008

• Boolean

– Could be represented by a single bit, but are often represented by a byte for more efficient memory access.

• Character– ASCII (8 bit)– ISO 8859-1 (8 bit)– Unicode (16 bit) --- Java & C# use this

• 1st 128 are ASCII

ASCII

Michael Eckmann - Skidmore College - CS 330 - Fall 2008

Unicode

Michael Eckmann - Skidmore College - CS 330 - Fall 2008

• http://www.unicode.org/standard/WhatIsUnicode.html

• Has characters in alphabets of many languages

Character strings

Michael Eckmann - Skidmore College - CS 330 - Fall 2008

• Implemented as

– Character array vs. primitive type– Static vs. dynamic length

• C & C++ strings are terminated with a null character (ASCII 0). – Length is not maintained but can be determined. The

end of the string is determined by the null character.– All that needs to be stored is a pointer to the first

character in the string.– Limited dynamic length (limited because there is a

maximum length)

Character strings

Michael Eckmann - Skidmore College - CS 330 - Fall 2008

• Java has two different types for Strings.

– String• Static length constant strings

– StringBuffer• Dynamic length and allows direct subscripting

• Fortran 95– String is a primitive type– Has typical operations for strings

• Issues– What to do when assigning strings of different

lengths? etc.

Character strings

Michael Eckmann - Skidmore College - CS 330 - Fall 2008

• Pattern matching with character strings

– Perl, JavaScript, PHP built in pattern matching with regular expressions

– Included in class libraries of C++, Java and C#

Character strings

Michael Eckmann - Skidmore College - CS 330 - Fall 2008

• Length

– Static length– Limited dynamic length– Dynamic length

• Requires dynamic storage allocation and deallocation

• Maximum flexibility

Character strings

Michael Eckmann - Skidmore College - CS 330 - Fall 2008

• Implementation

– Most often software (as opposed to hardware) implementation of storage, retrieval and manipulation

• Descriptor– Type name– Length (for static strings)– Address of first character

Character stringsDescriptor for static strings

Michael Eckmann - Skidmore College - CS 330 - Fall 2008

Character strings

Michael Eckmann - Skidmore College - CS 330 - Fall 2008

• Descriptor for limited dynamic

– Type name (Limited dynamic string)– Maximum length– Current length– Address of first character

• For dynamic length strings– Could be stored in linked list– Could be stored in adjacent storage cells

• What about when length changes? How can this be done?

Character strings

Michael Eckmann - Skidmore College - CS 330 - Fall 2008

• Linked list vs. adjacent evaluation

– Linked lists• string operations become complex (following

pointers around)• allocation and deallocation is simple and fast• but requires more storage (for the pointers or

references)– Adjacent

• faster string operations (because contiguous)• less storage• but allocation and deallocation is slower

Ordinal / Enumeration

Michael Eckmann - Skidmore College - CS 330 - Fall 2008

• Ordinal types

– Range of values are associated with positive integers

• Many languages provide support for user defined ordinal types like Enumerations.

• Enumeration types

– Named constants represent positive integers

– Design issues• Are enumeration type values coerced to integer?

– Same operations and range of values of integers• Are any types coerced to enumeration type values?

– Could break the allowable values of the enum type

– Range of values intentionally limited.

Enumeration

Michael Eckmann - Skidmore College - CS 330 - Fall 2008

• e.g. In C#

enum months {Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec};

• e.g. In C++

enum months {Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec};

months m; // declare a variable of type months

• Represented as 0 to 11 typically but could be given programmer specified values in the declaration.

• They look the same, but C# enum types not coerced to integer whereas C++'s are --- so, C# doesn't allow operations that don't make sense, while C++ does. e.g. In C++, we can add 1 to an enum

Enumeration

Michael Eckmann - Skidmore College - CS 330 - Fall 2008

• e.g. In C++

enum months {Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec};

// declares currentMonth of type months and assigns

// an initial value

months currentMonth = Jun;

• In C++, we can add 1 to an enum– e.g. currentMonth++;

• Any danger here?

Enumeration

Michael Eckmann - Skidmore College - CS 330 - Fall 2008

• As of Java 1.5 (aka 5.0), Java does support enumerations. When used, they create full classes for the enum. See:

http://java.sun.com/j2se/1.5.0/docs/guide/language/enums.html

• What are advantages to enumeration types?

Enumeration

Michael Eckmann - Skidmore College - CS 330 - Fall 2008

• What are advantages to enumeration types?

– All forms of implementation of them offer better readability

– Reliability• No arithmetic operations on the enum types in Ada

and C# --- this prevents adding/subtracting which might not make sense like in adding months together

• Ada and C# also restrict values within the range for the type.

• C treats them like integers so we don't get those advantages

Enumeration

Michael Eckmann - Skidmore College - CS 330 - Fall 2008

– Reliability• C++ allows numeric values to be assigned to enum

types if they are explicitly cast.• C++ also allows values to be assigned to enum

types but the range is checked for validity.– This just checks that the integer is between the

lowest and highest enum value. Enum values need not be consecutive integers. Any problem here?