Eyvind W. Axelsen eyvinda@ifi.uio.no INF3110: Solutions to Problem Set 2 Scoping, Runtime Organisation, Parameter Passing Methods Eyvind W. Axelsen.

Slides:



Advertisements
Similar presentations
CSI 3120, Implementing subprograms, page 1 Implementing subprograms The environment in block-structured languages The structure of the activation stack.
Advertisements

Chapter 5 ( ) of Programming Languages by Ravi Sethi
1 5.3 Sub Procedures, Part II Passing by Value Passing by Reference Sub Procedures that Return a Single Value Debugging.
Lecture 16 Subroutine Calls and Parameter Passing Semantics Dragon: Sec. 7.5 Fischer: Sec Procedure declaration procedure p( a, b : integer, f :
1 Pointers A pointer variable holds an address We may add or subtract an integer to get a different address. Adding an integer k to a pointer p with base.
Procedures in more detail. CMPE12cCyrus Bazeghi 2 Procedures Why use procedures? Reuse of code More readable Less code Microprocessors (and assembly languages)
ISBN Chapter 10 Implementing Subprograms.
Slides prepared by Rose Williams, Binghamton University Chapter 13 Interfaces and Inner Classes.
Run-Time Storage Organization
Semantics of Calls and Returns
ISBN Chapter 10 Implementing Subprograms –Semantics of Calls and Returns –Implementing “Simple” Subprograms –Implementing Subprograms with.
1 CSCI 360 Survey Of Programming Languages 9 – Implementing Subprograms Spring, 2008 Doug L Hoffman, PhD.
Chapter 10 Implementing Subprograms. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Semantics of Call and Return The subprogram call and return.
INF5110: Mandatory Exercise 2 Eyvind W. Axelsen @eyvindwa Slides are partly based on.
ISBN Chapter 10 Implementing Subprograms.
Values, variables and types © Allan C. Milne v
Compiler Construction
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
Pointers and Dynamic Memory Allocation Copyright Kip Irvine 2003, all rights reserved. Revised 10/28/2003.
Run-Time Storage Organization Compiler Design Lecture (03/23/98) Computer Science Rensselaer Polytechnic.
CS536 Semantic Analysis Introduction with Emphasis on Name Analysis 1.
Chapter 8: Advanced Method Concepts. Understanding Parameter Types Mandatory parameter – An argument for it is required in every method call Four types.
7. Runtime Environments Zhang Zhizheng
10-1 Chapter 10: Implementing Subprograms The General Semantics of Calls and Returns Implementing “Simple” Subprograms Implementing Subprograms with Stack-Dynamic.
Implementing Subprograms
Building Programs from Existing Information Solutions for programs often can be developed from previously solved problems. Data requirements and solution.
ISBN Chapter 10 Implementing Subprograms.
Chapter 10 Chapter 10 Implementing Subprograms. Implementing Subprograms  The subprogram call and return operations are together called subprogram linkage.
ISBN Chapter 10 Implementing Subprograms.
Code Generation Instruction Selection Higher level instruction -> Low level instruction Register Allocation Which register to assign to hold which items?
Design issues for Object-Oriented Languages
CS314 – Section 5 Recitation 9
Implementing Subprograms
C arrays (Reek, Ch. 8) CS 3090: Safety Critical Programming in C.
Chapter 10 : Implementing Subprograms
Chapter 5: Enhancing Classes
Run-Time Environments Chapter 7
Implementing Subprograms
Implementing Subprograms
CSE 374 Programming Concepts & Tools
Implementing Subprograms
Methods Attributes Method Modifiers ‘static’
A Lecture for the c++ Course
Subprograms The basic abstraction mechanism.
Run-time organization
Run-Time Storage Organization
Constructor & Destructor
INF3110: Exercises Part 6 Object Orientation Comments and solutions
INF3110: Exercises Part 3 Comments and Solutions
Eyvind W. Axelsen INF3110: Exercises Part 7 Types and Object Orientation II Comments and solutions Eyvind W. Axelsen
Java Review: Reference Types
CSE 143 Lecture 9 References and Linked Nodes reading: 3.3; 16.1
CSC 253 Lecture 8.
Implementing Subprograms
Object Oriented Programming COP3330 / CGS5409
Const in Classes CSCE 121 J. Michael Moore.
Pass by Reference, const, readonly, struct
CSC 253 Lecture 8.
Activation Records and Function Calls
Implementing Subprograms
Variables and Their scope
Object Oriented Programming
Defining methods and more arrays
Parameter Passing in Java
UNIT V Run Time Environments.
Pointers Pointers point to memory locations
INF3110: Exercises Part 6 Object Orientation Comments and solutions
INF3110: Mandatory 1 and Exercises Part 1 Comments and Solutions
Implementing Subprograms
Presentation transcript:

Eyvind W. Axelsen eyvinda@ifi.uio.no INF3110: Solutions to Problem Set 2 Scoping, Runtime Organisation, Parameter Passing Methods Eyvind W. Axelsen eyvinda@ifi.uio.no @eyvindwa http://eyvinda.at.ifi.uio.no Slides are based on material from previous years, made by Weiqing Zhang and Henning Berg.

Problem 1 –Allocation Exercise 7.1 in Mitchell: Debug a program that (tries to) calculate the absolute value of an integer This exercise is about C specifics! The moral of this story is: know the semantics of your language before you try to program (things of importance) in it.

Why does the program not work? C does not require that variables are initialized (so the program compiles and runs) But: the inner absval declarations shadow the outer

Might not work - depends on compiler’s implementation! The behavior of such programs is left unspecified in C What might happen is that inner absVal will be written to the same location as the (uninitialized) outer absVal The value may thus be correct at the end of the program

For instance: { int arbitraryValue = -1; } This will allocate the arbitraryValue in the same place in memory as absVal. -1 is never a valid absolute value.

Problem 2 – Static and Dynamic scope x in line 5, and x, f(x) in line 10

Problem 3 – Scope and Lifetime

Problem 4 – Static Scope 1) * 2) ** 3) *

Problem 5 – Dynamic Scope

Problem 5 – Dynamic Scope

Problem 5 – Dynamic Scope

Problem 5 – Dynamic Scope

Problem 6 – Static Scope y is from "program", t is from "f1", w is from “f2”, z is from "program"

Problem 7 – Parameter passing In call by value-result, the actual parameter supplied by the caller is copied into the callee's formal parameter; the function is run; and the (possibly modified) formal parameter is then copied back to the caller – c2 wiki

Problem 8 – Parameter passing Exercise 7.4 a) in Mitchell: Call by value/ call by reference

Problem 8 – Parameter passing The following cases work: 1: power(x by value, y by value, z by ref) 2: power(x by value, y by ref, z by ref) 3: power(x by ref, y by value, z by ref) Why? Obviously z needs to be by ref, in order to return a value from the function 1: local x and y; harmless 2: since x is by value, changing the original value of a through its reference in y is harmless, and thus this is OK 3: x is never assigned to in the function, so passing this by ref is OK, as long as y is by value Having all three by ref will clearly not work (decrementing y would also change x)

Problem 9 – Parameter passing Exercise 7.7 in Mitchell

Problem 9 – Parameter passing