References and pointers

Similar documents
NOTE: Answer ANY FOUR of the following 6 sections:

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

G Programming Languages - Fall 2012

Chapter 5 Names, Binding, Type Checking and Scopes

PROCEDURES, METHODS AND FUNCTIONS

CSC 533: Organization of Programming Languages. Spring 2005

CS 345. Functions. Vitaly Shmatikov. slide 1

G Programming Languages Spring 2010 Lecture 4. Robert Grimm, New York University

CS558 Programming Languages

Special Topics: Programming Languages

Chapter 5. Names, Bindings, and Scopes

Names, Bindings, Scopes

[0569] p 0318 garbage

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

Chapter 8. Fundamental Characteristics of Subprograms. 1. A subprogram has a single entry point

Concepts Introduced in Chapter 7

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

9. Subprograms. 9.2 Fundamentals of Subprograms

Weeks 6&7: Procedures and Parameter Passing

Chapter 9 Subprograms

Implementing Subprograms

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

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

Imperative Programming

COP4020 Programming Languages. Subroutines and Parameter Passing Prof. Robert van Engelen

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

Binding and Variables

Final-Term Papers Solved MCQS with Reference

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

Programming Languages: Lecture 11

Chapter 5 Names, Bindings, Type Checking, and Scopes

CS 415 Midterm Exam Spring 2002

Chapter 5 Names, Bindings, Type Checking, and Scopes

Imperative Languages!

Programmiersprachen (Programming Languages)

UNIT V Sub u P b ro r g o r g a r m a s

Outline. Programming Languages 1/16/18 PROGRAMMING LANGUAGE FOUNDATIONS AND HISTORY. Current

Programming Languages, Summary CSC419; Odelia Schwartz

Lecture 4 Memory Management

CPSC 3740 Programming Languages University of Lethbridge. Data Types

CS 314 Principles of Programming Languages

Programming Languages

Advances in Programming Languages: Regions

TYPES, VALUES AND DECLARATIONS

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

Principles of Programming Languages

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

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

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

CS558 Programming Languages

Organization of Programming Languages CS 3200/5200N. Lecture 09

What goes inside when you declare a variable?

Data Types In Text: Ch C apter 6 1

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

CE221 Programming in C++ Part 2 References and Pointers, Arrays and Strings

CS 3360 Design and Implementation of Programming Languages. Exam 1

Programming Languages Third Edition. Chapter 7 Basic Semantics

10. Abstract Data Types

Attributes of Variable. Lecture 13: Run-Time Storage Management. Lifetime. Value & Location

COP4020 Fall 2006 Final Exam

Pass by Value. Pass by Value. Our programs are littered with function calls like f (x, 5).

G Programming Languages - Fall 2012

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

CSCI312 Principles of Programming Languages!

Run-time Environments -Part 1

Run Time Environment. Activation Records Procedure Linkage Name Translation and Variable Access

Chapter-11 POINTERS. Important 3 Marks. Introduction: Memory Utilization of Pointer: Pointer:

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

Principles of Programming Languages

Run Time Environments

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

Programming Languages & Paradigms PROP HT Course Council. Subprograms. Meeting on friday! Subprograms, abstractions, encapsulation, ADT

SYSC 2006 C Winter 2012

Subprograms. Bilkent University. CS315 Programming Languages Pinar Duygulu

Lecture 13: Complex Types and Garbage Collection

Programming Languages

Names, Scopes, and Bindings II. Hwansoo Han

INITIALISING POINTER VARIABLES; DYNAMIC VARIABLES; OPERATIONS ON POINTERS

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

Code Generation & Parameter Passing

Chap. 8 :: Subroutines and Control Abstraction

CS 314 Principles of Programming Languages. Lecture 13

School of Science and Technology

CMSC 4023 Chapter 9. Fundamentals of Subprograms Introduction

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

Informal Semantics of Data. semantic specification names (identifiers) attributes binding declarations scope rules visibility

Chapter 10. Implementing Subprograms ISBN

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


Informatica 3 Syntax and Semantics

Special Topics: Programming Languages

Chapter 8 ( ) Control Abstraction. Subprograms Issues related to subprograms How is control transferred to & from the subprogram?

Agenda. Peer Instruction Question 1. Peer Instruction Answer 1. Peer Instruction Question 2 6/22/2011

(Refer Slide Time: 1:36)

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

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

One Ring to rule them all, One Ring to bring them all,

UNIT 3

CSE 307: Principles of Programming Languages

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

Transcription:

References and pointers Pointers are variables whose value is a reference, i.e. an address of a store location. Early languages (Fortran, COBOL, Algol 60) had no pointers; added to Fortran 90. In Pascal, Ada, C and C++, we can declare pointer variables. In C and C++, we can also do arithmetic on pointer variables. In Java, objects are always accessed via references (pointers), but we do not explicitly declare pointer variables. 121

References and pointers With pointer (reference) variables, there are two declaration-reference bindings to deal with, the allocation of space to the: pointer variable, the item the pointer variable is pointing at. 122

References and pointers Consider the effect of the following declarations: int i; //Java, C, C++ int* ipoint; // C, C++ What about these statements? ipoint = new int; // C++ int* ipoint = new int; 123

References and pointers What is the effect of this statement? *ipoint = 27; //C When we leave the method in which ipoint is declared, the location it is pointing at becomes inaccessible. It is now garbage. Why? 124

References and pointers C and C++ allow more complex assignments (assuming that i is an integer variable): ipoint = &i; There is no equivalent of the & operator in Java. What is the value of i after execution of: i = 34; *ipoint = 27; What is ipoint pointing to after ipoint = ipoint + 1; // C and C++ 125

Garbage When space for variables is allocated dynamically, garbage can be created. Suppose we have: int* ipoint = new int; *ipoint = 34; ipoint = new int; What has happened to the location containing 34? 126

Dangling reference Suppose we have: int* ipoint = new int; int* another = ipoint; Both another and ipoint now point to the same store location. This is known as aliasing. 127

Dangling reference delete ipoint; // C ++ ipoint = new int; What is another pointing to? We have a dangling reference. 128

Dangling reference Dangling references are a MAJOR problem. Leads to programs giving wrong results. Why? Example of the problem with aliases. Dangling references are much more serious than garbage. 129

Garbage Collection Space for dynamic variables organised as a free space list in what is known as the heap. Call of new takes the required amount of space from the free space list, while a call of delete in C++ returns it so that it can be re-used later. In Java, a garbage collector automatically determines store locations which have been allocated, but which are no longer accessible. It then collects them together and returns them to the free space list - no need for delete. This removes the problem of dangling references. 130

Pointers to objects In Pascal, Ada, C and C++ we can declare pointers to any type. Eg. // C ++ Pair* p = new Pair(43, 5); What does the * tell us? In Java, the equivalent declaration is: Pair p = new Pair(43, 5); 131

Objects In C++, we can declare a Pair object. Pair obj(43, 5); Space for the Pair object is allocated on block entry. Suppose that class Pair had a void method mkbigger: obj.mkbigger(); 132

Calling methods Recall p is a reference to a Pair object. In Java: p.mkbigger(); In C++ : (*p).mkbigger(); This is rather messy and so C++ has the syntactic sugar: p->mkbigger(); 133

Arrays Let us look again at arrays. Example: int[] a = new int[10]; //Java int* a = new int[10]; //C, C++ Space allocated on the stack or the heap? We access the elements of the array as: a[0], a[1], a[j] etc. 134

Arrays What about: int aa[10]; //C, C++ What is the effect of: int* b; b = aa; We could instead have written: b = &aa[0] 135

Arrays We can now access the array elements either as: aa[0], aa[1],... or as: b[0], b[1],... Also, instead of writing: aa[0] = 17; or b[0] = 17; we can write: *aa = 17; or *b = 17; i.e. the element pointed to by b is given the value 17. 136

Arithmetic on pointers C and C++ allow us to do arithmetic on pointers. Hence, instead of: for (int i = 0; i < 10; i++) b[i] = 0; we can write: for (int i = 0; i < 10; i++) *b++ = 0; Each time round the loop, the ++ causes b to point to the next element. This gives very fast access to arrays. 137

Pitfalls When using pointers, we must ensure that they are pointing to allocated space. Consider: int aa[10]; int* b = aa; int* c = new int[10]; Where is the storage allocated? How much is allocated? What happens when we leave the block? 138

Pitfalls There are some common, but subtle pitfalls. Consider: int* fun() { int aa[10];... return aa; // wrong!! } The function returns a pointer to an int and so this is syntactically OK. Why might this go wrong? 139

Reserving space Better: int* fun() { int* aa = new int[10];... return aa; } As space for the array is now allocated on the heap, it remains in existence after we leave fun. 140

Parameters Why use parameters to methods? How do pure functions differ? 141

Object-oriented languages Remember our BankAccount example. In Java, we had: bk1.deposit(6); We pass the value 6 to be deposited in our bk1 object. In Pascal, we had: deposit(bk1, 6); We pass over the variable bk1 and the value 6 and an updated version of bk1 is passed back to us. In Java, bk1 was an implicit extra parameter. 142

Passing information in The mechanism used in Algol 60, Pascal, C, C+ + and Java is call by value. Suppose we have the C++ or Java declaration: void ex(int par) {... } and the call: target.ex(actual); What values are copied, linked, or new storage locations created? 143

Passing information in The mechanism used in Ada is called call by constant-value. This is like call-by-value except that par is like a local constant. The advantage of call by value and call by constantvalue is that we have a guarantee that the actual parameter is not modified by the call. The drawback is that a copy of the actual parameter has to be made. Consider what happens if the parameter is a large array. 144

Call by reference What happens when information is to be passed back? In call by reference, the address of the actual parameter is passed over. Within the subprogram, the formal parameter is an indirect reference to the actual parameter. Any change in the formal parameter immediately affects the actual parameter. This is the way that var parameters in Pascal and reference parameters in C++ are implemented. 145

Call by reference In C++, we have: void swap(int& first, int& second) { int temp; temp = first; first = second; second = temp; } // swap Assuming dee and dum are declared as integers, the call is: swap(dee, dum); Java does not have reference parameters. 146

Call by reference in C In C, all parameter passing is done using call-by-value. Call-by-reference is achieved using pointers: void swap(int* first, int* second) { } int temp; temp = *first; *first = *second; // what?? *second = temp; Method call: swap(&dee, &dum);// recall & means address of The syntax is different, but the C and the C++ versions of swap are implemented in exactly the same way. 147

Call by reference Parameters in Java are always passed by value. Primitive types and references are therefore handled differently. Example: Someclass p; Can values in the object referred to by p be changed? 148

Call by reference Consider what happens with the method: void update(someclass f) {... f.somechange();... } // fun and the method call: target.update(p); 149

Call by reference The advantage of call by reference with structured types is that less space is needed. In call by value with an array (or indeed any large data structure), a copy of the complete array is passed over. In call by reference, only a pointer to the array is copied over. Hence, many Pascal programmers use this method with arrays, even when the array is not going to be changed. 150

Call by reference However there is then the disadvantage that we do not have a guarantee that the actual parameter will remain unchanged. Also accessing a variable through a pointer is slower than accessing a variable directly (because we need more steps to get to the value). Also, aliasing - two references point to the same object/value. 151

Call by value-result The value of the actual parameter is passed over. Within the procedure, changes affect only the local copy. On procedure exit, the actual parameter is updated. Advantages might include clarity. But can be expensive when used with structured variables. Needed in distributed systems that do not have shared memory as references are not then possible. 152

Call by value result In Ada, scalar objects are passed by valueresult. The syntax is: procedure ex(par : in out integer); However, structured objects in Ada may by passed by reference. 153

Functions No major difference between languages. In Java, value returning methods correspond to functions. A function returns a value and is used in expressions, while a procedure is a statement in its own right. Functions should not have side-effects: if (f1(e) && f2(c))... Here, the effect of calling f1 should not affect the execution of f2. Functions should return a value and have no other effect. 154