cs 330 programming languages 10 / 31 / 2006 boo! instructor: michael eckmann
TRANSCRIPT
CS 330Programming Languages
10 / 31 / 2006
Boo!
Instructor: Michael Eckmann
Michael Eckmann - Skidmore College - CS 330 - Fall 2006
Today’s Topics• Questions / comments?• Midterm Exam grade explanation• Continue with Chapter 5
– Binding– Type Checking– Scope– Lifetime– Named Constants
• Chapter 6– Data types
Michael Eckmann - Skidmore College - CS 330 - Fall 2006
0-4.0 scale grade coversion• Your exam was graded on a curve.• The total points: 111
• The mean: 83.63
• The standard deviation: 18.54
• Lowest: 61
• Highest: 108
• Your RawScore is the number of points you earned out of 111 (the numerator on the front of your test.)
(RawScore - 83.63) EC * 4.0 2.8 + ----------------------- + ------------ 18.54 111
examples for a raw score of 105 + 4 EC and of 71 + 0 EC
Michael Eckmann - Skidmore College - CS 330 - Fall 2006
0-4.0 scale grade coversion
These are the minimum numbers for each letter grade.
e.g.
a 2.9 is a B
a 3.53 is an A-
0.51 D -
0.84 D
1.17 D +
1.51 C-
1.84 C
2.17 C +
2.51 B-
2.84 B
3.17 B+
3.51 A-
3.84 A
4.17 A+
Michael Eckmann - Skidmore College - CS 330 - Fall 2006
Announcement• Ken Appel talk at 5:30 on Tuesday (today!) in Bolton 281.
• The title is:
• Mathematicians' Attitudes Towards Computer Proofs
• Also Wednesday at 10:10-11:05 a.m. in Linux Lab Ken Appel will give a talk on the Four Color Theorem.
Binding
Michael Eckmann - Skidmore College - CS 330 - Fall 2006
• Binding attributes to variables
– Static vs. Dynamic binding
– Explicit vs. Implicit variable declaration
• Can someone explain the difference between Static and Dynamic binding of attributes to variables?
• Can someone else explain the difference between Explicit and Implicit variable declarations?
Binding
Michael Eckmann - Skidmore College - CS 330 - Fall 2006
• Binding attributes to variable• 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
• 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
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.
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 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?
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. Example?
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.
• Stack-dynamic variables
• What are some advantages and disadvantages?
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.
• Stack-dynamic variables
• What are some advantages and disadvantages?
+ allows recursion
+ share memory space with other stack-dynamic variables
- slower (than static) because bindings to storage/memory occur at runtime
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.
• Explicit heap-dynamic variables (e.g. Java references to objects)
• What are some advantages and disadvantages?
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.
• Explicit heap-dynamic variables (e.g. Java references to objects)
• What are some advantages and disadvantages?
+ flexibility / expressivity
+ passing references/pointers to methods/functions is more efficient than passing by value --- why?
- 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 2006
• Evaluation of these kinds of variables. Think in terms of memory space, cost
of execution, reliability, efficiency etc.
• Implicit heap-dynamic variables (everything bound at assignment)
• What are some advantages and disadvantages?
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.
• Implicit heap-dynamic variables (everything bound at assignment)
• What are some advantages and disadvantages?
+ extremely flexible, can easily write very generic code (same code works for many types) -- how is this possible?
- slower at runtime b/c of overhead asssoc. with maintaining all the dynamic attributes (they can change remember)
- reduced error detection, therefore reduced safety
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 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 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.
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?
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 is type coercion and why does it reduce the usefullness of strong typing?
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-230) • Blocks (p. 231)
– Answers the question – When we reference a name of a variable which variable is it?
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.
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”;
}
Scope
Michael Eckmann - Skidmore College - CS 330 - Fall 2006
• 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 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?
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?
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.)
Ch. 6 - Data Types
Michael Eckmann - Skidmore College - CS 330 - Fall 2006
• A Data type
– defines a set of allowable values – defines the operations on those values
Ch. 6 - Data Types
Michael Eckmann - Skidmore College - CS 330 - Fall 2006
• How well do the types match real-world problems
• User defined types --- example anyone?
• Abstract data types --- example anyone?
• Structured data types
– Arrays, records
• Data types and operations support in hardware vs. software
• 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
Primitive Data Types
Michael Eckmann - Skidmore College - CS 330 - Fall 2006
• Not defined in terms of other types
• Used with type constructors to provide structured types
• Integer types– Signed vs. unsigned– Sign-magnitude vs. Two's complement vs. one's
complement representation of integers.
• Floating point are approximations to decimal numbers. Why only approximations? Let's convert 0.1 to binary.– Fully continuous? – Precision and range.
Primitive Data Types
Michael Eckmann - Skidmore College - CS 330 - Fall 2006
• 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 2006
• How to “fix” problems with floating point?
• Intervals– Interval arithmetic could guarantee that result of all
operations are within the interval– Have a min and max bound on results.
Primitive Data Types
Michael Eckmann - Skidmore College - CS 330 - Fall 2006
• Decimal types
– Most languages we use don't have a decimal type.– Cobol does, so does C#.– BCD– 1 or 2 digits per byte– Why have decimal types if they waste storage?
Primitive Data Types
Michael Eckmann - Skidmore College - CS 330 - Fall 2006
• Decimal types
– Most languages we use don't have a decimal type.– Cobol does, so does C#.– BCD– 1 or 2 digits per byte– 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 2006
• 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 2006
Unicode
Michael Eckmann - Skidmore College - CS 330 - Fall 2006
• http://www.unicode.org/standard/WhatIsUnicode.html
• Has characters in alphabets of many languages
Character strings
Michael Eckmann - Skidmore College - CS 330 - Fall 2006
• Implemented as
– Character array vs. primitive type– Static vs. dynamic length
• C & C++ strings are terminated with a null character. – 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 2006
• 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 2006
• 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 2006
• 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 2006
• 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 2006
Character strings
Michael Eckmann - Skidmore College - CS 330 - Fall 2006
• 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 2006
• 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 2006
• Ordinal types– Range of values are associated with positive integers
• 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 2006
• 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};
• Represented as 0 to 11 typically but could be given programmer specified values in the declaration.
• 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.
• In C++, we can add 1 to an enum
Enumeration
Michael Eckmann - Skidmore College - CS 330 - Fall 2006
• 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 2006
• 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 2006
• 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 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 2006
– 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?
Subrange types
Michael Eckmann - Skidmore College - CS 330 - Fall 2006
• Subrange types
• e.g. In Ada:
• type Days is (Mon, Tue, Wed, Thu, Fri, Sat, Sun);
• subtype Weekdays is Days range Mon..Fri;
• Day1 : Days;
• Day2 : Weekdays;
• Day2 := Day1;
• Only allowed if Day 1 isn't Sat or Sun.
Subrange types
Michael Eckmann - Skidmore College - CS 330 - Fall 2006
• Subrange types
– When do you think they're type checked?
– When do you think they're range checked?
Subrange types
Michael Eckmann - Skidmore College - CS 330 - Fall 2006
• Only current language that supports is Ada 95.
• Subrange types are implemented as their parent types but with additional range checks.
• Advantages / Disadvantages?
Subrange types
Michael Eckmann - Skidmore College - CS 330 - Fall 2006
• Advantages / Disadvantages?
– Increased code size
– Increased execution time due to range checking
– Readability increases and reliability increases