CS 330 Programming Languages 11 / 06 / 2007 Instructor: Michael Eckmann.

44
CS 330 Programming Languages 11 / 06 / 2007 Instructor: Michael Eckmann
  • date post

    19-Dec-2015
  • Category

    Documents

  • view

    219
  • download

    0

Transcript of CS 330 Programming Languages 11 / 06 / 2007 Instructor: Michael Eckmann.

Page 1: CS 330 Programming Languages 11 / 06 / 2007 Instructor: Michael Eckmann.

CS 330Programming Languages

11 / 06 / 2007

Instructor: Michael Eckmann

Page 2: CS 330 Programming Languages 11 / 06 / 2007 Instructor: Michael Eckmann.

Michael Eckmann - Skidmore College - CS 330 - Fall 2007

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

– Data types• Ordinal & Enumeration types

Page 3: CS 330 Programming Languages 11 / 06 / 2007 Instructor: Michael Eckmann.

Ordinal / Enumeration

Michael Eckmann - Skidmore College - CS 330 - Fall 2007

• 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 (how so?)– Range of values intentionally limited.

Page 4: CS 330 Programming Languages 11 / 06 / 2007 Instructor: Michael Eckmann.

Enumeration

Michael Eckmann - Skidmore College - CS 330 - Fall 2007

• 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

Page 5: CS 330 Programming Languages 11 / 06 / 2007 Instructor: Michael Eckmann.

Enumeration

Michael Eckmann - Skidmore College - CS 330 - Fall 2007

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

Page 6: CS 330 Programming Languages 11 / 06 / 2007 Instructor: Michael Eckmann.

Enumeration

Michael Eckmann - Skidmore College - CS 330 - Fall 2007

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

Page 7: CS 330 Programming Languages 11 / 06 / 2007 Instructor: Michael Eckmann.

Enumeration

Michael Eckmann - Skidmore College - CS 330 - Fall 2007

• 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

Page 8: CS 330 Programming Languages 11 / 06 / 2007 Instructor: Michael Eckmann.

Enumeration

Michael Eckmann - Skidmore College - CS 330 - Fall 2007

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

Page 9: CS 330 Programming Languages 11 / 06 / 2007 Instructor: Michael Eckmann.

Subrange types

Michael Eckmann - Skidmore College - CS 330 - Fall 2007

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

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

Subrange types

Michael Eckmann - Skidmore College - CS 330 - Fall 2007

• Compiler generates range-checking code for every assignment to a subrange var.

– When do you think they're type checked?

– When do you think they're range checked?

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

Subrange types

Michael Eckmann - Skidmore College - CS 330 - Fall 2007

• Compiler generates range-checking code for every assignment to a subrange var.

– When do you think they're type checked?• Types are checked for compatibility at compile-time.

– When do you think they're range checked?• runtime

Page 12: CS 330 Programming Languages 11 / 06 / 2007 Instructor: Michael Eckmann.

Subrange types

Michael Eckmann - Skidmore College - CS 330 - Fall 2007

• Only current language that supports is Ada 95.

• Subrange types are implemented as their parent types but with additional range checks.

• Advantages / Disadvantages?

Page 13: CS 330 Programming Languages 11 / 06 / 2007 Instructor: Michael Eckmann.

Subrange types

Michael Eckmann - Skidmore College - CS 330 - Fall 2007

• Advantages / Disadvantages?

– Increased code size

– Increased execution time due to range checking

– Readability increases and reliability increases

Page 14: CS 330 Programming Languages 11 / 06 / 2007 Instructor: Michael Eckmann.

Michael Eckmann - Skidmore College - CS 330 - Fall 2007

Unions• A Union is a type that can store one of different type values

during the execution of a program. Why?

• Design issues

– Type checking• Free

– No type checking• Discriminated

– Has type checking

– Are unions allowed in records?

Page 15: CS 330 Programming Languages 11 / 06 / 2007 Instructor: Michael Eckmann.

Michael Eckmann - Skidmore College - CS 330 - Fall 2007

Unions• A Union is a type that can store one of different type values

during the execution of a program. Why?

• Design issues

– Type checking

• Free (Fortran, C and C++ have this type)

– No type checking

• Discriminated (Ada has this type)

– Has type checking

• Java and C# (the newer languages) do not have unions.

Page 16: CS 330 Programming Languages 11 / 06 / 2007 Instructor: Michael Eckmann.

Michael Eckmann - Skidmore College - CS 330 - Fall 2007

Unions• An example in C++.

union Number {

int x;

float y;

}

// in some function somewhere we can:

Number num;

num.x = 100;

// then we print the contents of num.x and num.y

num.y = 25.4;

// then we print the contents of num.x and num.y

• note: this example is a modified version from Deitel & Deitel's C++ How to Program.

Page 17: CS 330 Programming Languages 11 / 06 / 2007 Instructor: Michael Eckmann.

Michael Eckmann - Skidmore College - CS 330 - Fall 2007

Unions• Discriminated union example of memory storage

Page 18: CS 330 Programming Languages 11 / 06 / 2007 Instructor: Michael Eckmann.

Michael Eckmann - Skidmore College - CS 330 - Fall 2007

Unions• Evaluation of Unions.

Page 19: CS 330 Programming Languages 11 / 06 / 2007 Instructor: Michael Eckmann.

Arrays

Michael Eckmann - Skidmore College - CS 330 - Fall 2007

• We all know what arrays are.

• Design issues

– Legal types for subscripts

– Are references to subscript expressions range checked?

– When are subscript ranges bound?

– When is allocation bound?

– Are ragged and multidimensional arrays allowed?

– Is initialization allowed at allocation time?

– Slices?

Page 20: CS 330 Programming Languages 11 / 06 / 2007 Instructor: Michael Eckmann.

Arrays

Michael Eckmann - Skidmore College - CS 330 - Fall 2007

• Most languages use the name of the array followed by an index in square brackets to specify an element of an array

– e.g. array_of_names[5]

• Ada uses parentheses surrounding the index. Ada also uses parentheses for subprogram (function) calls to enclose the parameters. Any idea why they might have done that? Is it a good idea?

Page 21: CS 330 Programming Languages 11 / 06 / 2007 Instructor: Michael Eckmann.

Arrays

Michael Eckmann - Skidmore College - CS 330 - Fall 2007

• Ada chose to use parentheses in these two ways because both array references and function calls are similar in that they map the index (or parameters) to a value which is returned.

• It certainly reduces readability to some degree because the reader of the program can't tell the difference between an array reference and a function call with one parameter.

Page 22: CS 330 Programming Languages 11 / 06 / 2007 Instructor: Michael Eckmann.

Arrays

Michael Eckmann - Skidmore College - CS 330 - Fall 2007

• There are two types associated with any array

– The type of the elements of the array

– The type of the subscript

• Most commonly integers (or subranges of integers)

• Ada allows boolean, characters and enumeration types as subscripts (how might they do that?)

• Range checking the subscripts (indices) When are we talking here?

– C, C++, Perl, and Fortran --- no range checking

– Java, ML, C# do range check

– Ada range checks by default, but can be programmer disabled

– Why not range check?

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

Arrays

Michael Eckmann - Skidmore College - CS 330 - Fall 2007

• Lower bound of subscripts

– Usually 0 (C-based languages) Fortran95 defaults to 1.

– Why use 0?

Page 24: CS 330 Programming Languages 11 / 06 / 2007 Instructor: Michael Eckmann.

Arrays

Michael Eckmann - Skidmore College - CS 330 - Fall 2007

• Five categories of arrays

– Static – subscript ranges and storage allocation is static. Statically bound variables are bound BEFORE run-time.

– Fixed stack-dynamic – subscript ranges statically bound, but allocation done at declaration elaboration time. When is declaration elaboration time?

– Let's compare these two. What are the advantages of each?

Page 25: CS 330 Programming Languages 11 / 06 / 2007 Instructor: Michael Eckmann.

Arrays

Michael Eckmann - Skidmore College - CS 330 - Fall 2007

• Static vs. Fixed stack-dynamic

– Let's compare these two. What are the advantages of each?

– Static --- speed efficient --- no dynamic allocation or deallocation (at runtime) so they're faster.

– Fixed stack-dynamic --- space efficient --- if have block scoped arrays, they're only allocated when they need to be in memory instead of allocating all of them before run-time.

Page 26: CS 330 Programming Languages 11 / 06 / 2007 Instructor: Michael Eckmann.

Arrays

Michael Eckmann - Skidmore College - CS 330 - Fall 2007

• Back to the five categories of arrays

– Besides static and fixed stack-dynamic

– Stack-dynamic• Subscript ranges dynamically bound

• Storage allocation is dynamic (on the stack)

Page 27: CS 330 Programming Languages 11 / 06 / 2007 Instructor: Michael Eckmann.

Arrays

Michael Eckmann - Skidmore College - CS 330 - Fall 2007

• Compare Stack-dynamic vs. static and fixed stack-dynamic

– Stack-dynamic• Subscript ranges dynamically bound

• Storage allocation is dynamic (on the stack)

• Both are fixed after they're bound

• What advantage does this have over static and fixed stack-dynamic?

Page 28: CS 330 Programming Languages 11 / 06 / 2007 Instructor: Michael Eckmann.

Arrays

Michael Eckmann - Skidmore College - CS 330 - Fall 2007

• Static and fixed stack-dynamic VS.

• Stack-dynamic– Size of the array doesn't need to be known until it is going to be

used.

Page 29: CS 330 Programming Languages 11 / 06 / 2007 Instructor: Michael Eckmann.

Arrays

Michael Eckmann - Skidmore College - CS 330 - Fall 2007

• Back to the five categories of arrays

– Besides static and fixed stack-dynamic and stack-dynamic

– Fixed heap-dynamic• Subscript ranges dynamically bound

• Storage allocation is dynamic (on the heap)

• Both are fixed after they're bound which is different from:

– Heap-dynamic• Subscript ranges dynamically bound

• Storage allocation is dynamic (on the heap)

• Both are changeable during execution.

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

Arrays

Michael Eckmann - Skidmore College - CS 330 - Fall 2007

• In functions (therefore block scope)

– C & C++ use static arrays if programmer uses the static modifier

– C & C++ use fixed stack-dynamic arrays by default (without the static modifier)

• C & C++ also use fixed heap-dynamic arrays

– when use malloc/free in C and new/delete in C++ • Java - fixed heap-dynamic arrays

– Their subscripts and allocation are dynamically bound but fixed afterwards.

• What kind of arrays do you think Perl has?

Page 31: CS 330 Programming Languages 11 / 06 / 2007 Instructor: Michael Eckmann.

Arrays

Michael Eckmann - Skidmore College - CS 330 - Fall 2007

• Perl and Javascript (and C# has capability)

– Heap-dynamic arrays

– Perl has push and pop functions that treat arrays like a stack therefore changing the length

– you could also do things like:

– @arr = (1, 2, 3, 4);

– $arr[4] = 5;

– Which also obviously lengthens the array dynamically

Page 32: CS 330 Programming Languages 11 / 06 / 2007 Instructor: Michael Eckmann.

Arrays

Michael Eckmann - Skidmore College - CS 330 - Fall 2007

• Array initialization– Sometimes the initialization implicitly states the length

– In C, C++, C# and Java this happens

– Ada has an interesting feature that allows initialization of some elements of the array to certain values and all others to another value.

Page 33: CS 330 Programming Languages 11 / 06 / 2007 Instructor: Michael Eckmann.

Arrays

Michael Eckmann - Skidmore College - CS 330 - Fall 2007

• Array operations (operate on array as a whole)

– Some languages allow concatenation of arrays and can use relational operaters on arrays

– Others like APL have arithmetic operations on arrays like vector multiplication and matrix transpose, etc.

Page 34: CS 330 Programming Languages 11 / 06 / 2007 Instructor: Michael Eckmann.

Arrays

Michael Eckmann - Skidmore College - CS 330 - Fall 2007

• Rectangular vs. jagged arrays– Only makes sense when talking about multidimensional arrays

– Jagged if rows can have different numbers of columns in the same array

– I made can bold above, because Java, C and C++ support jagged arrays but do not support rectangular arrays.

– This is because multidimensional arrays are implemented as arrays of arrays.

– Fortran, Ada and C# provide rectangular arrays

– When are different numbers of columns for a row useful?

Page 35: CS 330 Programming Languages 11 / 06 / 2007 Instructor: Michael Eckmann.

Arrays

Michael Eckmann - Skidmore College - CS 330 - Fall 2007

• Slices

– Not a new data type

– Instead, it is a way to reference part of an array as a separate unit.

– e.g. One row, or some subset of consecutive elements in a column, or some rectangular subset of rows etc.

– Anyone know any languages that support slices?

Page 36: CS 330 Programming Languages 11 / 06 / 2007 Instructor: Michael Eckmann.

Arrays

Michael Eckmann - Skidmore College - CS 330 - Fall 2007

• Slices

– Not a new data type

– Instead, it is a way to reference part of an array as a separate unit.

– e.g. One row, or some subset of consecutive elements in a column, or some rectangular subset of rows etc.

– Anyone know any languages that support slices?• Fortran 95, Python, Ruby

Page 37: CS 330 Programming Languages 11 / 06 / 2007 Instructor: Michael Eckmann.

Arrays

Michael Eckmann - Skidmore College - CS 330 - Fall 2007

• Implementation of array types

– How to access array elements• Code needs to produce element addresses

– Descriptor for arrays• Needs to have information in it to construct the access

function

• That is, when an element of an array is referenced, the index along with the information in the descriptor needs to be able to find the address in memory where that element lives.

• So if run-time range checking is done in some language, what do you think the descriptor will contain?

Page 38: CS 330 Programming Languages 11 / 06 / 2007 Instructor: Michael Eckmann.

Multidimensional Arrays

Michael Eckmann - Skidmore College - CS 330 - Fall 2007

• Design issue:

– How to store the multidimensional array in memory?

– A 1-D array is typically stored sequentially in memory

Page 39: CS 330 Programming Languages 11 / 06 / 2007 Instructor: Michael Eckmann.

Multidimensional Arrays

Michael Eckmann - Skidmore College - CS 330 - Fall 2007

• Design issue:

– How to store the multidimensional array in memory?

– Row major order

– vs.

– Column major order

– Fortran uses column, all others use row

• Would the programmer care whether the multidimensional array is stored in row or column major order?

Page 40: CS 330 Programming Languages 11 / 06 / 2007 Instructor: Michael Eckmann.

Multidimensional Arrays

Michael Eckmann - Skidmore College - CS 330 - Fall 2007

• How to access 2-dimensional array elements at run-time (assuming row major order and all rows have same number of columns and index starts at 0).

– We have the subscripts to look for

– We need address of first element

– What else do we need?

Page 41: CS 330 Programming Languages 11 / 06 / 2007 Instructor: Michael Eckmann.

Multidimensional Arrays

Michael Eckmann - Skidmore College - CS 330 - Fall 2007

• How to access 2-dimensional array elements at run-time (assuming row major order and all rows have same number of columns and index starts at 0).

– We have the subscripts to look for

– We need address of first element

– We need the size of one element (determined from the type of the array.)

– We need to know the number of columns per row

– all this info is stored in the descriptor for the array

Page 42: CS 330 Programming Languages 11 / 06 / 2007 Instructor: Michael Eckmann.

Multidimensional Arrays

Michael Eckmann - Skidmore College - CS 330 - Fall 2007

• Address of element at row i, column j is =

address_of_array +

(num_cols * i + j) * size_of_element;

Page 43: CS 330 Programming Languages 11 / 06 / 2007 Instructor: Michael Eckmann.

Associative Arrays

Michael Eckmann - Skidmore College - CS 330 - Fall 2007

• Unordered collection of data indexed by keys.

• Each element of an associative array is a pair:

– key, value

• The reason Perl's associative arrays are called hashes is because there are implemented using a hash table and hash function.

• Because the data is unordered, Perl can store it in memory in any order it cares to.

Page 44: CS 330 Programming Languages 11 / 06 / 2007 Instructor: Michael Eckmann.

Hashing

Michael Eckmann - Skidmore College - CS 330 - Fall 2007

• A hash table can be implemented as a 1d array of linked lists.

• Each key is supplied to a hash function which returns a number.

• The index to the array is determined by the number returned from the hash function mod array_length.

• Then go sequentially through the linked list in that element of the array to find the value.

• The length of the array is chosen wisely (usually a prime number so the mod works well) and the hash function is chosen wisely (to distribute the keys well throughout the hash table.)