Programming Languages

Similar documents
Chapter 8 :: Composite Types

Types II. Hwansoo Han

Lecture 13: Complex Types and Garbage Collection

Concepts Introduced in Chapter 7

Programming Languages

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

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

CS558 Programming Languages

Chapter 5. Names, Bindings, and Scopes

Chapter 5 Names, Binding, 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

Programming Languages

Principles of Programming Languages Topic: Scope and Memory Professor Louis Steinberg Fall 2004

Data Types In Text: Ch C apter 6 1

G Programming Languages - Fall 2012

Chapter 5 Names, Bindings, Type Checking, and Scopes

NOTE: Answer ANY FOUR of the following 6 sections:

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

Lecture 13: Garbage Collection

Programming Languages Third Edition. Chapter 7 Basic Semantics

Chapter 11 :: Functional Languages

Programming Languages Third Edition. Chapter 10 Control II Procedures and Environments

CPSC 3740 Programming Languages University of Lethbridge. Data Types

Storage. Outline. Variables and Updating. Composite Variables. Storables Lifetime : Programming Languages. Course slides - Storage

Imperative Programming

CMSC 330: Organization of Programming Languages

Chapter 3:: Names, Scopes, and Bindings (cont.)

Chapter 3:: Names, Scopes, and Bindings (cont.)

CMSC 331 Final Exam Section 0201 December 18, 2000

CS 536 Introduction to Programming Languages and Compilers Charles N. Fischer Lecture 11

Organization of Programming Languages (CSE452) Why are there so many programming languages? What makes a language successful?

Chapter 3:: Names, Scopes, and Bindings

Storage. Outline. Variables and updating. Copy vs. Ref semantics Lifetime. Dangling References Garbage collection

CS321 Languages and Compiler Design I Winter 2012 Lecture 13

Programming Language Pragmatics

Structure of Programming Languages Lecture 10

One-Slide Summary. Lecture Outine. Automatic Memory Management #1. Why Automatic Memory Management? Garbage Collection.

first class citizens

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

Memory Allocation. Static Allocation. Dynamic Allocation. Dynamic Storage Allocation. CS 414: Operating Systems Spring 2008

CMSC 330: Organization of Programming Languages

Lecture Overview. [Scott, chapter 7] [Sebesta, chapter 6]

Acknowledgements These slides are based on Kathryn McKinley s slides on garbage collection as well as E Christopher Lewis s slides

Deallocation Mechanisms. User-controlled Deallocation. Automatic Garbage Collection

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

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

Principles of Programming Languages

Lecture 23: Object Lifetime and Garbage Collection

CIS24 Project #3. Student Name: Chun Chung Cheung Course Section: SA Date: 4/28/2003 Professor: Kopec. Subject: Functional Programming Language (ML)

Memory Management. Memory Management... Memory Management... Interface to Dynamic allocation

Run-Time Environments/Garbage Collection

Heap Management. Heap Allocation

CS 230 Programming Languages

Introduction. A. Bellaachia Page: 1

Chapter 6. Data Types ISBN

Example. program sort; var a : array[0..10] of integer; procedure readarray; : function partition (y, z :integer) :integer; var i, j,x, v :integer; :

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

Sustainable Memory Use Allocation & (Implicit) Deallocation (mostly in Java)

Compiler Construction

CMSC 330: Organization of Programming Languages. Memory Management and Garbage Collection

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

Types and Type Inference

Chapter 8 :: Subroutines and Control Abstraction. Final Test. Final Test Review Tomorrow

Compiler Construction

Chapter 6. Data Types ISBN

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

Memory Management. Didactic Module 14 Programming Languages - EEL670 1

Memory Management. Chapter Fourteen Modern Programming Languages, 2nd ed. 1

Organization of Programming Languages CS3200 / 5200N. Lecture 06

1. true / false By a compiler we mean a program that translates to code that will run natively on some machine.

Type Systems, Type Inference, and Polymorphism

Run-time Environments

Run-time Environments

2. Reachability in garbage collection is just an approximation of garbage.

Outline. When we last saw our heros. Language Issues. Announcements: Selecting a Language FORTRAN C MATLAB Java

Operating Systems CMPSCI 377, Lec 2 Intro to C/C++ Prashant Shenoy University of Massachusetts Amherst

Topic 9: Type Checking

Topic 9: Type Checking


Functional Programming Language Haskell

CS558 Programming Languages

Course Overview. Arrays. Static Arrays. Static Arrays. Static Arrays with different lower bound. Static Arrays. PART I: overview material

Automatic Memory Management

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

Compiler construction 2009

Types and Type Inference

Programming Languages, Summary CSC419; Odelia Schwartz

Weeks 6&7: Procedures and Parameter Passing

A.Arpaci-Dusseau. Mapping from logical address space to physical address space. CS 537:Operating Systems lecture12.fm.2

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

Functional Programming

References and pointers

CS113: Lecture 4. Topics: Functions. Function Activation Records


CS558 Programming Languages

Control Flow February 9, Lecture 7

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

ACM Trivia Bowl. Thursday April 3 rd (two days from now) 7pm OLS 001 Snacks and drinks provided All are welcome! I will be there.

Run-time Environments - 3

CA31-1K DIS. Pointers. TA: You Lu

Transcription:

Programming Languages Tevfik Koşar Lecture - XVIII March 23 rd, 2006 1 Roadmap Arrays Pointers Lists Files and I/O 2 1

Arrays Two layout strategies for arrays Contiguous elements Row pointers Row pointers an option in C allows rows to be put anywhere - nice for big arrays on machines with segmentation problems avoids multiplication nice for matrices whose rows are of different lengths e.g. an array of strings requires extra space for the pointers 3 Arrays 4 2

Example: Suppose Arrays A : array [L1..U1] of array [L2..U2] of array [L3..U3] of elem; D1 = U1-L1+1 D2 = U2-L2+1 D3 = U3-L3+1 Let S3 = size of elem S2 = D3 * S3 S1 = D2 * S2 5 Arrays 6 3

Arrays Example (continued) We could compute all that at run time, but we can make do with fewer subtractions: == (i * S1) + (j * S2) + (k * S3) + address of A - [(L1 * S1) + (L2 * S2) + (L3 * S3)] The stuff in square brackets is compile-time constant that depends only on the type of A 7 Strings Strings are really just arrays of characters They are often special-cased, to give them flexibility (like polymorphism or dynamic sizing) that is not available for arrays in general It's easier to provide these things for strings than for arrays in general because strings are onedimensional and (more important) non-circular 8 4

Sets We learned about a lot of possible implementations Bitsets are what usually get built into programming languages Things like intersection, union, membership, etc. can be implemented efficiently with bitwise logical instructions Some languages place limits on the sizes of sets to make it easier for the implementor There is really no excuse for this 9 Pointers And Recursive Types Pointers serve two purposes: efficient (and sometimes intuitive) access to elaborated objects (as in C) dynamic creation of linked data structures, in conjunction with a heap storage manager Several languages (e.g. Pascal) restrict pointers to accessing things in the heap Pointers are used with a value model of variables They aren't needed with a reference model 10 5

Pointers And Recursive Types 11 Pointers And Recursive Types 12 6

Pointers And Recursive Types C pointers and arrays int *a == int a[] int **a == int *a[] BUT equivalences don't always hold Specifically, a declaration allocates an array if it specifies a size for the first dimension otherwise it allocates a pointer int **a, int *a[] pointer to pointer to int int *a[n], n-element array of row pointers int a[n][m], 2-d array 13 Pointers And Recursive Types Compiler has to be able to tell the size of the things to which you point So the following aren't valid: int a[][] bad int (*a)[] bad C declaration rule: read right as far as you can (subject to parentheses), then left, then out a level and repeat int *a[n], n-element array of pointers to integer int (*a)[n], pointer to n-element array of integers 14 7

Pointers And Recursive Types Problems with dangling pointers are due to explicit deallocation of heap objects only in languages that have explicit deallocation implicit deallocation of elaborated objects Two implementation mechanisms to catch dangling pointers Tombstones Locks and Keys 15 Pointers And Recursive Types 16 8

Pointers And Recursive Types 17 Pointers And Recursive Types Problems with garbage collection many languages leave it up to the programmer to design without garbage creation - this is VERY hard others arrange for automatic garbage collection reference counting does not work for circular structures works great for strings should also work to collect unneeded tombstones 18 9

Pointers And Recursive Types Garbage collection with reference counts 19 Pointers And Recursive Types Mark-and-sweep commonplace in Lisp dialects complicated in languages with rich type structure, but possible if language is strongly typed achieved successfully in Cedar, Ada, Java, Modula-3, ML complete solution impossible in languages that are not strongly typed conservative approximation possible in almost any language (Xerox Portable Common Runtime approach) 20 10

Pointers And Recursive Types Garbage collection with pointer reversal 21 Lists A list is defined recursively as either the empty list or a pair consisting of an object (which may be either a list or an atom) and another (shorter) list Lists are ideally suited to programming in functional and logic languages In Lisp, in fact, a program is a list, and can extend itself at run time by constructing a list and executing it Lists can also be used in imperative programs 22 11

Files and Input/Output Input/output (I/O) facilities allow a program to communicate with the outside world interactive I/O and I/O with files Interactive I/O generally implies communication with human users or physical devices Files generally refer to off-line storage implemented by the operating system Files may be further categorized into temporary persistent 23 12