Download presentation
Presentation is loading. Please wait.
Published byElmer Daniel Modified over 9 years ago
1
1 CS 403 - Programming Languages Class 09 September 21, 2000
2
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
3
CS 403, Class 08 Slide # 3 Chapter 5 Data Types
4
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.
5
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
6
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?
7
CS 403, Class 08 Slide # 7 Fundamental Pointer Operations 1.Assignment of an address to a pointer 2.References (explicit versus implicit dereferencing)
8
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
9
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.
10
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
11
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)
12
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]
13
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))
14
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
15
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
16
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
17
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.
18
CS 403, Class 08 Slide # 18 Chapter 6 Expressions and the Assignment Statement
19
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
20
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?
21
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. +, -
22
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.
23
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
24
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 */
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.