Top Banner
CS 330 Programming Languages 10 / 30 / 2007 Instructor: Michael Eckmann
41
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: CS 330 Programming Languages 10 / 30 / 2007 Instructor: Michael Eckmann.

CS 330Programming Languages

10 / 30 / 2007

Instructor: Michael Eckmann

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

Michael Eckmann - Skidmore College - CS 330 - Fall 2007

Today’s Topics• Questions / comments?• Midterm grading explanation• Chapter 5

– storage bindings and lifetime– type checking– scope / lifetime

• Chapter 6– Data types

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

Michael Eckmann - Skidmore College - CS 330 - Fall 2007

0-4.0 scale grade coversion• Your exam was graded on a curve.• The total points: 90

• The mean: 73.56 (highest and lowest discarded)

• The standard deviation: 10.35

• Low: 37

• High: 88

• Your RawScore is the number of points you earned out of 90 (the numerator on the front of your test.)

(RawScore - 73.56) ECMin(4.33, 3.0 + --------------------- + ------ ) 10.35 90

- can't get higher than 4.33.

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

Michael Eckmann - Skidmore College - CS 330 - Fall 2007

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+

Page 5: CS 330 Programming Languages 10 / 30 / 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 6: CS 330 Programming Languages 10 / 30 / 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 7: CS 330 Programming Languages 10 / 30 / 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 8: CS 330 Programming Languages 10 / 30 / 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 9: CS 330 Programming Languages 10 / 30 / 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.

• Stack-dynamic variables

• What are some advantages and disadvantages?

Page 10: CS 330 Programming Languages 10 / 30 / 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.

• 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 11: CS 330 Programming Languages 10 / 30 / 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.

• Explicit heap-dynamic variables (e.g. Java references to objects)

• What are some advantages and disadvantages?

Page 12: CS 330 Programming Languages 10 / 30 / 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.

• 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 13: CS 330 Programming Languages 10 / 30 / 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.

• Implicit heap-dynamic variables

• What are some advantages and disadvantages?

Page 14: CS 330 Programming Languages 10 / 30 / 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.

• 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 15: CS 330 Programming Languages 10 / 30 / 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 16: CS 330 Programming Languages 10 / 30 / 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 17: CS 330 Programming Languages 10 / 30 / 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 18: CS 330 Programming Languages 10 / 30 / 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 19: CS 330 Programming Languages 10 / 30 / 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 20: CS 330 Programming Languages 10 / 30 / 2007 Instructor: Michael Eckmann.

Scope

Michael Eckmann - Skidmore College - CS 330 - Fall 2007

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

}

Page 21: CS 330 Programming Languages 10 / 30 / 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 22: CS 330 Programming Languages 10 / 30 / 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 23: CS 330 Programming Languages 10 / 30 / 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.)

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

Ch. 6 - Data Types

Michael Eckmann - Skidmore College - CS 330 - Fall 2007

• A Data type

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

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

Ch. 6 - Data Types

Michael Eckmann - Skidmore College - CS 330 - Fall 2007

• 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

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

Primitive Data Types

Michael Eckmann - Skidmore College - CS 330 - Fall 2007

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

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

Primitive Data Types

Michael Eckmann - Skidmore College - CS 330 - Fall 2007

• 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

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

Primitive Data Types

Michael Eckmann - Skidmore College - CS 330 - Fall 2007

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

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

Primitive Data Types

Michael Eckmann - Skidmore College - CS 330 - Fall 2007

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

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

Primitive Data Types

Michael Eckmann - Skidmore College - CS 330 - Fall 2007

• 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

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

Primitive Data Types

Michael Eckmann - Skidmore College - CS 330 - Fall 2007

• 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

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

ASCII

Michael Eckmann - Skidmore College - CS 330 - Fall 2007

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

Unicode

Michael Eckmann - Skidmore College - CS 330 - Fall 2007

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

• Has characters in alphabets of many languages

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

Character strings

Michael Eckmann - Skidmore College - CS 330 - Fall 2007

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

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

Character strings

Michael Eckmann - Skidmore College - CS 330 - Fall 2007

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

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

Character strings

Michael Eckmann - Skidmore College - CS 330 - Fall 2007

• Pattern matching with character strings

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

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

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

Character strings

Michael Eckmann - Skidmore College - CS 330 - Fall 2007

• Length

– Static length– Limited dynamic length– Dynamic length

• Requires dynamic storage allocation and deallocation

• Maximum flexibility

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

Character strings

Michael Eckmann - Skidmore College - CS 330 - Fall 2007

• 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

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

Character stringsDescriptor for static strings

Michael Eckmann - Skidmore College - CS 330 - Fall 2007

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

Character strings

Michael Eckmann - Skidmore College - CS 330 - Fall 2007

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

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

Character strings

Michael Eckmann - Skidmore College - CS 330 - Fall 2007

• 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