CS 363 – Chapter 8 Composite data types Records

Slides:



Advertisements
Similar presentations
Introduction to Memory Management. 2 General Structure of Run-Time Memory.
Advertisements

Chapter 7:: Data Types Programming Language Pragmatics
Various languages….  Could affect performance  Could affect reliability  Could affect language choice.
Kernighan/Ritchie: Kelley/Pohl:
Chapter 10 Introduction to Arrays
Chapter 8 Runtime Support. How program structures are implemented in a computer memory? The evolution of programming language design has led to the creation.
Chapter 6 Structured Data Types Arrays Records. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Definitions data type –collection of data objects.
ISBN Chapter 6 Data Types: Structured types.
1 CS 201 Passing Function as Parameter & Array Debzani Deb.
Pointers Applications
Arrays and Pointers in C Alan L. Cox
Type Checking and Data Type Implementation (Sections )
17. ADVANCED USES OF POINTERS. Dynamic Storage Allocation Many programs require dynamic storage allocation: the ability to allocate storage as needed.
Copyright © 2002, Systems and Computer Engineering, Carleton University a-JavaReview.ppt * Object-Oriented Software Development Unit.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Computer Science and Software Engineering University of Wisconsin - Platteville 2. Pointer Yan Shi CS/SE2630 Lecture Notes.
Arrays An array is a data structure that consists of an ordered collection of similar items (where “similar items” means items of the same type.) An array.
Basic Semantics Associating meaning with language entities.
1 Records Record aggregate of data elements –Possibly heterogeneous –Elements/slots are identified by names –Elements in same fixed order in all records.
Pointers: Basics. 2 What is a pointer? First of all, it is a variable, just like other variables you studied  So it has type, storage etc. Difference:
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
ISBN Chapter 6 Data Types Pointer Types Reference Types Memory Management.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department Lecture 2 – August 23, 2001.
MORE POINTERS Plus: Memory Allocation Heap versus Stack.
Chapter 1: Preliminaries Lecture # 2. Chapter 1: Preliminaries Reasons for Studying Concepts of Programming Languages Programming Domains Language Evaluation.
CSI 3125, Data Types, page 1 Data types Outline Primitive data types Structured data types Strings Enumerated types Arrays Records Pointers Reading assignment.
Data Types Chapter 6: Data Types Lectures # 13. Topics Chapter 6: Data Types 2 Introduction Primitive Data Types Character String Types Array Types Associative.
CS314 – Section 5 Recitation 9
Dynamic Allocation in C
Object Lifetime and Pointers
Dynamic Storage Allocation
EGR 2261 Unit 11 Pointers and Dynamic Variables
Data Types In Text: Chapter 6.
Overview 4 major memory segments Key differences from Java stack
Chapter 6 – Data Types CSCE 343.
Type Checking Generalizes the concept of operands and operators to include subprograms and assignments Type checking is the activity of ensuring that the.
Expressions and Assignment
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
Motivation and Overview
Programming Languages and Paradigms
Run-time organization
Concepts of programming languages
Programming Paradigms
JavaScript: Functions.
Java Review: Reference Types
Memory and Addresses Memory is just a sequence of byte-sized storage devices. The bytes are assigned numeric addresses, starting with zero, just like the.
CSCE 210 Data Structures and Algorithms
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
Complex Data Types One very important measure of the “goodness” of a PL is the capability of its data types to model the problem space variables Design.
Object Oriented Programming COP3330 / CGS5409
Overview 4 major memory segments Key differences from Java stack
Data.
Chapter 15 Pointers, Dynamic Data, and Reference Types
7 Arrays.
Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes
CSC 533: Programming Languages Spring 2015
Arrays and Pointers Reference: Chapter , 4.11 CMSC 202.
Memory Allocation CS 217.
Lecture 18 Arrays and Pointer Arithmetic
Dynamic Memory Allocation (and Multi-Dimensional Arrays)
Arrays.
7 Arrays.
Overloading functions
Course Overview PART I: overview material PART II: inside a compiler
CSC 533: Programming Languages Spring 2018
CSC 533: Programming Languages Spring 2019
Classes and Objects Object Creation
Lecture 7: Types (Revised based on the Tucker’s slides) 10/4/2019
SPL – PS2 C++ Memory Handling.
Presentation transcript:

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

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 

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;

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;

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

Arrays Notation Memory allocation Dynamic arrays Address calculation

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

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

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.

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.

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.

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

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.

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

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

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

Array address calculation Sets & pointers

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

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

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?

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) + 1 * (size of row) + 4 * (size of cell)

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

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.

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?

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) + 1 * (pages) + 2

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

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

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

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

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

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.

Last look at data types List Set Pointer

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)

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

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.

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.

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;

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

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

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.

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.