CSC2/454 Programming Languages Design and Implementation Records and Arrays

Similar documents
Types II. Hwansoo Han

Chapter 8 :: Composite Types

CSCI312 Principles of Programming Languages!

22c:111 Programming Language Concepts. Fall Types I

CPSC 3740 Programming Languages University of Lethbridge. Data Types

Data Types. CSE 307 Principles of Programming Languages Stony Brook University

CS 430 Spring Mike Lam, Professor. Data Types and Type Checking

Array. Prepared By - Rifat Shahriyar

C for Engineers and Scientists: An Interpretive Approach. Chapter 10: Arrays

CS201- Introduction to Programming Latest Solved Mcqs from Midterm Papers May 07,2011. MIDTERM EXAMINATION Spring 2010

Index. object lifetimes, and ownership, use after change by an alias errors, use after drop errors, BTreeMap, 309

Lab 3. Pointers Programming Lab (Using C) XU Silei

Organization of Programming Languages CS320/520N. Lecture 06. Razvan C. Bunescu School of Electrical Engineering and Computer Science

Chapter 6 part 1. Data Types. (updated based on 11th edition) ISBN

Class Information ANNOUCEMENTS

Design and development of embedded systems for the Internet of Things (IoT) Fabio Angeletti Fabrizio Gattuso

Data Types. Data Types. Introduction. Data Types. Data Types. Data Types. Introduction

Introduction Primitive Data Types Character String Types User-Defined Ordinal Types Array Types. Record Types. Pointer and Reference Types

Francesco Nidito. Programmazione Avanzata AA 2007/08

CS 230 Programming Languages

Arrays and Pointers in C. Alan L. Cox

Novell Sample Paper. 1. On December 31, which ships will be sailing from the Port on a New Year's Eve.

CS2351 Data Structures. Lecture 7: A Brief Review of Pointers in C

Fundamentals of Programming Languages. Data Types Lecture 07 sl. dr. ing. Ciprian-Bogdan Chirila

Lectures 5-6: Introduction to C

Contents of Lecture 3

Organization of Programming Languages CS3200 / 5200N. Lecture 06

QUIZ: loops. Write a program that prints the integers from -7 to 15 (inclusive) using: for loop while loop do...while loop

CS349/SE382 A1 C Programming Tutorial

Introduction. Primitive Data Types: Integer. Primitive Data Types. ICOM 4036 Programming Languages

Review of the C Programming Language for Principles of Operating Systems

COSC252: Programming Languages: Basic Semantics: Data Types. Jeremy Bolton, PhD Asst Teaching Professor

Chapter 6. Data Types

The New C Standard (Excerpted material)

Novell Sample Papers


SFU CMPT Topic: Compound Types

1 Epic Test Review 2 Epic Test Review 3 Epic Test Review 4. Epic Test Review 5 Epic Test Review 6 Epic Test Review 7 Epic Test Review 8

Exam 3 Chapters 7 & 9

[0569] p 0318 garbage

HANDLING NONLOCAL REFERENCES

Recap. ANSI C Reserved Words C++ Multimedia Programming Lecture 2. Erwin M. Bakker Joachim Rijsdam

FOR Loop. FOR Loop has three parts:initialization,condition,increment. Syntax. for(initialization;condition;increment){ body;

C Programming. Course Outline. C Programming. Code: MBD101. Duration: 10 Hours. Prerequisites:

Type Checking. Prof. James L. Frankel Harvard University

Principles of Programming. Chapter 6: Arrays

Arrays and Pointers in C & C++

C: Pointers. C: Pointers. Department of Computer Science College of Engineering Boise State University. September 11, /21

Maltepe University Computer Engineering Department. BİL 133 Algoritma ve Programlama. Chapter 8: Arrays and pointers

CSCI-243 Exam 1 Review February 22, 2015 Presented by the RIT Computer Science Community

Computer Programming. C Array is a collection of data belongings to the same data type. data_type array_name[array_size];

Complex data structures. Cedric Saule

6. Pointers, Structs, and Arrays. 1. Juli 2011

COS 140: Foundations of Computer Science

TYPES, VALUES AND DECLARATIONS

M4.1-R3: PROGRAMMING AND PROBLEM SOLVING THROUGH C LANGUAGE

Chapter 6. Structured Data Types. Topics. Structured Data Types. Vectors and Arrays. Vectors. Vectors: subscripts

Lectures 5-6: Introduction to C

M3-R4: PROGRAMMING AND PROBLEM SOLVING THROUGH C LANGUAGE

Programming refresher and intro to C programming

Short Notes of CS201

Java Language Basics: Introduction To Java, Basic Features, Java Virtual Machine Concepts, Primitive Data Type And Variables, Java Operators,

UNIT 2 ARRAYS 2.0 INTRODUCTION. Structure. Page Nos.

Parameter passing. Programming in C. Important. Parameter passing... C implements call-by-value parameter passing. UVic SEng 265

STRUCTURED DATA TYPE ARRAYS IN C++ ONE-DIMENSIONAL ARRAY TWO-DIMENSIONAL ARRAY

CS201 - Introduction to Programming Glossary By

Programming. Pointers, Multi-dimensional Arrays and Memory Management

ECE 15B COMPUTER ORGANIZATION

CS 110 Computer Architecture. Lecture 2: Introduction to C, Part I. Instructor: Sören Schwertfeger.

CSC266 Introduction to Parallel Computing using GPUs Optimizing for Caches

Lesson 7. Reading and Writing a.k.a. Input and Output

Character Strings. String-copy Example

Announcements. My office hours are today in Gates 160 from 1PM-3PM. Programming Project 3 checkpoint due tomorrow night at 11:59PM.

COP 3223 Introduction to Programming with C - Study Union - Fall 2017

CSC 1600 Memory Layout for Unix Processes"

Review of the C Programming Language

From Java to C. Thanks to Randal E. Bryant and David R. O'Hallaron (Carnegie-Mellon University) for providing the basis for these slides

Data Types (cont.) Subset. subtype in Ada. Powerset. set of in Pascal. implementations. CSE 3302 Programming Languages 10/1/2007

Lecture 14. No in-class files today. Homework 7 (due on Wednesday) and Project 3 (due in 10 days) posted. Questions?

C Programming Language: C ADTs, 2d Dynamic Allocation. Math 230 Assembly Language Programming (Computer Organization) Thursday Jan 31, 2008

Software Practice 1 Basic Grammar

CSCI-1200 Data Structures Spring 2014 Lecture 5 Pointers, Arrays, Pointer Arithmetic

Final Exam. 12 December 2018, 120 minutes, 26 questions, 100 points

CS241 Computer Organization Spring Data Alignment

Gabriel Hugh Elkaim Spring CMPE 013/L: C Programming. CMPE 013/L: C Programming

Data Types. Outline. In Text: Chapter 6. What is a type? Primitives Strings Ordinals Arrays Records Sets Pointers 5-1. Chapter 6: Data Types 2

G Programming Languages - Fall 2012

Darshan Institute of Engineering & Technology for Diploma Studies Unit 5

CSE351 Spring 2018, Final Exam June 6, 2018

Dynamic Memory Allocation and Command-line Arguments

SE352b: Roadmap. SE352b Software Engineering Design Tools. W3: Programming Paradigms

CS 314 Principles of Programming Languages. Lecture 9

CS 31: Intro to Systems Arrays, Structs, Strings, and Pointers. Kevin Webb Swarthmore College March 1, 2016

Data Types and Computer Storage Arrays and Pointers. K&R, chapter 5

Pointers, Arrays, and Strings. CS449 Spring 2016

Programming for Electrical and Computer Engineers. Pointers and Arrays

Department of Computer Science & Engineering Indian Institute of Technology Kharagpur. Practice Sheet #04

Data Storage. August 9, Indiana University. Geoffrey Brown, Bryce Himebaugh 2015 August 9, / 19

Nested Loops. A loop can be nested inside another loop.

Chapter 1 GETTING STARTED. SYS-ED/ Computer Education Techniques, Inc.

Transcription:

CSC2/454 Programming Languages Design and Implementation Records and Arrays Sreepathi Pai November 6, 2017 URCS

Outline Scalar and Composite Types Arrays Records and Variants

Outline Scalar and Composite Types Arrays Records and Variants

Machine Types as Scalars Integer Types 8-bit (e.g. byte/char) 16-bit (e.g. short) 32-bit (e.g. int, long) 64-bit (e.g. int, long, long long) Exactly which C types are 64-bit specified by OS (ILP64, LP64, LLP64) Addresses Pointers (next class) Floating-point numbers (usually IEEE 754 standard) Half-precision (16-bit) Single-precision (32-bit) Double-precision (64-bit)

Non-machine Types as Scalars Multibyte Characters e.g. Unicode Usually mapped to same size integer type Bigints Integers much bigger than machine word size e.g. Python longs Multiprecision Arithmetic Exact arithmetic with reals e.g. numbers in Unix bc

Exotic Scalar Machine Types Decimal Numbers Binary coded decimal What other types does your machine support?

Composite Types Arrays Multiple elements, same type Records Multiple elements, different types Variants/Unions Multiple elements, different types But one type at a time! Bitsets Sets, Tuples, Dictionaries/Maps not covered today

Outline Scalar and Composite Types Arrays Records and Variants

Array Concepts Usually contains multiple elements of the same type Can support multi-dimensional data e.g. Matrix (2-D) Only array has name, elements are exclusively numerically indexed Unlike maps/dictionaries Some languages provide additional features e.g. consecutive elements of an array are also consecutive in memory

User Interface, Syntax and other Design Considerations [] or () for subscripts? a[2] or a(2)? a[1][2] or a[1, 2]? C s syntax also allows 3[a]? Allow access to slices of array? Entire row (e.g. a[1][:], or entire column? 2-D subset, n-d subset? Are these also arrays? Extend scalar operations to arrays? i.e. a + b is a[i] + b[i] when a and b are arrays? could utilize SIMD units Should compiler/runtime track array shape or should user? shape is size in each dimension e.g. for a[2][5]; size(a, 1) is 2.

Creation and Layout (1D) int a[5]; [ ] 0 1 2 3 4 + + + + + a a a a a

Creation and Layout (2D) - Row Major Multidimensional arrays need to be mapped to 1-D memory e.g. if a[2][5], then accessing a[1][4] maps to 9 Multiple ways to map array subscripts to addresses Scheme 1: Row Major Elements in same row are consecutive in memory Most common format, used in C

Creation and Layout (2D) - Column Major Scheme 2: Column Major Elements in same column are consecutive in memory Used mostly by FORTRAN

Creation and Layout (2D) - Pointer to Arrays Scheme 3: Pointer to Arrays Elements in outer dimensions are pointers to arrays in inner dimensions Only multidimensional array type in Java Also supported in C char a[7][10]; char *b[7] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};

Layout of Pointer to Arrays S u n d a y M o n d a y T u e s d a y W e d n e s d a y T h u r s d a y F r i d a y S a t u r d a y S u n d a y M o n d a y T u e s d a y W e d n e s d a y T h u r s d a y F r i d a y S a t u r d a y

Creation and Layout (2D) - Space-filling curves Z-order Curve Hilbert Curves

Address Calculations - I Row Major Array size a[2][5], base address is a Access a[1][3] is a + 1 * 5 + 3 i.e. row * ncols + col Column Major Array size a[2][5], base address is a Access a[1][3] is a + 3 * 2 + 1 i.e. col * nrows + row In both cases, largely O(1) for a fixed array for all indices Compiler can also simplify calculations at compile-time if dimensions known

Address Calculations - II Multidimensional arrays are arrays of pointers Array size *a[10], base address is a Access a[1][2] is deref(a + 1) + 2 Indirect addressing, requires reads of memory

Address Calculations - III Z-Curve Ordering? Hilbert Ordering? More computation, but better locality Is the tradeoff worth it?

Bounds Checking Should compiler track array shape? Should compiler throw runtime errors when out-of-bounds access occur? e.g. access a[5] in int a[5]? int A[N]; for(int i = 0; i <= N; i++)

Buffer Overflows What happens when elements beyond array length are written? int main(void) { int a[3]; int N = 7; int i; } for(i = 0; i < N; i++) { a[i] = 0xaabbccdd; printf("%d: %x\n", i, a[i]); }

Buffer Overflows Stack growth Higher addresses Previous (calling) frame buf Return address (gdb) run Starting program: /tmp/a.out 0: aabbccdd 1: aabbccdd 2: aabbccdd 3: aabbccdd 4: aabbccdd 5: aabbccdd 6: aabbccdd Program received signal SIGSEGV, Segmentation fault. 0x00007fffaabbccdd in?? ()

Dynamic Arrays, Resizing Arrays Should the language support arrays whose size will be known only at runtime? Stored on stack? Stored on heap? Multidimensional dynamic arrays? void fn(int N) { int x[n]; } int *y = malloc(n*sizeof(int));... Should resizing arrays be allowed?

Strings Are strings arrays? char a[] = "hello world"; Recall C does not store size of arrays C uses \0 as end-of-string marker. What about Unicode? (or wide characters?) These are multibyte character sets

How C-like languages deal with Unicode Recall \0 means end of string in C Unicode is usually 16-bit, but can be up to 32 bits UTF-8 encoding: 0 127 use a single byte 128+ use multiple bytes (up to 4) How does indexing change?

Outline Scalar and Composite Types Arrays Records and Variants

Records Also known as product types struct { char name[2]; int atomic_number; double atomic_weight; char metallic; } What is the size of this struct if: char is 1 byte int is 4 bytes double is 8 bytes

Record Layout (Packed) 4 bytes/32 bits number name atomic_weight metallic atomic_

Record Layout 4 bytes/32 bits name atomic_number atomic_weight metallic

Record Layout (Rearranged) 4 bytes/32 bits name metallic atomic_number atomic_weight

Variants Like records, except all fields occupy same space union { int seconds_since_epoch; int julian_day; } relative_date; The size of relative date is size of int, not 2 int

Problems void print_relative_date(union relative_date x) { printf("seconds since epoch: %d\n", x.seconds_since_epoch); }

Variant Tags struct relative_date_tagged { int is_seconds; union { int seconds_since_epoch; int julian_day; } relative_date; }

Variant Use Should language check variants are being used correctly? Initialization Every use struct relative_date_tagged t; if(t.is_seconds) { printf("seconds since epoch: %d\n", t.relative_date.seconds_since_epoch); } // code to handle julian days is missing! Some languages (notably the ML family) do not allow access to union members except in a separate match construct