Structure of Programming Languages Lecture 10

Similar documents
G Programming Languages - Fall 2012

CMSC 330: Organization of Programming Languages

CMSC 330: Organization of Programming Languages

Run-Time Environments/Garbage Collection

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

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

Programs in memory. The layout of memory is roughly:

Runtime. The optimized program is ready to run What sorts of facilities are available at runtime

Programming Language Implementation

Compiler Construction D7011E

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

CPS 506 Comparative Programming Languages. Programming Language

Project. there are a couple of 3 person teams. a new drop with new type checking is coming. regroup or see me or forever hold your peace

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

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

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

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

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

Deallocation Mechanisms. User-controlled Deallocation. Automatic Garbage Collection

CS61C : Machine Structures

Outline. Java Models for variables Types and type checking, type safety Interpretation vs. compilation. Reasoning about code. CSCI 2600 Spring

Lecture Notes on Garbage Collection

Heap Management. Heap Allocation

Lecture 23: Object Lifetime and Garbage Collection

CS61, Fall 2012 Section 2 Notes

CS 241 Honors Memory

Agenda. CSE P 501 Compilers. Java Implementation Overview. JVM Architecture. JVM Runtime Data Areas (1) JVM Data Types. CSE P 501 Su04 T-1

Design Issues. Subroutines and Control Abstraction. Subroutines and Control Abstraction. CSC 4101: Programming Languages 1. Textbook, Chapter 8

(notes modified from Noah Snavely, Spring 2009) Memory allocation

CSE P 501 Compilers. Java Implementation JVMs, JITs &c Hal Perkins Winter /11/ Hal Perkins & UW CSE V-1

Lecture Notes on Garbage Collection

Lecture 13: Garbage Collection

CA341 - Comparative Programming Languages

INITIALISING POINTER VARIABLES; DYNAMIC VARIABLES; OPERATIONS ON POINTERS

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

Pointers and References

Compiler Construction

Types. What is a type?

Dynamic Storage Allocation

CSCI-1200 Data Structures Spring 2017 Lecture 27 Garbage Collection & Smart Pointers

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

Garbage Collection (1)

Chapter 5. Names, Bindings, and Scopes

CS 231 Data Structures and Algorithms, Fall 2016

NOTE: Answer ANY FOUR of the following 6 sections:

CSCI-1200 Data Structures Fall 2011 Lecture 24 Garbage Collection & Smart Pointers

Lecture 13: Object orientation. Object oriented programming. Introduction. Object oriented programming. OO and ADT:s. Introduction

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

CS558 Programming Languages

Habanero Extreme Scale Software Research Project

CS558 Programming Languages

Programming Languages

CS558 Programming Languages

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

Chapter 17 vector and Free Store. Bjarne Stroustrup

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

Garbage Collection. Steven R. Bagley

Page 1. Stuff. Last Time. Today. Safety-Critical Systems MISRA-C. Terminology. Interrupts Inline assembly Intrinsics

Vector and Free Store (Pointers and Memory Allocation)

CS201 Some Important Definitions

Pointer Basics. Lecture 13 COP 3014 Spring March 28, 2018

CSC 533: Organization of Programming Languages. Spring 2005

CSCI-1200 Data Structures Spring 2016 Lecture 6 Pointers & Dynamic Memory

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

CSE 413 Languages & Implementation. Hal Perkins Winter 2019 Structs, Implementing Languages (credits: Dan Grossman, CSE 341)

COMP6771 Advanced C++ Programming

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

Memory Management. Didactic Module 14 Programming Languages - EEL670 1

Compiler Construction

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

Short Notes of CS201

Chapter 5 Names, Binding, Type Checking and Scopes

CPSC 3740 Programming Languages University of Lethbridge. Data Types

CSE P 501 Compilers. Memory Management and Garbage Collec<on Hal Perkins Winter UW CSE P 501 Winter 2016 W-1

Qualifying Exam in Programming Languages and Compilers

CS201 - Introduction to Programming Glossary By

CS 31: Intro to Systems Pointers and Memory. Kevin Webb Swarthmore College October 2, 2018

Memory. don t forget to take out the

C11: Garbage Collection and Constructors

+ Abstract Data Types

CSE450. Translation of Programming Languages. Lecture 11: Semantic Analysis: Types & Type Checking

Lecture 13: Complex Types and Garbage Collection

Lecture 7: Binding Time and Storage

CS 345. Garbage Collection. Vitaly Shmatikov. slide 1

Class Information ANNOUCEMENTS

Names, Scope, and Bindings

Francesco Nidito. Programmazione Avanzata AA 2007/08

Lecture 4 Memory Management

1 Dynamic Memory continued: Memory Leaks

Concepts Introduced in Chapter 7

Run-time Environments

Run-time Environments

Run-time Environments - 3

Object-Oriented Programming for Scientific Computing

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

Compilers. 8. Run-time Support. Laszlo Böszörmenyi Compilers Run-time - 1

Important From Last Time

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

LECTURE 03 LINKED LIST

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

Transcription:

Structure of Programming Languages Lecture 10 CS 6636 4536 Spring 2017 CS 6636 4536 Lecture 10: Classes... 1/23 Spring 2017 1 / 23

Outline 1 1. Types Type Coercion and Conversion Type Classes, Generics, and Polymorphism 2 Memory Management The Stack, the Static, and the Heap Non-Managed Memory Problems Garbage Collection 3 Homework CS 6636 4536 Lecture 10: Classes... 2/23 Spring 2017 2 / 23

1. Types Type Coercion and Conversion 3. Type Coercion and Conversion Casts, Conversions, Coercions Some coercions will create nonsense PL/1 was liberal with coercion C implemented coercion between numeric types Ada threw it all out C++ adds new coercions, under programmer control CS 6636 4536 Lecture 10: Classes... 3/23 Spring 2017 3 / 23

1. Types Type Coercion and Conversion Casts, Conversions, Coercions. The C type cast hides a rat s nest of problems. C++ got it straight. Static cast (type conversion, changes size or encoding, keeps semantics.) Reinterpret cast (pointer cast, changes semantics, keeps representation.) Const cast (removes or adds a restriction to a pointer) Dynamic cast (movement up or down a type hierarchy. Downward movement causes a run time type test and may fail.) A type coercion is a cast that is applied by the compiler. CS 6636 4536 Lecture 10: Classes... 4/23 Spring 2017 4 / 23

1. Types Type Coercion and Conversion Some coercions will create nonsense. Some languages will coerce argument types when they do not match the corresponding parameter types. Coercions are supposed to be applied ONLY when they preserve semantics. Lengthening a short value to a longer type preserves semantics. Shortening might or might not preserve the semantics, so it should not be used freely. Some changes of representation preserve semantics (int to double). Some generally do not (double to short int). Coercion is, therefore, a questionable practice. CS 6636 4536 Lecture 10: Classes... 5/23 Spring 2017 5 / 23

1. Types Type Coercion and Conversion PL/1 was liberal with coercion. In PL/1, any argument would be coerced to any parameter type if the compiler could find a chain of conversions to get from one to the other. In the process, garbage often happened. Consider this PL/1 statement: IF (a<=b<=c) THEN x = 1; ELSE t = 1 The result of a<=b is a single bit truth value, 1 or 0. The 1 or 0 is cast to the underlying type bitstring, length 1. The bitstring is promoted to an integer to match type of c. The integer is lengthened to the length of c. Now it is compared to c, and the answer is always true if c. is greater than 0! Analysis: Casting to an underlying representation type removes the original semantics. Then casting to a higher-level represented type ADDS semantics, which may be inappropriate. The final step in this evaluation compared apples (painted orange) to oranges. CS 6636 4536 Lecture 10: Classes... 6/23 Spring 2017 6 / 23

1. Types Type Coercion and Conversion C implemented coercion between numeric types. The idea in C is that numbers can retain all or most of their meaning when converted to a different representation. Lengthening a representation (float --> double) is safe and is used freely for coercion. Shortening a representation is not safe, and is only used if necessary to carry out an assignment or call-by-value. It should trigger a warning. Converting to a floating representation (int --> double or float) is usually safe and is used freely for coercion. Converting to an integer representation (double or float --> int) usually loses precision. It is considered unsafe and used only for assignment. It should trigger a warning. Analysis: This set of conventions works and is convenient. Warnings are never given if the cast is explicit. CS 6636 4536 Lecture 10: Classes... 7/23 Spring 2017 7 / 23

1. Types Type Coercion and Conversion Ada threw it all out. Semantic validity was a primary goal in the design of Ada. The coercion messes made by PL/1 were to be avoided. So all coercion was thrown out in the design of Ada. If you wanted to add 1 to a double, you could define a method for operator+ that converted the 1 to a double representation, then did the addition and returned a real number. Ada programmers learned to be careful of the types of their constants. Analysis: The semantic nonsense problem was solved at the cost of convenience. CS 6636 4536 Lecture 10: Classes... 8/23 Spring 2017 8 / 23

1. Types Type Coercion and Conversion C++ adds new coercions, under programmer control. C++ adopted the type coercion rules of C and added the ability to define conversions for new types. Programmer-defined casts will be used for coercion, where needed. A class constructor can be used to convert from any type to the class type. The cast operator can be used to convert from the class type to any other type. Programmer-defined casts will be used for coercion, if necessary. Overuse of this facility can make code very hard to penetrate. Casts up a class hierarchy (toward the base class) are used for coercion. Downward casts must be explicit and may bomb at runtime. C++ preserves object semantics by doing a run-time test for the validity of a downward cast on a derivation hierarchy. CS 6636 4536 Lecture 10: Classes... 9/23 Spring 2017 9 / 23

1. Types Type Classes, Generics, and Polymorphism What is a Class? If a language supports classes, it is called object oriented. However, this covers a wide range of behaviors. A class is a set of data members, with member names...... plus the operators and functions that work on the data. It may be fully defined or be abstract because some of its functions are prototyped but not defined. The privacy/visibility of data can be restricted to a class. It should provide a stable interface to the world. (Not Ruby, Python.) It may or may not have subclass relationships with other classes. If one class is derived from another, they may or may not form a polymorphic class. At run time, each object belongs to at least one class the class used to allocate it. It also belongs to all the superclasses of that class. CS 6636 4536 Lecture 10: Classes... 10/23 Spring 2017 10 / 23

1. Types Type Classes, Generics, and Polymorphism Why are Classes Useful? Grouping related data and functions (lexical coherence) is better than spreading them all over a program. Classes make it possible to develop and debug programs in a team environment, as long as the language enforces a stable interface for each class. Classes provide a way to make programmer-defined types behave like primitive types w.r.t. input, output, and operations. Derived classes and polymorphism allow efficient creation of many variations on a basic type. Derivation gives access to sophisticated pre-written packages (Swing, collections). Classes provide an analog of database tables. This is essential in web application programming. CS 6636 4536 Lecture 10: Classes... 11/23 Spring 2017 11 / 23

1. Types Type Classes, Generics, and Polymorphism Classes in C++ In C++, the interface provided by a class is fully defined at compile time and stable thereafter. Classes are used for strong type checking and type coercion. Cast operators may be programmer-defined and will be used for coercion, where appropriate. New methods can be defined for all the existing operators, including input and output. In a class hierarchy, methods can be either virtual (dispatched at run time) or not (dispatched at compile time). This allows for optimization of non-virtual functions. CS 6636 4536 Lecture 10: Classes... 12/23 Spring 2017 12 / 23

1. Types Type Classes, Generics, and Polymorphism Classes in Ruby Ruby classes have methods, including constructors (initialize) getters(@var) and setters (var=). Syntax is provided (attr_accessor)for defining part names with getters and setters. Data members (untyped names) can be added during execution by mentioning a new name in a method. Different execution paths could create different sets of data members. New methods can be added to a class at any time. Thus, a Ruby class does not provide a consistent interface. Each instance of a Ruby class could have different functionality. To Ruby people, this is an advantage: This language feature makes the language incredibly flexible. CS 6636 4536 Lecture 10: Classes... 13/23 Spring 2017 13 / 23

Memory Management Part 2. Memory Management The Stack, the Static, and the Heap Non- Managed Memory Problems Garbage Collection CS 6636 4536 Lecture 10: Classes... 14/23 Spring 2017 14 / 23

Memory Management The Stack, the Static, and the Heap The System, the Process, and Virtual Memory When a process is loaded, a virtual memory operating system will create segments for: The code (read only) and literal strings. The stack, possibly combined with global variables. The dynamic heap. A static storage area, if the language supports local static variables. Globals could also be placed in this segment. Each segment is implemented by a page table and can be as short or as long as necessary. CS 6636 4536 Lecture 10: Classes... 15/23 Spring 2017 15 / 23

Memory Management The Stack, the Static, and the Heap The Stack, the Static, and the Heap The code segment and static segment do not grow or shrink during execution. The stack grows automatically as functions are called; if more pages are needed to implement it, they are added automatically. As functions return, the stack shrinks. If a stack-page is no longer needed because its functions have all returned, it will be paged out and not become part of the memory-bloat. The stack is always compact. The heap grows when new objects are dynamically allocated. It doesn t automatically shrink. As objects become inaccessible (garbage) or are freed explicitly, the heap becomes sparse. This becomes a problem if objects are constantly created and discarded, since more and more pages will be required to store the remaining useful data objects CS 6636 4536 Lecture 10: Classes... 16/23 Spring 2017 16 / 23

Memory Management Non-Managed Memory Problems Memory Leaks A memory leak is an area of memory that has been allocated, is no longer in use, but it has not been deallocated. Memory leaks happen when programmers allocate new objects and fail to incorporate them into the program s data structures. Then they forget to free them. This is not a problem for little applications that run and terminate. It becomes a problem only for applications that you turn on and leave open on your desktop for days or weeks. (A mail program?) Gradually, the number of dead memory blocks builds up and the heap segment becomes larger and larger, sparser and sparser. Eventually, the memory needs of the application become so great that it crowds out other applications and thrashing starts. CS 6636 4536 Lecture 10: Classes... 17/23 Spring 2017 17 / 23

Memory Management Non-Managed Memory Problems Dangling Pointers A dangling pointer is a pointer that is still in use longer in use that points at an object that has been deallocated. These happen when a short-lived object is pointed at by an object with a longer lifetime. The typical error is to set a pointer to a local variable or array, and return that pointer from the function. This is a plague on intermediate programmers who have no clear idea of when objects are born, how long they live, and when they die. Managed-memory systems avoid this problem by forcing arrays and objects to all be dynamically allocated (permanent lifetime). The result is that lots of extra run-time and run-space is used creating very temporary objects that then need collection. CS 6636 4536 Lecture 10: Classes... 18/23 Spring 2017 18 / 23

Memory Management Non-Managed Memory Problems Efficient Management of Dynamic Objects Make your own freelist. For every type of object you allocate dynamically, create a recycling system and an allocation function called mynew. Pre-allocate a block, called pool, of this type of objects and create a linked-list called freelist to organize them. Initialize freelist to null. When mynew is called, return the first object on the freelist. If is is empty, return the next object from the pool. If the pool is also empty, allocate another block of objects and store it in pool. CS 6636 4536 Lecture 10: Classes... 19/23 Spring 2017 19 / 23

Memory Management Garbage Collection Mark-and Sweep Garbage Collection A garbage collector (GC) identifies the memory blocks that are still accessible and collects the discarded areas for recycling. It starts with the roots of the objects in use, on the stack, in registers, in system data structures. It follows each pointer attached to anything attached to a root. This means walking down entire data structures at run time. At each step, the current block is marked When this is finished, the garbage is swept up by starting at the beginning of each memory segment and reclaiming blocks that have not been marked recently. Compaction is easy and efficient during this process IF ALL POINTERS were the results of calling new. (No pointers were calculated by programmer-written code.) CS 6636 4536 Lecture 10: Classes... 20/23 Spring 2017 20 / 23

Memory Management Garbage Collection Advantages of Managed Memory A programmer can use a managed-memory language without having any firm idea of how objects, pointers, allocation, and deallocation work. Some programmers have trouble getting programs to work when they need to use pointers. They write code that won t compile or code that crashes at run time. Reliance on managed memory lets more programmers debug more code faster and with less expertise. Our systems are so big and so fast now, maybe we don t need to worry about code bloat and inefficient code any more. CS 6636 4536 Lecture 10: Classes... 21/23 Spring 2017 21 / 23

Memory Management Garbage Collection Disadvantages of Managed Memory The programmer cannot control when the GC works or what it collects. The GC has a complex relationship with new allocation and initialization: the garbage collector must not try to collect a half-initialized new object. For this reason, there are times when a GC may and may not execute safely. Some programmers are not aware of the performance problems caused by managed memory. They write code that creates many unnecessary objects that must soon be collected, decreasing application performance. While collection is happening, the process is suspended. This makes managed memory inappropriate for some real-time applications. Use of a compacting collector strongly restricts the kind of data structures you can use. CS 6636 4536 Lecture 10: Classes... 22/23 Spring 2017 22 / 23

Homework Homework 10 Read Chapters 14 18 in the textbook. 1 How is a static variable like a global variable? How are they different? 2 Explain the most important benefit of managed memory. 3 Explain an important fault or limitation of managed memory. 4 Choose some detail about types and compare (in half a page) how it is supported (or not supported) in two different languages, either languages mentioned in the reading or in this lecture. 5 Write a half-page essay about something you learned about types from reading this material. CS 6636 4536 Lecture 10: Classes... 23/23 Spring 2017 23 / 23