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

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

Embed Size (px)

Citation preview

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

1

CS 403 - Programming Languages

Class 09September 21, 2000

Page 2: 1 CS 403 - Programming Languages Class 09 September 21, 2000

CS 403, Class 08 Slide # 2

Today’s Agenda

Finish Chapter 5: Data Types: PointersAssignment: Read chapter 5 for today. Read chapter 6 for Tuesday

Page 3: 1 CS 403 - Programming Languages Class 09 September 21, 2000

CS 403, Class 08 Slide # 3

Chapter 5

Data Types

Page 4: 1 CS 403 - Programming Languages Class 09 September 21, 2000

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.

Page 5: 1 CS 403 - Programming Languages Class 09 September 21, 2000

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 flexibility2. Dynamic storage management

Page 6: 1 CS 403 - Programming Languages Class 09 September 21, 2000

CS 403, Class 08 Slide # 6

Pointer Design Issues1.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?

Page 7: 1 CS 403 - Programming Languages Class 09 September 21, 2000

CS 403, Class 08 Slide # 7

Fundamental Pointer Operations

1.Assignment of an address to a pointer2.References (explicit versus implicit

dereferencing)

Page 8: 1 CS 403 - Programming Languages Class 09 September 21, 2000

CS 403, Class 08 Slide # 8

Problems with pointers1. 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 pointer3. Deallocate the heap-dynamic

variable,using the first pointer

Page 9: 1 CS 403 - Programming Languages Class 09 September 21, 2000

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:

1. 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.

Page 10: 1 CS 403 - Programming Languages Class 09 September 21, 2000

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

Page 11: 1 CS 403 - Programming Languages Class 09 September 21, 2000

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)

Page 12: 1 CS 403 - Programming Languages Class 09 September 21, 2000

CS 403, Class 08 Slide # 12

Pointer Examples3. 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]

Page 13: 1 CS 403 - Programming Languages Class 09 September 21, 2000

CS 403, Class 08 Slide # 13

Pointer Examples4. 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))

Page 14: 1 CS 403 - Programming Languages Class 09 September 21, 2000

CS 403, Class 08 Slide # 14

Pointer Examples4. 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

Page 15: 1 CS 403 - Programming Languages Class 09 September 21, 2000

CS 403, Class 08 Slide # 15

Pointer Examples5. C++ Reference Types

• Constant pointers that are implicitly dereferenced

• Used for parameters• Advantages of both pass-by-reference

and pass-by-value

Page 16: 1 CS 403 - Programming Languages Class 09 September 21, 2000

CS 403, Class 08 Slide # 16

Pointer Examples6. 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

Page 17: 1 CS 403 - Programming Languages Class 09 September 21, 2000

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.

Page 18: 1 CS 403 - Programming Languages Class 09 September 21, 2000

CS 403, Class 08 Slide # 18

Chapter 6

Expressions and the Assignment Statement

Page 19: 1 CS 403 - Programming Languages Class 09 September 21, 2000

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

Page 20: 1 CS 403 - Programming Languages Class 09 September 21, 2000

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?

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

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. +, -

Page 22: 1 CS 403 - Programming Languages Class 09 September 21, 2000

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.

Page 23: 1 CS 403 - Programming Languages Class 09 September 21, 2000

CS 403, Class 08 Slide # 23

Operand Evaluation

Operand evaluation orderThe process:1. Variables: just fetch the value2. 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

Page 24: 1 CS 403 - Programming Languages Class 09 September 21, 2000

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 */