ISBN 0-321-19362-8 Chapter 6 Data Types Pointer Types Reference Types Memory Management.

24
ISBN 0-321-19362-8 Chapter 6 Data Types •Pointer Types •Reference Types •Memory Management

Transcript of ISBN 0-321-19362-8 Chapter 6 Data Types Pointer Types Reference Types Memory Management.

Page 1: ISBN 0-321-19362-8 Chapter 6 Data Types Pointer Types Reference Types Memory Management.

ISBN 0-321-19362-8

Chapter 6

Data Types•Pointer Types•Reference Types•Memory Management

Page 2: ISBN 0-321-19362-8 Chapter 6 Data Types Pointer Types Reference Types Memory Management.

Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 6-2

Pointers and Reference Types

• For a pointer type, range of values is memory addresses and nil.

• Uses– Indirect addressing

– Dynamic storage management

Page 3: ISBN 0-321-19362-8 Chapter 6 Data Types Pointer Types Reference Types Memory Management.

Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 6-3

Design Issues

• Scope and lifetime of pointer• Lifetime of heap dynamic variable• Type checking• What uses are allowed• Support pointer or reference types

Page 4: ISBN 0-321-19362-8 Chapter 6 Data Types Pointer Types Reference Types Memory Management.

Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 6-4

Pointer Operations

• Assignment– Need to store an address in the variable

– Generally part of the allocation process for heap-dynamic variables

• Address of operation needed if indirect addressing is used

• Dereferencing– Get the data being pointed to

– Can be explicit or implicit

Page 5: ISBN 0-321-19362-8 Chapter 6 Data Types Pointer Types Reference Types Memory Management.

Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 6-5

Pointer Problems

1. Dangling pointers (dangerous)

2. Lost Heap-Dynamic Variables ( wasteful) 1. aka garbage

Page 6: ISBN 0-321-19362-8 Chapter 6 Data Types Pointer Types Reference Types Memory Management.

Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 6-6

Dangling pointers

• A pointer points to a heap-dynamic variable that has been deallocated

• Creating one (with explicit deallocation):a) Allocate a heap-dynamic variable and set a

pointer to point at it

b) Set a second pointer to the value of the first pointer

c) Deallocate the heap-dynamic variable, using the first pointer

Page 7: ISBN 0-321-19362-8 Chapter 6 Data Types Pointer Types Reference Types Memory Management.

Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 6-7

Lost Heap-Dynamic Variables

• A heap-dynamic variable that is no longer referenced by any program pointer

• Creating one:a) Pointer p1 is set to point to a newly created heap-

dynamic variable

b) 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 8: ISBN 0-321-19362-8 Chapter 6 Data Types Pointer Types Reference Types Memory Management.

Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 6-8

Pointers in Pascal

• used for dynamic storage management only– Explicit dereferencing (postfix ^)

– Dangling pointers are possible (dispose)

– Dangling objects are also possible

Page 9: ISBN 0-321-19362-8 Chapter 6 Data Types Pointer Types Reference Types Memory Management.

Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 6-9

Pointers in Ada

• A little better than Pascal– Some dangling pointers are disallowed because

dynamic objects can be automatically deallocated at the end of pointer's type scope

– All pointers are initialized to null

– Similar dangling object problem (but rarely happens, because explicit deallocation is rarely done)

Page 10: ISBN 0-321-19362-8 Chapter 6 Data Types Pointer Types Reference Types Memory Management.

Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 6-10

Pointers in C and C++

• Used for dynamic storage management and addressing

• Explicit dereferencing and address-of operator• Domain type need not be fixed (void *)

– void * - Can point to any type and can be type checked (cannot be dereferenced)

Page 11: ISBN 0-321-19362-8 Chapter 6 Data Types Pointer Types Reference Types Memory Management.

Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 6-11

Pointers in C and C++ (continued)

• Can do address arithmetic in restricted forms, 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]

(Implicit scaling)

Page 12: ISBN 0-321-19362-8 Chapter 6 Data Types Pointer Types Reference Types Memory Management.

Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 6-12

Pointers

The assignment operation j = *ptr

Page 13: ISBN 0-321-19362-8 Chapter 6 Data Types Pointer Types Reference Types Memory Management.

Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 6-13

Pointers in FORTRAN 90

• Can point to heap and non-heap variables• Implicit dereferencing• Pointers can only point to variables that have

the TARGET attribute• The TARGET attribute is assigned in the

declaration, as in: INTEGER, TARGET :: NODE

Page 14: ISBN 0-321-19362-8 Chapter 6 Data Types Pointer Types Reference Types Memory Management.

Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 6-14

Pointers FORTRAN 90 Pointers (continued)• A special assignment operator is used 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)

• This sets ptr to have the same value as target

Page 15: ISBN 0-321-19362-8 Chapter 6 Data Types Pointer Types Reference Types Memory Management.

Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 6-15

C++ Reference Types

• Constant pointers that are implicitly dereferenced– Used for parameters

• Advantages of both pass-by-reference and pass-by-value

Page 16: ISBN 0-321-19362-8 Chapter 6 Data Types Pointer Types Reference Types Memory Management.

Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 6-16

Pointers in 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: ISBN 0-321-19362-8 Chapter 6 Data Types Pointer Types Reference Types Memory Management.

Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 6-17

Evaluation of Pointers

• Dangling pointers and dangling objects are problems, as is heap management

• Pointers are like goto's--they widen the range of cells that can be accessed by a variable

• Pointers or references are necessary for dynamic data structures--so we can't design a language without them

Page 18: ISBN 0-321-19362-8 Chapter 6 Data Types Pointer Types Reference Types Memory Management.

Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 6-18

Pointer Implementation

• Representation of pointers and references– Large computers use single values

– Intel microprocessors use segment and offset

Page 19: ISBN 0-321-19362-8 Chapter 6 Data Types Pointer Types Reference Types Memory Management.

Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 6-19

Memory Management

• Avoiding dangling pointer problems1. Tombstone: extra heap cell that is a pointer to the

heap-dynamic variable• The actual pointer variable points only at tombstones

• When heap-dynamic variable deallocated, tombstone remains but set to nil

2. Locks and keys: Pointer values are represented as (key, address) pairs• Heap-dynamic variables are represented as variable plus

cell for integer lock value

• When heap-dynamic variable allocated, lock value is created and placed in lock cell and key cell of pointer

Page 20: ISBN 0-321-19362-8 Chapter 6 Data Types Pointer Types Reference Types Memory Management.

Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 6-20

Tombstones

Page 21: ISBN 0-321-19362-8 Chapter 6 Data Types Pointer Types Reference Types Memory Management.

Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 6-21

Heap management

• How do we avoid the problem of dangling pointers and garbage?

• Issues– Single-size cells vs. variable-size cells

– Reference counters (eager approach) vs. garbage collection (lazy approach)

Page 22: ISBN 0-321-19362-8 Chapter 6 Data Types Pointer Types Reference Types Memory Management.

Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 6-22

Reference counters

• Maintain a counter in every cell that store the number of pointers currently pointing at the cell

• Disadvantages– space required

– execution time required

– complications for cells connected circularly

Page 23: ISBN 0-321-19362-8 Chapter 6 Data Types Pointer Types Reference Types Memory Management.

Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 6-23

Garbage collection

• Allocate and disconnect until all available cells allocated; then begin gathering all garbage– Every heap cell has an extra bit used by collection

algorithm

– All cells initially set to garbage

– All pointers traced into heap, and reachable cells marked as not garbage

– All garbage cells returned to list of available cells

• Disadvantages: when you need it most, it works worst (takes most time when program needs most of cells in heap)

Page 24: ISBN 0-321-19362-8 Chapter 6 Data Types Pointer Types Reference Types Memory Management.

Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 6-24

Marking Algorithm