Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 363 – Chapter 8 Composite data types Records

Similar presentations


Presentation on theme: "CS 363 – Chapter 8 Composite data types Records"— Presentation transcript:

1 CS 363 – Chapter 8 Composite data types Records
Arrays, including string List Set Pointer

2 Records A data type consisting of fields
Fields may be of different types, even records Fields usually aligned on word boundary Unless “packed” Compiler may rearrange to save space 

3 Examples type person = record name : char[30]; salary: int;
birthdate: date; phone: char[7]; date = record month, day, year : int; type plane = record flight: int; status : (air, ground, terminal); altitude: int; heading: int; runway: string; airport: string: boarding: boolean; departure: int;

4 Variant record We can save space if we know some fields only make sense at certain times. Every time we use a variant field, we must check the value of “status” or else info is corrupted. type plane = record flight: int; case status : (air, ground, terminal) of air: altitude: int; heading: int; ground: runway: string; airport: string: terminal: boarding: boolean; departure: int;

5 Example In C, a variant record is called a “union”.
What happens when we break the rules? union union_type { float float_part; // no field to say which! int int_part; } u; u.float_part = 3.14; printf("%d\n", u.int_part);

6 Arrays Notation Memory allocation Dynamic arrays Address calculation

7 Using arrays Origin is subscript notation, e.g. a2
Subscript enclosed in [ ] or ( ) Can have more than one dimension Multi-dimensional, e.g. int a[8][10] Array of arrays considered distinct in Ada a: array(1..10) of array(1..10) of integer; b: array(1..10, 1..10) of integer; Ability to reference a row by itself

8 Array Slice In most PL, the only slice allowed is one row, page, etc. of an array Fortran 90 Slice can be contiguous or not Analogous to selecting ranges in Excel  Slices of same shape are compatible

9 Allocating memory Static / global – compiler knows size, and no need to destroy the array during run In Java, we don’t allocate space until run-time. Arrays stored on heap even though they don’t change size. Static / local – compiler knows size, but shouldn’t be global because we need multiple instances of the array In Ada, if we don’t know size of array, but know that it’s a constant, we can have a pointer in activation record… (p. 354) Dynamic – array size changes during execution! Store on heap.

10 In Java What is happening in this code? int [ ] a; a = new int [10];
... a = new int [20]; In Java, this is not considered “dynamic” like ArrayList. In most languages, once you declare a static array, you are stuck with it.

11 C examples It’s better to change declaration to: However,
char *s = "water"; // point to str const s[2] = 'i'; // illegal It’s better to change declaration to: char s[] = "water"; // compiler can count  However, char s[4] = "water"; // dumb char s[5] = "water"; // dangerous Globally declared arrays automatically 0 – saves trouble of initializing.

12 Pointer and array In C & C++ we can use pointer notation
*a means a[ 0 ] *(a + n) means a[ n ] **a means a[ 0 ][ 0 ] What are the pointer expressions for a[ i ][ j ], a[ i ][ 0 ], a[ 0 ][ j ] ?

13 Jagged array Rows can have varying lengths
char *s[] = { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday" } This is an array of 5 pointers Each pointer points to start of a string.

14 Dynamic In C, we typically call an OS function to dynamically allocate memory for array. Use: calloc ( number of elements, size of each ) int *a = calloc(10, 4); int *a = calloc(10, sizeof(int)); // portable  Two dimensions ? First we need an array of pointers. Then, each pointer for each row. int **a = calloc(10, sizeof(int *)); for (i = 0; i < 10; ++i) a[i] = calloc(20, sizeof(int));

15 &a[i] = &a[0] + i * element_size
Array address Memory address of any array element is base address + offset Base address is usually known need to compute offset! For 1-D arrays, pretty simple: &a[i] = &a[0] + i * element_size Isn’t it nice that each element is same size  What do we do if first element is not # 0 ?

16 Multi-dimension Row major Column major 1 2 3 4 5 6 7 8 9 10 11 12 13
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 5 10 15 20 1 6 11 16 21 2 7 12 17 22 3 8 13 18 23 4 9 14 19 24

17 Array address calculation
Sets & pointers

18 Comparison Row major is like an odometer… column major is odometer with digits backwards. Row major: Column major: [0][0][0] [0][0][0] [0][0][1] [1][0][0] [0][0][9] [9][0][0] [0][1][0] [0][1][0] [0][9][9] [9][9][0] [1][0][0] [0][0][1]

19 Addr calc example To generate correct code, compiler needs method to figure address of any array element. int a[5][4][8][10]; Where is a[3][1][5][7] ? Need to know Base address of array Offset: distance from (0,0,0,0) to (3,1,5,7).

20 Row major 2-D Suppose we want &a[2][3] in a 7x5 array.
Number of rows in entire array is irrelevant. Offset Need to travel 2 entire rows down to go from row 0 to row 2. Once on row 2, travel 3 cells to the right. 2 rows + 3 cells = 2 * 5 + 3 General formula?

21 Row major 3-D Suppose we want &a[2][1][4] in a 5x4x6 array.
(We need # rows & columns but not # pages.) Travel 2 pages  page 2 Travel 1 row  row 1 Travel 4 cells  cell 4 Offset = 2 * (size of page) * (size of row) + 4 * (size of cell)

22 Row major – a[3][1][5][7] in 5 books, 4 pages, 8 rows, 10 columns

23 Row major review Array Element Offset 7x5 [2][3] 2 * 5 + 3 5x4x6
[2][1][4] 2 * (4 * 6) + 1 * (6) + 4 5x4x8x10 [3][1][5][7] 3 * (4 * 8 * 10) + 1 * (8 * 10) + 5 * (10) + 7 Don’t forget, we still need to multiply by element size, and add base address.

24 Column major 2-D Suppose we want &a[2][3] in a 7x5 array.
Number of columns in entire array is irrelevant. Offset Need to travel 3 entire columns right to go from col 0 to col 3. Once on col 3, travel 2 cells down. 3 cols + 2 cells = 3 * 7 + 2 General formula?

25 Column major 3-D Suppose we want &a[2][1][4] in a 5x4x6 array.
(We need # pages & rows but not # columns!) Travel 4 cols  col 4 Travel 1 row  row 1 Travel 2 pages  page 2 Offset = 4 * (pages * rows) * (pages) + 2

26 Let’s go to &a[3][1][5][7] in 5 books, 4 pages, 8 rows, 10 columns!

27 Column major – First, we travel the 7 columns to reach column 7.

28 Column major – Second, we travel 5 rows to row 5.

29 Column major – Third, we travel 1 page to page 1.

30 Column major – Finally, we travel 3 books to get to book 3.

31 Column major review Array Element Offset 7x5 [2][3] 3 * 7 + 2 5x4x6
[2][1][4] 4 * (5 * 4) + 1 * (5) + 5x4x8x10 [3][1][5][7] 7 * (5 * 4 * 8) + 5 * (5 * 4) + 3 Don’t forget, we still need to multiply by element size, and add base address.

32 Last look at data types List Set Pointer

33 List type Linear data structure; dynamic size
Usually not part of language itself, e.g. Java LISP language List is a fundamental data type basic operations: car, cdr and cons (set 'text '(many happy returns)) (car text) returns many (cdr text) returns (happy returns) (cons 'too text) returns (too many happy returns)

34 Set type Like a list but no duplicates & order doesn’t matter Pascal
We just want to know if some element is in the set. One way to do it: use one bit per possible element. Pascal Can declare “set of <any type>” Use + * and – operators on sets. C, C++ We can use integer for same purpose and use bitwise operators & | ^ ~ Requires more work to have arbitrary number of bits. 

35 Pointer type A variable that stores the address (reference) of another variable. Used for assignments & accessing data, especially when we point to something BIG like a record. Useful to pass as parameter to function (chapter 8) Need special default value (e.g. 0) Usually, we point to something on the heap.

36 Pointer to primitive Dynamic memory allocation returns pointer to “new” data structure. int *p = new int; // p is pointer to int int value = *p; p is address of int, *p is value of int. So: p == &value, and value == *p int *a = new int[40]; // dynamic array As we saw before, a[0] == &a, etc.

37 Pointer to record A record (struct in C/C++) has fields… we can use pointer to access field: struct employee_type employee; struct employee_type *p; p = & employee; initialize(p); // initialize record w/pointer The following statements are equivalent: money = employee.salary; money = (*p).salary; De-referencing and field access are so common, we have special operator  money = p->salary;

38 Reference type Similar to pointer
Always in “de-reference” mode, so you don’t need to precede with *. Not concerned with address contained. In C++… Used primarily for parameter passing Allows a function to change param value

39 Must allocate space! Common mistake – declare pointer or reference and assume that space for new object also created. Similar mistake: setting pointer equal, creating an alias You’ve seen this in Java too!: Room office = new Room (16, 14, 10); Room kitchen = office; office.setHeight(20);

40 Behind the scenes We usually point to objects on the heap.
Periodically we need to perform garbage collection: (lazy algorithm) Mark all heap objects as garbage Trace all pointers. Anything reachable marked as not garbage. Any garbage left over is de-allocated. Another method – reference counting (eager algorithm) For each heap object, keep track of how many pointers point to it. When reaches 0, de-allocate it.

41 Dangling pointer If we do garbage collection ourselves, problem of de-allocating pointer too early! In C++, a class can have a destructor. Run-time system should detect if a pointer points to space that has been de-allocated. Here are 2 strategies: Tombstones: each heap object has an agent that we can point to. When the heap object is de-allocated, the tombstone gets set to null. Lock & key: the pointer type has a 2nd field representing a key. The key gets invalidated when the heap object is de-allocated.


Download ppt "CS 363 – Chapter 8 Composite data types Records"

Similar presentations


Ads by Google