CSE 307: Principles of Programming Languages

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

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

Programming Languages Third Edition. Chapter 7 Basic Semantics

Pointers. 1 Background. 1.1 Variables and Memory. 1.2 Motivating Pointers Massachusetts Institute of Technology

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

CSE 307: Principles of Programming Languages

Compiler Construction

[0569] p 0318 garbage

CS558 Programming Languages

CSE 307: Principles of Programming Languages

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

CS558 Programming Languages

CPSC 3740 Programming Languages University of Lethbridge. Data Types

A brief introduction to C programming for Java programmers

Pointers, Dynamic Data, and Reference Types

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

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

Compiler Construction

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

Memory and C++ Pointers

Memory and Addresses. Pointers in C. Memory is just a sequence of byte-sized storage devices.

Chapter 5 Names, Binding, Type Checking and Scopes

CS558 Programming Languages

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

TYPES, VALUES AND DECLARATIONS

Class Information ANNOUCEMENTS

nptr = new int; // assigns valid address_of_int value to nptr std::cin >> n; // assigns valid int value to n

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


Dynamic Allocation in C

Names, Bindings, Scopes

INITIALISING POINTER VARIABLES; DYNAMIC VARIABLES; OPERATIONS ON POINTERS

CPSC 213. Introduction to Computer Systems. Instance Variables and Dynamic Allocation. Unit 1c

CS558 Programming Languages. Winter 2013 Lecture 3

Recap: Pointers. int* int& *p &i &*&* ** * * * * IFMP 18, M. Schwerhoff

CS 415 Midterm Exam Spring 2002

G Programming Languages - Fall 2012

CS162 - POINTERS. Lecture: Pointers and Dynamic Memory

Semantic actions for expressions

Types and Type Inference

Pointers, Arrays and C-Strings

Binding and Variables

Pointers (continued), arrays and strings

Intro to C: Pointers and Arrays

Chapter 5. Names, Bindings, and Scopes

Type Checking and Type Inference

Programming Language Concepts Scoping. Janyl Jumadinova January 31, 2017

CSE 307: Principles of Programming Languages

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

CPSC 427a: Object-Oriented Programming

Homework #3 CS2255 Fall 2012

Pointers (continued), arrays and strings

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

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

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

Arrays. Returning arrays Pointers Dynamic arrays Smart pointers Vectors

CS558 Programming Languages

CSC 211 Intermediate Programming. Arrays & Pointers

From Java to C++ From Java to C++ CSE250 Lecture Notes Weeks 1 2, part of 3. Kenneth W. Regan University at Buffalo (SUNY) September 10, 2009

CS 314 Principles of Programming Languages

Run-time Environments - 3

CONTENTS: Array Usage Multi-Dimensional Arrays Reference Types. COMP-202 Unit 6: Arrays

Types and Type Inference

Introducing C++ to Java Programmers

Modern Programming Languages. Lecture Java Programming Language. An Introduction

CMSC 330: Organization of Programming Languages

Lecture Notes on Memory Management

Pointers and Memory 1

Memory, Data, & Addressing II CSE 351 Spring

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

names names identifiers variables subroutines constants

CS558 Programming Languages

Dynamic Allocation in C

See the CS 2704 notes on C++ Class Basics for more details and examples. Data Structures & OO Development I

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

Programming Methodology

Types, Type Inference and Unification

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

Lecture Notes on Memory Management

CMSC 330: Organization of Programming Languages

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

Chapter 13: Reference. Why reference Typing Evaluation Store Typings Safety Notes

Chapter 1: Object-Oriented Programming Using C++

a data type is Types

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

Advances in Programming Languages: Regions

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

Pointers II. Class 31

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

CS 230 Programming Languages

CSE 100: C++ TEMPLATES AND ITERATORS

CS321 Languages and Compiler Design I Winter 2012 Lecture 13

CSE 504: Compilers. Expressions. R. Sekar

Pointers and Arrays CS 201. This slide set covers pointers and arrays in C++. You should read Chapter 8 from your Deitel & Deitel book.

StackVsHeap SPL/2010 SPL/20

G Programming Languages - Fall 2012

Pierce Ch. 3, 8, 11, 15. Type Systems

Functions and Parameter Passing

C11: Garbage Collection and Constructors

CS 61C: Great Ideas in Computer Architecture C Pointers. Instructors: Vladimir Stojanovic & Nicholas Weaver

Transcription:

CSE 307: Principles of Programming Languages Variables and Constants R. Sekar 1 / 22 Topics 2 / 22 Variables and Constants Variables are stored in memory, whereas constants need not be. Value of variables can change at runtime. Variables have a location (l-value) and value (r-value). Constants have a value, but no location. 3 / 22

Constants Constants may some times be stored in memory If so, they have r-values but not l-values Since values stored in constants cannot be changed, there is no use in accessing l-values Thus constants have a Value semantics 4 / 22 Values and Constants Values are quantities manipulated by a program (e.g. integers, strings, data structures, etc.) Constants have a fixed value for the duration of its existence in a program. Constants in a program may be Literals: unnamed values specified using a particular representation. e.g.: 42 "Markov" 0x2eff Symbolic: names associated with fixed values. e.g. const int n = 100; static final int limit = 1024 5 / 22 Binding Time of Constants Compile-time const int n = 100; Binding of n (to value 100) is known at compile time. Load-time static final Date d = new Date(); Constant d is bound to the value of today s date at load time. Execution-time int f(int x) { const int y = x+1;...} Constant y is bound to its value at execution time! Note that y is local to f and refers to different entities for each invocation of f. The above declaration says that y will be constant for any particular invocation. 6 / 22

Variables Variables are associated with locations in Store (memory) Representation of variables (for explanations only): Variable Location Value The stored values are changed through assignments: e.g. x = y; The value stored at the location associated with y is copied to the location associated with x 7 / 22 L-value, R-value and Assignment In an assignment x = y we refer to l-value of x on the lhs ( l for location or lhs of assignments) r-value of y on the rhs ( r for right-hand-side of assignments) Storage semantics: update location of x with the value of y Accessing a value stored at a location is called dereferencing. C/C++/Java: l-values of variables on rhs are implicitly dereferenced to get their r-values. In ML, dereferencing should be explicit, as in x :=!y 8 / 22 Pointers C/C++ address-of operation to explicitly turn a reference into a pointer. e.g. &x evaluates to the location of x. Example: int x; // x's location stores int int *y; // y's location stores // pointers to int x = 20; y = &x; x 20 y The * operator is used to dereference a pointer e.g. in the above example, the value stored at *y is 20 9 / 22

L-value and R-value (Continued) Pointer semantics x simply points to y more efficient if the size of y is large but causes confusion in languages with assignment Java uses storage semantics for basic types, and pointer semantics for objects C/C++ use value semantics for all types In a language free of side-effects (i.e., memory updates), both semantics are equivalent. 10 / 22 Arrays Vs Pointers in C In C, arrays are similar to pointers int a[5]; int *b a and b have the same type, but semantically, they differ b = a is allowed, but a = b is not! the l-value of a cannot be changed (it is a const) 11 / 22 Arrays vs. Pointers in C *a=3 and *b=3 have very different effects For this to work correctly, b should have been previously initialized to hold a valid pointer value 12 / 22

Garbage Location that has been allocated, but no longer accessible int *x = new int; *x = 5; int y = 3; x = &y; 13 / 22 Garbage (Continued) Accumulation of garbage can lead to programs running out of memory eventually But no immediate adverse impact on program correctness of program is unaffected by garbage A program that produces garbage is said to have memory leaks 14 / 22 Dangling Pointer A pointer that points to memory that has been deallocated Consider: int *x, *y, *z; x = new int; *x = 3; y= x delete x; x = NULL; z = new int; *z = 5; *y = 2; 15 / 22

Dangling Pointer (Continued) Dangling pointers have an immediate impact on correctness they cause program to fail Failure may be immediate access through NULL pointer or be delayed corruption of data structures reached through dangling pointers 16 / 22 Dangling Pointer Vs. Garbage As compared to garbage, dangling pointers cause much more serious errors So, it is safer to never free memory But programs will run out of memory after a period of time Not an issue for programs that run for short times To avoid this, can use garbage collection automatically release unreachable memory used in OCAML, Java garbage collection is much harder for languages with weak type systems (e.g., C and C++). 17 / 22 Aliases Alias: Two variables have the same l-value C does not support references, but C++ does Use the syntax <typename>&: int& y References have to be initialized with their l-value int x = 1; int& y = x; 18 / 22

Aliases x and y are aliased they both have same l-value when two variables are aliased, assignments to one variable have the side-effect of changing the r-value of the other variable side-effects cause confusion They should be used sparingly Aliasing should be used very carefully 19 / 22 Aliases (Continued) Aliases may be created using pointer variables as well int *x = NULL; 20 / 22 Aliases (Continued) x = new int; *x = 4; 21 / 22

Aliases (Continued) int *y; y = x; 22 / 22