1 CS 403 - Programming Languages Class 09 September 21, 2000.

Slides:



Advertisements
Similar presentations
Chapter 6 Data Types
Advertisements

ISBN Chapter 7 Expressions and Assignment Statements.
CS 355 – PROGRAMMING LANGUAGES Dr. X. Topics Introduction Arithmetic Expressions Overloaded Operators Type Conversions Relational and Boolean Expressions.
Various languages….  Could affect performance  Could affect reliability  Could affect language choice.
COEN Expressions and Assignment
CS 355 – Programming Languages
Chapter 7 Expressions and Assignment Statements. Copyright © 2012 Addison-Wesley. All rights reserved.1-2 Chapter 7 Topics Introduction Arithmetic Expressions.
ISBN Chapter 7 Expressions and Assignment Statements.
1 Chapter 7: Expressions Expressions are the fundamental means of specifying computations in a programming language To understand expression evaluation,
CSE 452: Programming Languages Expressions and Control Flow.
ISBN Chapter 7 Expressions and Assignment Statements.
ISBN Chapter 7 Expressions and Assignment Statements.
ISBN Lecture 07 Expressions and Assignment Statements.
Expressions, Evaluation and Assignments
College of Computer Science and Engineering
Copyright © 1998 by Addison -Wesley Longman, Inc. 1 Chapter 6 Arithmetic Expressions - Their evaluation was one of the motivations for the development.
1-1 University of Hail College of Computer Science and Engineering Department of computer Science and Software Engineering Course: ICS313: Fundamentals.
7-1 Chapter 7: Expressions and Assignment Statements Arithmetic Expressions –the fundamental means of specifying computations in a programming language.
Chapter 7 Expressions and Assignment Statements. Chapter 7 Topics 1-2 Introduction Arithmetic Expressions Overloaded Operators Type Conversions Relational.
Chapter 7 Expressions and Assignment Statements. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Arithmetic Expressions Arithmetic evaluation.
COMP4730/2002/lec7/H.Melikian Arithmetic Expressions Overloaded Operators Type Conversations Relational and Boolean Expressions Short Circuit Evaluation.
CS 363 Comparative Programming Languages Expressions and Assignment Statements.
C H A P T E R S E V E N Expressions and Assignment Statements.
ISBN 0-321— Chapter 6 sections 1-4, 9 Primitive Data Types Numbers Strings Ordinal Types Pointers.
1 CS Programming Languages Class 07 September 14, 2000.
Names Variables Type Checking Strong Typing Type Compatibility 1.
5-1 Chapter 5: Names, Bindings, Type Checking, and Scopes Variables The Concept of Binding Type Checking Strong Typing Type Compatibility Scope and Lifetime.
Introduction A variable can be characterized by a collection of properties, or attributes, the most important of which is type, a fundamental concept in.
1 Records Record aggregate of data elements –Possibly heterogeneous –Elements/slots are identified by names –Elements in same fixed order in all records.
Programming Languages and Paradigms Imperative Programming.
Arithmetic Expressions
Expressions and Assignment Statements
Chapter 7 Expressions and Assignment Statements. Copyright © 2009 Addison-Wesley. All rights reserved.1-2 Chapter 7 Topics Introduction Arithmetic Expressions.
ISBN Chapter 7 Expressions and Assignment Statements.
Chapter 7 © 1998 by Addison -Wesley Longman, Inc Arithmetic Expressions - Their evaluation was one of the motivations for the development of the.
Chapter 7 Expressions and Assignment Statements. Outline Introduction Arithmetic Expressions Overloaded Operators Type Conversions Assignment Statements.
ISBN Chapter 7 Expressions and Assignment Statements.
ISBN Chapter 6 Data Types Pointer Types Reference Types Memory Management.
March 31, ICE 1341 – Programming Languages (Lecture #11) In-Young Ko Programming Languages (ICE 1341) Lecture #11 Programming Languages (ICE 1341)
Chapter 6 © 2002 by Addison Wesley Longman, Inc Introduction - Evolution of Data Types: FORTRAN I (1957) - INTEGER, REAL, arrays … Ada (1983) -
ISBN Chapter 5 Names, Bindings, Type Checking, and Scopes.
Copyright © 1998 by Addison Wesley Longman, Inc. 1 Chapter 5 Evolution of Data Types: FORTRAN I (1956) - INTEGER, REAL, arrays … Ada (1983) - User can.
1 CS Programming Languages Class 10 September 26, 2000.
ISBN Chapter 7 Expressions and Assignment Statements.
Chapter 7 Expressions and Assignment Statements. Copyright © 2012 Addison-Wesley. All rights reserved.1-2 Chapter 7 Topics Introduction Arithmetic Expressions.
7-1/27 Chapter 7 Expressions and Assignment Statements Introduction Arithmetic Expressions Overloaded Operators Type Conversions Relational and Boolean.
Data Types Chapter 6: Data Types Lectures # 13. Topics Chapter 6: Data Types 2 Introduction Primitive Data Types Character String Types Array Types Associative.
Expressions and Assignment Statements
Object Lifetime and Pointers
Expressions and Assignment Statements
7.2 Arithmetic Expressions
Data Types In Text: Chapter 6.
Expressions and Assignment Statements
Expressions and Assignment Statements
Records Design Issues: 1. What is the form of references?
Concepts of programming languages
Expressions and Assignment Statements
Expressions and Assignment Statements
Arithmetic Expressions
Expressions and Assignment Statements
Expressions and Assignment Statements
College of Computer Science and Engineering
Expressions and Assignment Statements
Expression and Asignment Statements
Chapter 7 Expressions and Assignment Statements.
Chapter 7: Expressions and Assignment Statements Sangho Ha
PRESENTED BY ADNAN M. UZAIR NOMAN
Expressions and Assignment Statements
Expressions and Assignment Statements
Presentation transcript:

1 CS Programming Languages Class 09 September 21, 2000

CS 403, Class 08 Slide # 2 Today’s Agenda Finish Chapter 5: Data Types: Pointers Assignment: Read chapter 5 for today. Read chapter 6 for Tuesday

CS 403, Class 08 Slide # 3 Chapter 5 Data Types

CS 403, Class 08 Slide # 4 Evaluation of Sets If a language does not have sets, they must be simulated, either with enumerated types or with arrays. Arrays are more flexible than sets, but have much slower operations.

CS 403, Class 08 Slide # 5 Pointers A pointer type is a type in which the range of values consists of memory addresses and a special value, nil (or null) Uses: 1.Addressing flexibility 2.Dynamic storage management

CS 403, Class 08 Slide # 6 Pointer Design Issues 1.What is the scope and lifetime of pointer variables? 2.What is the lifetime of heap-dynamic variables? 3.Are pointers restricted to pointing at a particular type? 4.Are pointers used for dynamic storage management, indirect addressing, or both? 5.Should a language support pointer types, reference types, or both?

CS 403, Class 08 Slide # 7 Fundamental Pointer Operations 1.Assignment of an address to a pointer 2.References (explicit versus implicit dereferencing)

CS 403, Class 08 Slide # 8 Problems with pointers 1.Dangling pointers (dangerous) A pointer points to a heap-dynamic variable that has been deallocated Creating one: 1.Allocate a heap-dynamic variable and set a pointer to point at it. 2.Set a second pointer to the value of the first pointer 3.Deallocate the heap-dynamic variable,using the first pointer

CS 403, Class 08 Slide # 9 Problems with Pointers 2. Lost Heap-Dynamic Variables (wasteful) A heap-dynamic variable that is no longer referenced by any program pointer. Creating one:  Pointer p1 is set to point to a newly created heap- dynamic variable 2. p1 is later set to point to another newly created heap- dynamic variable The process of losing heap-dynamic variables is called memory leakage.

CS 403, Class 08 Slide # 10 Pointer Examples 1.Pascal: used for dynamic storage management only Explicit dereferencing Dangling pointers are possible ( dispose ) Dangling objects are also possible

CS 403, Class 08 Slide # 11 Pointer Examples 2. Ada: a little better than Pascal and Modula-2 Some dangling pointers are disallowed because dynamic objects can be automatically deallocated at the end of pointer's scope All pointers are initialized to null Similar dangling object problem (but rarely happens)

CS 403, Class 08 Slide # 12 Pointer Examples 3. C and C++ Used for dynamic storage management and addressing Explicit dereferencing and address-of operator Can do address arithmetic in restricted forms void * - can point to any type and can be type checked (cannot be dereferenced) Domain type need not be fixed ( void * ) e.g. float stuff[100]; float *p; p = stuff; *(p+5) is equivalent to stuff[5] and p[5] *(p+i) is equivalent to stuff[i] and p[i]

CS 403, Class 08 Slide # 13 Pointer Examples 4. FORTRAN 90 Pointers - Can point to heap and non-heap variables - Implicit dereferencing - Special assignment operator for non- dereferenced references, e.g. REAL, POINTER :: ptr (POINTER is an attribute) ptr => target (where target is either a pointer or a non- pointer with the TARGET attribute))

CS 403, Class 08 Slide # 14 Pointer Examples 4. FORTRAN 90 Pointers (cont’d) - The TARGET attribute is assigned in the declaration, as in: INTEGER, TARGET :: NODE - There is a special assignment when dereferencing is not wanted e.g., pointer => target

CS 403, Class 08 Slide # 15 Pointer Examples 5. C++ Reference Types Constant pointers that are implicitly dereferenced Used for parameters Advantages of both pass-by-reference and pass-by-value

CS 403, Class 08 Slide # 16 Pointer Examples 6. Java - Only references No pointer arithmetic Can only point at objects (which are all on the heap) No explicit deallocator (garbage collection is used) Means there can be no dangling references Dereferencing is always implicit

CS 403, Class 08 Slide # 17 Evaluation of pointers 1.Dangling pointers and dangling objects are problems, as is heap management. 2.Pointers are like goto's--they widen the range of cells that can be accessed by a variable. 3.Pointers are necessary--so we can't design a language without them.

CS 403, Class 08 Slide # 18 Chapter 6 Expressions and the Assignment Statement

CS 403, Class 08 Slide # 19 Arithmetic Expressions Their evaluation was one of the motivations for the development of the first programming languages - Arithmetic expressions consist of operators, operands, parentheses, and function calls

CS 403, Class 08 Slide # 20 Design issues for arithmetic expressions 1. What are the operator precedence rules? 2. What are the operator associativity rules? 3. What is the order of operand evaluation? 4. Are there restrictions on operand evaluation side effects? 5. Does the language allow user-defined operator overloading? 6. What mode mixing is allowed in expressions?

CS 403, Class 08 Slide # 21 Operator Precedence Def: The operator precedence rules for expression evaluation define the order in which “adjacent” operators of different precedence levels are evaluated (“adjacent” means they are separated by at most one operand - Typical precedence levels 1. parentheses 2. unary operators 3. ** (if the language supports it) 4. *, / 5. +, -

CS 403, Class 08 Slide # 22 Associativity Def: The operator associativity rules for expression evaluation define the order in which adjacent operators with the same precedence level are evaluated Typical associativity rules Left to right, except **, which is right to left Sometimes unary operators associate right to left (e.g., FORTRAN) Precedence and associativity rules can be overriden with parentheses.

CS 403, Class 08 Slide # 23 Operand Evaluation Operand evaluation order The process: 1. Variables: just fetch the value 2. Constants: sometimes a fetch from memory sometimes the constant is in the machine language instruction 3. Parenthesized expressions: evaluate all operands and operators first 4. Function references: The case of most interest! Order of evaluation is crucial Functional side effects - when a function changes a two-way parameter or a nonlocal variable

CS 403, Class 08 Slide # 24 Functional Side Effects -When a function referenced in an expression alters another operand of the expression e.g., for a parameter change: a = 10; b = a + fun(&a); /* Assume that fun changes its parameter */