Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 330 Programming Languages 11 / 09 / 2006 Instructor: Michael Eckmann.

Similar presentations


Presentation on theme: "CS 330 Programming Languages 11 / 09 / 2006 Instructor: Michael Eckmann."— Presentation transcript:

1 CS 330 Programming Languages 11 / 09 / 2006 Instructor: Michael Eckmann

2 Michael Eckmann - Skidmore College - CS 330 - Fall 2006 Today’s Topics Questions / comments? Chapter 6 –arrays Static Fixed stack-dynamic Stack-dynamic Fixed heap-dynamic Heap-dynamic –multidimensional arrays –associative arrays / hashes –records –pointers

3 Michael Eckmann - Skidmore College - CS 330 - Fall 2006 Coming up next... Scheme and Functional Programming in general is the next topic after we finish chapter 6.

4 Arrays Michael Eckmann - Skidmore College - CS 330 - Fall 2006 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?

5 Arrays Michael Eckmann - Skidmore College - CS 330 - Fall 2006 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?

6 Arrays Michael Eckmann - Skidmore College - CS 330 - Fall 2006 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.

7 Arrays Michael Eckmann - Skidmore College - CS 330 - Fall 2006 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?

8 Arrays Michael Eckmann - Skidmore College - CS 330 - Fall 2006 Lower bound of subscripts –Usually 0 (C-based languages) Fortran95 defaults to 1. –Why use 0?

9 Arrays Michael Eckmann - Skidmore College - CS 330 - Fall 2006 Five categories of arrays are: –static –fixed stack-dynamic –stack-dynamic –fixed heap-dynamic –heap dynamic We'll define each of these in turn and we'll evaluate them amongst each other. If you had to guess what do you think is the definition of a static array (vs. the other kinds above)?

10 Arrays Michael Eckmann - Skidmore College - CS 330 - Fall 2006 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?

11 Arrays Michael Eckmann - Skidmore College - CS 330 - Fall 2006 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.

12 Arrays Michael Eckmann - Skidmore College - CS 330 - Fall 2006 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) Both are fixed after they're bound

13 Arrays Michael Eckmann - Skidmore College - CS 330 - Fall 2006 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?

14 Arrays Michael Eckmann - Skidmore College - CS 330 - Fall 2006 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.

15 Arrays Michael Eckmann - Skidmore College - CS 330 - Fall 2006 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.

16 Arrays Michael Eckmann - Skidmore College - CS 330 - Fall 2006 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) Textbook says C & C++ also use fixed heap-dynamic arrays –But it also says they can be dynamically allocated and the sizes can change over execution time because there is no index range checking. That sounds to me like heap-dynamic. Anyone agree or disagree? Java - fixed heap-dynamic arrays –Their subscripts and allocation are dynamically bound but fixed afterwards. What kind of arrays do you think Perl has?

17 Arrays Michael Eckmann - Skidmore College - CS 330 - Fall 2006 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 --- I had not mentioned these before. –I did mention though that you could do things like: –@arr = (1, 2, 3, 4); –$arr[4] = 5; –Which also obviously lengthens the array dynamically

18 Arrays Michael Eckmann - Skidmore College - CS 330 - Fall 2006 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.

19 Arrays Michael Eckmann - Skidmore College - CS 330 - Fall 2006 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.

20 Arrays Michael Eckmann - Skidmore College - CS 330 - Fall 2006 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? –Evaluation? for/against - jagged/rectangular.

21 Arrays Michael Eckmann - Skidmore College - CS 330 - Fall 2006 Slices –Not a new data type –Instead, it is a way to reference part of an array as a seperate 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? textbook talks about Ada and Fortran 95 supporting slices.

22 Arrays Michael Eckmann - Skidmore College - CS 330 - Fall 2006 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. And if run-time range checking is done in some language, what do you think the descriptor will contain?

23 Multidimensional Arrays Michael Eckmann - Skidmore College - CS 330 - Fall 2006 Design issue: –How to store the multidimensional array in memory? –A 1-D array is typically stored sequentially in memory

24 Multidimensional Arrays Michael Eckmann - Skidmore College - CS 330 - Fall 2006 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?

25 Multidimensional Arrays Michael Eckmann - Skidmore College - CS 330 - Fall 2006 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?

26 Multidimensional Arrays Michael Eckmann - Skidmore College - CS 330 - Fall 2006 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 –Luckily all this info is stored in the desriptor for the array

27 Multidimensional Arrays Michael Eckmann - Skidmore College - CS 330 - Fall 2006 Address of element at row i, column j is = address_of_array + (num_cols * i + j) * size_of_element;

28 Associative Arrays Michael Eckmann - Skidmore College - CS 330 - Fall 2006 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 they 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.

29 Hashing Michael Eckmann - Skidmore College - CS 330 - Fall 2006 A hash table is 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.)

30 Michael Eckmann - Skidmore College - CS 330 - Fall 2006 Records Records are a way to hold different types of data as a group. e.g. A record might hold an employee name, salary, hire-date, etc. The individual elements of a record are called fields. Design issues include: –How are fields referenced in the language –How are fields within nested record types referenced in the language

31 Michael Eckmann - Skidmore College - CS 330 - Fall 2006 Records COBOL has elaborate record structures. A record name is defined at a LEVEL with a low number. Fields are defined at a LEVEL with a higher number. Nested Records are created by having LEVELs in between these. We'll go over an example of COBOL records in the next couple of slides struct defines records in C, C++ and C# What defines records in Java?

32 Michael Eckmann - Skidmore College - CS 330 - Fall 2006 Records COBOL record example. 01 STUDENT-RECORD. 05 STUDENT-NAME. 10 LAST-NAME PICTURE X(20). 10 FIRST-NAME PICTURE X(20). 05 BIRTH-DATE PICTURE 999999. 05 EXPECTED-GRAD PICTURE 9999. Note that STUDENT-NAME is also a RECORD.

33 Michael Eckmann - Skidmore College - CS 330 - Fall 2006 Records COBOL record example. 01 EMPLOYEE-RECORD. 05 EMPLOYEE-NAME. 10 LNAME PICTURE X(20). 10 FNAME PICTURE X(20). 05 BIRTH-DATE PICTURE 999999. 05 SALARY PICTURE 99999V99.

34 Michael Eckmann - Skidmore College - CS 330 - Fall 2006 Records COBOL has a MOVE CORRESPONDING command to copy values of fields of one record to another. It copies values only of fields with the same name in both records. Do you see any benefits or drawbacks to this?

35 Michael Eckmann - Skidmore College - CS 330 - Fall 2006 Records Ada also allows nested levels of records. Do C++ or Java allow nested records? Referencing field names –Fully qualified reference –Elliptical reference LAST-NAME OF STUDENT-RECORD How do we reference fields in Java or C++?

36 Michael Eckmann - Skidmore College - CS 330 - Fall 2006 Records There are some similarities and differences between records and arrays. –Records are used when the data is of different types –Arrays are used when the data is all the same type –Record elements are referenced with static field names as “subscripts.” –Array elements are referenced with dynamic subscripts. Implementation –In the descriptor, the address of the record is stored as well as the name, type and offset address of each field.

37 Michael Eckmann - Skidmore College - CS 330 - Fall 2006 Unions & Records Unions may be decided to be allowed in records or not. –what are reasons for and against allowing Unions in records

38 Michael Eckmann - Skidmore College - CS 330 - Fall 2006 Pointers Pointer types –Store memory addresses or nil (no address.) For indirect addressing For dynamic storage management –Can access data at a particular memory address in the heap Provide a way to have data structures that shrink and grow during execution.

39 Michael Eckmann - Skidmore College - CS 330 - Fall 2006 Pointers Using pointers –Pointer reference (the address) –Indirect reference (aka dereference) gives us what data is in the memory address stored in the pointer Dereferencing is usually explicit (as in C, C++ with the * operator) but it is sometimes implicit (ala Ada) Dereferencing example –variable ptr is a pointer and contains the memory address 7080 –Memory address 7080 contains the integer value 206 –j is a variable of type integer.

40 Michael Eckmann - Skidmore College - CS 330 - Fall 2006 Pointers int j; int *ptr; // code missing // to assign val to ptr j = *ptr; // line above assigns // value in address // that ptr points to

41 Michael Eckmann - Skidmore College - CS 330 - Fall 2006 Pointers Accessing fields of records via pointers to records –C++ (pointer to a struct) –(*ptr).field_name (* dereferences,. accesses field ) –ptr -> field_name ( -> does both ) Because this is a common operation, there's a shorthand –Ada –ptr.field_name (because of implicit dereferencing acts just like -> in C++)

42 Michael Eckmann - Skidmore College - CS 330 - Fall 2006 Pointers Problems –Dangling pointer (or dangling reference in those languages that don't have pointers) –Memory leakage –Did anyone read about these in the text or know what they are from prior knowledge?

43 Michael Eckmann - Skidmore College - CS 330 - Fall 2006 Pointers Problems –Dangling pointer (or dangling reference in those languages that don't have pointers) When a pointer contains the address of a variable that was deallocated already example: –pointer p1 points to some heap dynamic variable –pointer p2 = p1 // make p2 also point there –deallocate p1's heap dynamic variable –// now, p2 is a dangling pointer –If programmer cannot explicitly deallocate heap dynamic variables then there will be no possibility of dangling pointers.

44 Michael Eckmann - Skidmore College - CS 330 - Fall 2006 Pointers Problems –Memory leakage When a pointer's value changes (that is a different address is stored in it) and the address it was pointing to didn't have it's data deallocated Garbage collection (frees deallocated memory for use) –C++ (done explicitly by programmer) –Java (done automagically)

45 Michael Eckmann - Skidmore College - CS 330 - Fall 2006 Pointers Let's look at these problems in Ada, C/C++, Fortran 95, Java Ada –Pointers are of type access –Handles the dangling pointer problem by design Implicit deallocation of memory at end of scope is done Also allows explicit deallocation by the programmer via the deallocator: Unchecked_Deallocation (which can cause the dangling pointer problem) –Memory leakage Nothing by design to prevent this

46 Michael Eckmann - Skidmore College - CS 330 - Fall 2006 Pointers Let's look at these problems in Ada, C/C++, Fortran 95, Java C/C++ –Both the dangling pointer problem and memory leakage exist in these languages –Can do pointer arithmetic –Can have pointers to any type and pointers to functions –One use of pointers is to pass variables by reference (so they may be changed within the function) --- contrast with pass by value, where the variable's value is copied to new temporary space in the function. –& is used to get the address of a variable –Examples of this stuff...

47 Michael Eckmann - Skidmore College - CS 330 - Fall 2006 Pointers C/C++ int *ptr; int count=30, init=70; ptr = &init; count = *ptr; What does this code do?

48 Michael Eckmann - Skidmore College - CS 330 - Fall 2006 Pointers C/C++ int *ptr; // declare a pointer to an int int count=30, init=70; ptr = &init; // store the address of init in ptr count = *ptr; // dereference ptr and store what's in the address // that ptr is pointing to // so what value does count have?

49 Michael Eckmann - Skidmore College - CS 330 - Fall 2006 Pointers C/C++ (pointer arithmetic) int *ptr; // declare a pointer to an int int grades[ 100 ]; ptr = grades; // grades is a “pointer” to grades[0] and now // so is ptr *(ptr + 1) // here the + is pointer addition, so it actually adds // the size of one int to ptr so that it dereferences // (ptr + 1) to grades[1] ptr[index]; // can use ptr like an array. // What is the difference between ptr and grades?

50 Michael Eckmann - Skidmore College - CS 330 - Fall 2006 Pointers Let's look at these problems in Ada, C/C++, Fortran 95, Java Fortran95 –Has dangling pointer problem because allows explicit Deallocation command on a pointer so how does that make dangling pointers possible? –Like Ada, pointers are implicitly dereferenced, but Fortran95 also provides a way to explicitly not dereference a pointer What does this mean again? Why might this be?

51 Michael Eckmann - Skidmore College - CS 330 - Fall 2006 Pointers Let's look at these problems in Ada, C/C++, Fortran 95, Java Java –Anybody know whether we have dangling pointers/references or memory leakage problems in Java?

52 Michael Eckmann - Skidmore College - CS 330 - Fall 2006 Pointers Reference types in C/C++ and Java and C# –C/C++ reference type is a special kind of pointer type Used when declaring parameters to a function so that it is passed by reference Formal parameter is specified with an & But inside the function, it is implicitly dereferenced. –Makes code more readable and safer Can do same thing with regular pointers but code is less readable (b/c explicit dereferencing required) and less safe

53 Michael Eckmann - Skidmore College - CS 330 - Fall 2006 Pointers Reference types in C/C++ and Java and C# –C/C++ reference type is a special kind of pointer type Would there be any use for passing by reference using a constant reference parameter (that is, one that disallows its contents to be changed)? –Java References replace C++'s pointers –Why? Java references refer to class instances (objects), so arithmetic with them doesn't make sense. No dangling references b/c implicit deallocation

54 Michael Eckmann - Skidmore College - CS 330 - Fall 2006 Pointers Reference types in C/C++ and Java and C# –C# Has both Java-like references and C++-like pointers. Best (or worst) of both worlds? Pointers are discouraged --- methods that use pointers need to be modified with the unsafe keyword.

55 Michael Eckmann - Skidmore College - CS 330 - Fall 2006 Pointers Implementation of pointers –Pointers hold an address Solutions to dangling pointer problem –Tombstones A tombstone points to (holds the address of) where the data is (a heap-dynamic variable.) Pointers can only point to a tombstone (which in turn points to the actual data.) What does this solve? When deallocate, the tombstone is set to nil. Why aren't they used do you think? --- Know of any languages that use them?

56 Michael Eckmann - Skidmore College - CS 330 - Fall 2006 Pointers Solutions to dangling pointer problem (continued) –locks-and-keys Pointers and variables need different implementation for this method Pointers are ordered pairs of an integer key and an address. Heap-dynamic variables –include a header cell that stores a lock value –and storage cell(s) for the variable itself During allocation a lock value is calculated and placed in the key portion of the pointer AND the header cell of the variable. Key and header cell are compared when the pointer is dereferenced and if they're the same it's a legal reference otherwise it is an illegal reference (which causes run-time error.)

57 Michael Eckmann - Skidmore College - CS 330 - Fall 2006 Pointers Solutions to dangling pointer problem (continued) –Locks-and-keys (continued) Multiple pointers may point to the variable but they all must have the same key. When variable is deallocated (explicitly), the variable's header cell is changed to an illegal lock value (so no key will match it ever.) –Any other solutions you can think of to handle the dangling pointer problem?

58 Michael Eckmann - Skidmore College - CS 330 - Fall 2006 Pointers Solutions to dangling pointer problem (continued) –Any other solutions you can think of to handle the dangling pointer problem? Don't allow programmer to explicitly deallocate heap- dynamic variables. Like Java and LISP. Also like C#'s references (but not like C#'s pointers.)

59 Michael Eckmann - Skidmore College - CS 330 - Fall 2006 Pointers Solutions to memory leakage problem –Reference counters Store a reference counter for each memory cell whose value is the number of pointers currently pointing to it. When pointers change value or are destroyed, the counter is decremented. It is also checked to see if it is zero. If it is, then the memory can be reclaimed. –Any drawbacks to this method?

60 Michael Eckmann - Skidmore College - CS 330 - Fall 2006 Pointers Solutions to memory leakage problem –Reference counters (continued) Any drawbacks to this method? –Space –Time –Circular references

61 Michael Eckmann - Skidmore College - CS 330 - Fall 2006 Pointers Solutions to memory leakage problem (continued) –Garbage collection Every heap cell needs a bit or possibly a field as a flag to indicate whether or not it contains garbage. When want to reclaim memory the garbage collector will –1. Set all the flags as “containing garbage” –2. Go through the program and determine all the memory that is being pointed to --- and changes the flags for those cells as “not containing garbage” –3. All the ones still marked as “containing garbage” are reclaimed. –All preceding discussion assumed that the heap cells were all the same size. Major difficulties arise when non-uniform size cells are used. Why?


Download ppt "CS 330 Programming Languages 11 / 09 / 2006 Instructor: Michael Eckmann."

Similar presentations


Ads by Google