CS 230 Programming Languages

Similar documents
CPSC 3740 Programming Languages University of Lethbridge. Data Types

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

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

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

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

Chapter 6. Data Types ISBN

Chapter 6. Data Types

Chapter 6. Data Types ISBN

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

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

Chapter 5. Names, Bindings, and Scopes

Chapter 5 Names, Binding, Type Checking and Scopes

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

Chapter 5 Names, Bindings, Type Checking, and Scopes

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

Chapter 6. Data Types. *modified by Stephanie Schwartz ISBN

Organization of Programming Languages CS3200 / 5200N. Lecture 06

Software II: Principles of Programming Languages

In Java we have the keyword null, which is the value of an uninitialized reference type

Data Types In Text: Ch C apter 6 1

Types II. Hwansoo Han

Chapter 6. Data Types ISBN

CS 330 Lecture 18. Symbol table. C scope rules. Declarations. Chapter 5 Louden Outline

A data type defines a collection of data objects and a set of predefined operations on those objects

NOTE: Answer ANY FOUR of the following 6 sections:

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #44. Multidimensional Array and pointers

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

9/7/17. Outline. Name, Scope and Binding. Names. Introduction. Names (continued) Names (continued) In Text: Chapter 5

Chapter 8 :: Composite Types

TYPES, VALUES AND DECLARATIONS

12/4/18. Outline. Implementing Subprograms. Semantics of a subroutine call. Storage of Information. Semantics of a subroutine return

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

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

Programming Languages Third Edition. Chapter 7 Basic Semantics

Implementing Abstractions

Programming Languages, Summary CSC419; Odelia Schwartz

Chapter 9 Subprograms

Chapter 3:: Names, Scopes, and Bindings

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

CSE 452: Programming Languages. Where are we? Types: Intuitive Perspective. Data Types. High-level Programming Languages.

CS 3360 Design and Implementation of Programming Languages. Exam 1

Programming Language Concepts Scoping. Janyl Jumadinova January 31, 2017

COS 140: Foundations of Computer Science

Subprograms. Bilkent University. CS315 Programming Languages Pinar Duygulu

Programming Languages

Data Types. Every program uses data, either explicitly or implicitly to arrive at a result.

Heap, Variables, References, and Garbage. CS152. Chris Pollett. Oct. 13, 2008.

Chapter 7. Expressions and Assignment Statements ISBN

CS 376b Computer Vision

Chapter 7. Expressions and Assignment Statements

Types. What is a type?

CSC 533: Organization of Programming Languages. Spring 2005

Class Information ANNOUCEMENTS

Chapter 9. Subprograms

Chapter 5. Variables. Topics. Imperative Paradigm. Von Neumann Architecture

Chapter 7. Expressions and Assignment Statements (updated edition 11) ISBN

CS162 - POINTERS. Lecture: Pointers and Dynamic Memory

Names, Bindings, Scopes

COS 140: Foundations of Computer Science

Lecture 9: Control Flow

Concepts Introduced in Chapter 7

Software II: Principles of Programming Languages. Why Expressions?

Subprograms. Copyright 2015 Pearson. All rights reserved. 1-1

Special Topics: Programming Languages

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

Array Initialization. Rectangular and Jagged Arrays. Arrays Operations. ICOM 4036 Programming Languages. Data Types

The PCAT Programming Language Reference Manual

Answer: Early binding generally leads to greater efficiency (compilation approach) Late binding general leads to greater flexibility

Type Bindings. Static Type Binding

Binding and Variables

Lecture 7: Type Systems and Symbol Tables. CS 540 George Mason University

Topic IV. Block-structured procedural languages Algol and Pascal. References:

The SPL Programming Language Reference Manual

Fundamentals of Programming Languages

CSE 307: Principles of Programming Languages

Chapter 7:: Data Types. Mid-Term Test. Mid-Term Test (cont.) Administrative Notes

Arrays: Higher Dimensional Arrays. CS0007: Introduction to Computer Programming

CSE 307: Principles of Programming Languages

For each of the following variables named x, specify whether they are static, stack-dynamic, or heapdynamic:

Programming Languages

CS107 Handout 08 Spring 2007 April 9, 2007 The Ins and Outs of C Arrays

CS453 Arrays in Java in general

Programming Languages

Topic IV. Parameters. Chapter 5 of Programming languages: Concepts & constructs by R. Sethi (2ND EDITION). Addison-Wesley, 1996.

UEE1302 (1102) F10 Introduction to Computers and Programming (I)

An Activation Record for Simple Subprograms. Activation Record for a Language with Stack-Dynamic Local Variables

Concepts of Programming Languages

Declaring Pointers. Declaration of pointers <type> *variable <type> *variable = initial-value Examples:

More Perl. CS174 Chris Pollett Oct 25, 2006.

Programming Languages Third Edition. Chapter 9 Control I Expressions and Statements

Pointers. Memory. void foo() { }//return

Where We Are. Lexical Analysis. Syntax Analysis. IR Generation. IR Optimization. Code Generation. Machine Code. Optimization.

Hi everyone. I hope everyone had a good Fourth of July. Today we're going to be covering graph search. Now, whenever we bring up graph algorithms, we

Pointers in C/C++ 1 Memory Addresses 2

Chapter 7. Expressions and Assignment Statements

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

Imperative Programming

C++ for Java Programmers

Lecture 13: Complex Types and Garbage Collection

Final-Term Papers Solved MCQS with Reference

Transcription:

CS 230 Programming Languages 11 / 20 / 2015 Instructor: Michael Eckmann

Questions/comments? Chapter 6 Arrays Pointers Today s Topics

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?

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?

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.

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

Lower bound of subscripts Usually 0 (C-based languages) Fortran95 defaults to 1. Why use 0?

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?

Static vs. Fixed stack-dynamic Arrays 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. And they can be deallocated from the stack when they're no longer needed.

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)

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

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.

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.

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 Java - fixed heap-dynamic arrays Their subscripts and allocation are dynamically bound but fixed afterwards. What does that mean? What kind of arrays do you think Perl has?

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

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.

Array operations (operate on array as a whole) Some languages allow concatenation of arrays and can use relational operators on arrays Others like APL have arithmetic operations on arrays like vector multiplication and matrix transpose, etc.

Rectangular vs. jagged arrays 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?

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.

Implementation of array types Arrays How to access an array element Access function code needs to produce the address of an element 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 being done, what do you think the descriptor will contain?

Multidimensional Arrays Design issue: How to store the multidimensional array in memory? A 1-D array is typically stored sequentially in memory

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

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

Multidimensional Arrays 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 descriptor for the array

Multidimensional Arrays Address of element at row i, column j is = addressofarray + (numcols * i + j) * sizeofelement;

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.

Pointers Using pointers Pointer reference (the address) Indirect reference (aka dereference) gives us what data is in the memory address that is 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.

Pointers

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