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

Similar documents
CSE 307: Principles of Programming Languages

Programming Languages Third Edition. Chapter 7 Basic Semantics

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

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

Run-time Environments

Run-time Environments

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

StackVsHeap SPL/2010 SPL/20

Binding and Storage. COMP 524: Programming Language Concepts Björn B. Brandenburg. The University of North Carolina at Chapel Hill

Chapter 5 Names, Binding, Type Checking and Scopes

Compiler Construction

Scope, Functions, and Storage Management

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

Class Information ANNOUCEMENTS

Compiler Construction

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


Chapter 3:: Names, Scopes, and Bindings

G Programming Languages - Fall 2012

Name, Scope, and Binding. Outline [1]

Arrays. Returning arrays Pointers Dynamic arrays Smart pointers Vectors

Types and Type Inference

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

Programming Language Concepts Scoping. Janyl Jumadinova January 31, 2017

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

Run-Time Data Structures

11. a b c d e. 12. a b c d e. 13. a b c d e. 14. a b c d e. 15. a b c d e

Older geometric based addressing is called CHS for cylinder-head-sector. This triple value uniquely identifies every sector.

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

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

Contents. 8-1 Copyright (c) N. Afshartous

CMSC 330: Organization of Programming Languages

Chapter 5. Names, Bindings, and Scopes

CS558 Programming Languages

CSC 1600 Memory Layout for Unix Processes"

CPSC 3740 Programming Languages University of Lethbridge. Data Types

CMSC 330: Organization of Programming Languages

CS162 - POINTERS. Lecture: Pointers and Dynamic Memory

Advanced Programming & C++ Language

CS558 Programming Languages

CSCI312 Principles of Programming Languages!

CS 230 Programming Languages

CS558 Programming Languages

Names, Bindings, Scopes

Attributes, Bindings, and Semantic Functions Declarations, Blocks, Scope, and the Symbol Table Name Resolution and Overloading Allocation, Lifetimes,

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

Topic 7: Activation Records

Implementing Subroutines. Outline [1]

Lecture Notes on Memory Management

Advances in Programming Languages: Regions

Procedure and Object- Oriented Abstraction

Short Notes of CS201

Concepts Introduced in Chapter 7

Memory management COSC346

CS201 - Introduction to Programming Glossary By

LANGUAGE RUNTIME NON-VOLATILE RAM AWARE SWAPPING

Run-time Environments. Lecture 13. Prof. Alex Aiken Original Slides (Modified by Prof. Vijay Ganesh) Lecture 13

Run-time Environments -Part 3

CS 345. Garbage Collection. Vitaly Shmatikov. slide 1

Object-Oriented Programming for Scientific Computing

Structure of Programming Languages Lecture 10

Dynamic Data Structures. CSCI 112: Programming in C

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

Chapter 9 :: Subroutines and Control Abstraction

Chapter 5 Names, Bindings, Type Checking, and Scopes

CSE 5317 Midterm Examination 4 March Solutions

Heap Management. Heap Allocation

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

Run Time Environment

CS558 Programming Languages. Winter 2013 Lecture 3

Run-time Environments - 2

CS 241 Honors Memory

Pointers, Dynamic Data, and Reference Types

CS164: Programming Assignment 5 Decaf Semantic Analysis and Code Generation

! Those values must be stored somewhere! Therefore, variables must somehow be bound. ! How?

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

Lecture 13: Garbage Collection

Chapter 8 :: Composite Types

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

Lecture 7: Binding Time and Storage

Deallocation Mechanisms. User-controlled Deallocation. Automatic Garbage Collection

Types and Type Inference

Separate Compilation Model


Inheritance, Polymorphism and the Object Memory Model

CPSC 213. Introduction to Computer Systems. Winter Session 2017, Term 2. Unit 1c Jan 24, 26, 29, 31, and Feb 2

Run-time Environments - 3

Chapter 5 Names, Bindings, Type Checking, and Scopes

NOTE: Answer ANY FOUR of the following 6 sections:

Run-time Environment

Lecture Notes on Memory Management

Programmin Languages/Variables and Storage

Performance of Non-Moving Garbage Collectors. Hans-J. Boehm HP Labs

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

6.172 Performance Engineering of Software Systems Spring Lecture 9. P after. Figure 1: A diagram of the stack (Image by MIT OpenCourseWare.

Program Analysis. Uday Khedker. ( uday) Department of Computer Science and Engineering, Indian Institute of Technology, Bombay

Object-oriented Programming. Object-oriented Programming

The basic operations defined on a symbol table include: free to remove all entries and free the storage of a symbol table

Programming Languages

CSE 504. Expression evaluation. Expression Evaluation, Runtime Environments. One possible semantics: Problem:

Transcription:

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

Outline. Dynamic Allocation. Variables and Constants. Aliases and Problems. Garbage.

Introduction. On Wednesday, we were talking about environments These were mappings of names to environments. We had just discussed how this worked for local variables in block structured languages: When we make a function call, we push locations onto the stack as needed for the local variables; when the call completes these locations are popped. Parameter variables that don't fit into the available registers are handled similarly. It is usually arranged that after the call returns the top of the stack will have the return value. Usually one has a couple of offset pointers into the stack, such as a pointer to the top and a base pointer for the start of the frame. How long a variable lives on the stack is its lifetime.

The Heap It is often the case that one has a programming task for which we don't know how much memory will be needed until runtime. For example, we store user input and we don't know in advance how much there will be. It also might be convenient to allow the lifetime of this data to exactly match that of a given block. To facilitate this kind of allocation (called dynamic allocation), most modern programming languages have a runtime heap. In C, when we make a call to malloc, we are allocating memory from this heap; a call to free deletes from the heap. In C++, or Java when we use new we are allocating from the heap.

More on the Heap. Typically, the memory for the heap is allocated starting from the bottom of the program environment growing towards the top; the stack on the other hand, grows downward to the bottom; and the static area is separate from either. C/C++ use pointers to refer to locations within the heap. A NULL or 0 address is used as the address for pointers which are not yet assigned to allocated memory. Other languages such as Java use the name references. The sections of heap memory currently allocated may become holey as memory becomes deallocated. So the heap structure needs mechanisms both to remember what is currently free/allocated; as well as occasionally to defragment this memory. Some languages such as Java have a garbage collector which is used to deallocate memory that is no longer referenced. Note some languages allow us to set which of the heap, stack, or global storage should be used for storing a variable. For example, the keyword static in C.

Variables. A variable is an object whose stored value can change during execution. So a variable can be completely specified by: Name, Other Attributes (such as type and size), Location, and Value. Sometimes people use box and circle diagrams to represent the main aspects of this: Name Value Location

Assignment The principle way a variable changes value is through assignment: x= e; where e is an expression. The semantics of this are that e is evaluated to a value, which is then copied into the location of x. If e is another variable y, then the assignment: =y; // can be viewed as y x

More on Assignment It is important to distinguish between the location of a variable and the value stored there. The value stored at a location is often called an r-value (because this is what a variable often means when it is on the right hand side of an assignment); the location of a variable is often called the l-value. Some languages like ML make this distinction explicit. For instance, x would refer to the location,!x to the value. So to increment x one would write: x :=!x+1; C automatically dereferences l-values to r-values, and to do things explicitly uses & to get the l-value of an r-value and * to get r-value of an l-value.

Assignment Types. In some languages an assignment x=y actually binds the location of x to y. This is called assignment by sharing. An alternative to this is to allocate a new location, copy the value of y to this new location, and bind x to the new location. This might be called assignment by cloning. These two kinds of semantics are often called pointer semantics to distinguish them from the usual storage semantics. For example, Java uses assignment for sharing for objects, but not for simple data.

Constants. A constant is a language entity that has a fixed value for the duration of its existence in a program. A constant is like a variable except that it has no location attribute only a value attribute. One sometimes says it has value semantics. The notion of a constant is symbolic. A constant is essentially a name for a value. Entities like "hello there", 42 and the like are literals not constants. Constants come in two main flavors: compile-time, whose value can be determine at the time of translation; and static, whose value can only be determined after the program loads.(for instance, the location of a global variable). Only the latter actually need to be stored in memory. One can also have function constants, function variables, and function literals.

Aliases. An alias occurs when the same object is bound to two different names at the same time. For example, two pointers to the same object. Aliases can cause side-effects, where a side-effect of a statement to be any change in the value of a variable that persists beyond the execution of the statement. For example, if x and y point to the same thing. Then *y=2; has two side-effects changing the value of y and changing the value of x -- the latter may not have been intended. Another example is x and y point to the same memory. We call free(y); //Now x points to memory which has been deallocated -- this is called a dangling reference.

Garbage Collection. One way to reduce the problem of dangling references is to not allow explicit deallocation of memory. Instead, a runtime program called a garbage collector is used which determines which locations on the heap are no longer referenced, and deallocate them. Java, Smalltalk, and many functional languages use garbage collectors.