Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


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

1 CS 330 Programming Languages 11 / 08 / 2007 Instructor: Michael Eckmann

2 Michael Eckmann - Skidmore College - CS 330 - Fall 2007 Today’s Topics Questions / comments? Chapter 6 –Data types Arrays Records Pointers

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

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

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

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

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

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

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

10 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) Both are fixed after they're bound

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

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

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

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

15 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

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

17 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 operators on arrays –APL has arithmetic operations on arrays like vector multiplication and matrix transpose, etc. –Python has array concatenation membership testing comparison to determine if vars are referencing the same object (same array) comparison of the corresponding objects in the references to see if arrays contain equal elements –Ruby has similar facilities

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

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

20 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

21 Announcement Michael Eckmann - Skidmore College - CS 330 - Fall 2007 TODAY 5:15 PM Harder 202 "Arithmetic In The Real World" Dr. John Hershey Senior Researcher, GE Global Research Center

22 Comment about the Memory Model Michael Eckmann - Skidmore College - CS 330 - Fall 2007 Code, Static Data, Stack, Heap

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

24 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

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

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

27 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

28 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;

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

30 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.)

31 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

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

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

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

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

36 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++?

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

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.


Download ppt "CS 330 Programming Languages 11 / 08 / 2007 Instructor: Michael Eckmann."

Similar presentations


Ads by Google