Copyright © 2006 Addison-Wesley. All rights reserved. 6-1 Subscript Bindings and Array Categories...

49
Copyright © 2006 Addison-Wesley. All rights reserved. 6-1 Subscript Bindings and Array Categories In some languages the lower bound of the subscript range is implicit, e.g. C-based languages – 0; Fortran 95 – 1. In some other languages, subscript ranges must be completely specified by the programmer.

description

Copyright © 2006 Addison-Wesley. All rights reserved. 6-3 Subscript Bindings and Array Categories –Stack-dynamic array: subscript ranges are dynamically bound, and storage allocation is done during run time. Once the ranges are bound and the storage allocated, they remain fixed during the lifetime of the variable. Advantage: flexibility, the size of an array need not be known until the array is about to be used. E.g.: Ada arrays –Fixed heap-dynamic array: subscript ranges and storage are dynamically bound during run time when the user program request them. Fixed when they are allocated. The storage is allocated from the heap. E.g.:C, C++, Java, C#

Transcript of Copyright © 2006 Addison-Wesley. All rights reserved. 6-1 Subscript Bindings and Array Categories...

Page 1: Copyright © 2006 Addison-Wesley. All rights reserved. 6-1 Subscript Bindings and Array Categories In some languages the lower bound of the subscript range.

Copyright © 2006 Addison-Wesley. All rights reserved. 6-1

Subscript Bindings and Array Categories• In some languages the lower bound of the

subscript range is implicit, e.g. C-based languages – 0; Fortran 95 – 1. In some other languages, subscript ranges must be completely specified by the programmer.

Page 2: Copyright © 2006 Addison-Wesley. All rights reserved. 6-1 Subscript Bindings and Array Categories In some languages the lower bound of the subscript range.

Copyright © 2006 Addison-Wesley. All rights reserved. 6-2

Subscript Bindings and Array Categories• Five categories of arrays

– Static Array: subscript ranges are statically bound; storage allocation is static (done before run-time). Advantage: efficiency, no dynamic allocation or deallocation is required. E.g.: C. C++ static variables

– Fixed stack-dynamic array: subscript ranges are statically bound, but the allocation is done at declaration elaboration time during execution. Advantage: space efficiency, share space among different subprograms. E.g.: arrays declared in C, C++ functions

Page 3: Copyright © 2006 Addison-Wesley. All rights reserved. 6-1 Subscript Bindings and Array Categories In some languages the lower bound of the subscript range.

Copyright © 2006 Addison-Wesley. All rights reserved. 6-3

Subscript Bindings and Array Categories– Stack-dynamic array: subscript ranges are dynamically bound, and storage allocation is done during run time. Once the ranges are bound and the storage allocated, they remain fixed during the lifetime of the variable. Advantage: flexibility, the size of an array need not be known until the array is about to be used. E.g.: Ada arrays

– Fixed heap-dynamic array: subscript ranges and storage are dynamically bound during run time when the user program request them. Fixed when they are allocated. The storage is allocated from the heap. E.g.:C, C++, Java, C#

Page 4: Copyright © 2006 Addison-Wesley. All rights reserved. 6-1 Subscript Bindings and Array Categories In some languages the lower bound of the subscript range.

Copyright © 2006 Addison-Wesley. All rights reserved. 6-4

Subscript Bindings and Array Categories– Heap-dynamic array: the binding of subscript ranges and the storage allocation is dynamic and can change any number of times during the array’s lifetime. Advantage: flexibility. E.g.: C# ArrayList, Perl, JavaScript

Page 5: Copyright © 2006 Addison-Wesley. All rights reserved. 6-1 Subscript Bindings and Array Categories In some languages the lower bound of the subscript range.

Copyright © 2006 Addison-Wesley. All rights reserved. 6-5

Subscript Bindings and Array Categories

Type Range Binding

Storage Allocation

Time of Allocation

Examples

Static Static Static Before run time

C, C++ static variables

Fixed stack-dynamic

Static Dynamic from stack, then fixed

Run time, declaration

Declared in C, C++ functions

Stack-dynamic

Dynamic Dynamic from stack, then fixed

Run time, when used

Ada

Fixed heap-dynamic

Dynamic Dynamic from heap, then fixed

Run time, when used

C, C++, Java, C#

Heap-dynamic

Dynamic Dynamic from heap

Run time, can grow and shrink

C#, Perl, JavaScript

Page 6: Copyright © 2006 Addison-Wesley. All rights reserved. 6-1 Subscript Bindings and Array Categories In some languages the lower bound of the subscript range.

Copyright © 2006 Addison-Wesley. All rights reserved. 6-6

Array Initialization

• Some language allow initialization at the time of storage allocation– C, C++, Java, C# exampleint list [] = {4, 5, 7, 83} – Character strings in C and C++char name [] = “freddie”;– Arrays of strings in C and C++char *names [] = {“Bob”, “Jake”, “Joe”];– Java initialization of String objectsString[] names = {“Bob”, “Jake”, “Joe”};

Page 7: Copyright © 2006 Addison-Wesley. All rights reserved. 6-1 Subscript Bindings and Array Categories In some languages the lower bound of the subscript range.

Copyright © 2006 Addison-Wesley. All rights reserved. 6-7

Arrays Operations• APL provides the most powerful array

processing operations for vectors and matrixes as well as unary operators (for example, to reverse column elements)

• Ada allows array assignment but also catenation

• Fortran provides elemental operations because they are between pairs of array elements– For example, + operator between two arrays

results in an array of the sums of the element pairs of the two arrays

Page 8: Copyright © 2006 Addison-Wesley. All rights reserved. 6-1 Subscript Bindings and Array Categories In some languages the lower bound of the subscript range.

Copyright © 2006 Addison-Wesley. All rights reserved. 6-8

Rectangular and Jagged Arrays

• A rectangular array is a multi-dimensioned array in which all of the rows have the same number of elements and all columns have the same number of elements– Supported by Fortran, Ada and C#.– myArray[3,7]

• A jagged matrix has rows with varying number of elements– Possible when multi-dimensioned arrays actually appear

as arrays of arrays– Supported by C, C++ and Java– myArray[3][7]

Page 9: Copyright © 2006 Addison-Wesley. All rights reserved. 6-1 Subscript Bindings and Array Categories In some languages the lower bound of the subscript range.

Copyright © 2006 Addison-Wesley. All rights reserved. 6-9

Slices

• A slice is some substructure of an array; nothing more than a referencing mechanism

• can be one row or one column• Slices are only useful in languages that

have array operations

Page 10: Copyright © 2006 Addison-Wesley. All rights reserved. 6-1 Subscript Bindings and Array Categories In some languages the lower bound of the subscript range.

Copyright © 2006 Addison-Wesley. All rights reserved. 6-10

Slice Examples

• Fortran 95Integer, Dimension (10) :: VectorInteger, Dimension (3, 3) :: MatInteger, Dimension (3, 3, 4) :: Cube

Vector (3:6) is a four element array

Page 11: Copyright © 2006 Addison-Wesley. All rights reserved. 6-1 Subscript Bindings and Array Categories In some languages the lower bound of the subscript range.

Copyright © 2006 Addison-Wesley. All rights reserved. 6-11

Implementation of Arrays

• Access function maps subscript expressions to an address in the array

• Access function for single-dimensioned arrays:address(list[k]) = address (list[lower_bound])

+ ((k-lower_bound) * element_size)

Page 12: Copyright © 2006 Addison-Wesley. All rights reserved. 6-1 Subscript Bindings and Array Categories In some languages the lower bound of the subscript range.

Copyright © 2006 Addison-Wesley. All rights reserved. 6-12

Accessing Multi-dimensioned Arrays• Must be mapped into a single dimensioned

memory• Two common ways:

– Row major order (by rows) – used in most languages

– E.g. 3 4 7 6 2 5 1 3 8 => 3,4,7,6,2,5,1,3,8

– column major order (by columns) – used in Fortran

– E.g. same as above =>3,6,1,4,2,3,7,5,8• When using a pointer to access the array

element one by one, need to know the major order

Page 13: Copyright © 2006 Addison-Wesley. All rights reserved. 6-1 Subscript Bindings and Array Categories In some languages the lower bound of the subscript range.

Copyright © 2006 Addison-Wesley. All rights reserved. 6-13

Locating an Element in a Multi-dimensioned Array•General format

Location (a[i,j]) = address of a [row_lb,col_lb] + (((i - row_lb) * n) + (j - col_lb)) * element_size

Page 14: Copyright © 2006 Addison-Wesley. All rights reserved. 6-1 Subscript Bindings and Array Categories In some languages the lower bound of the subscript range.

Copyright © 2006 Addison-Wesley. All rights reserved. 6-14

Compile-Time Descriptors

Single-dimensioned array Multi-dimensional array

Page 15: Copyright © 2006 Addison-Wesley. All rights reserved. 6-1 Subscript Bindings and Array Categories In some languages the lower bound of the subscript range.

Copyright © 2006 Addison-Wesley. All rights reserved.

Associative Arrays

• An associative array is an unordered collection of data elements that are indexed by an equal number of values called keys – User defined keys must be stored– A key value pair– Access the value through the key

• Design issues: What is the form of references to elements?

6-36

Page 16: Copyright © 2006 Addison-Wesley. All rights reserved. 6-1 Subscript Bindings and Array Categories In some languages the lower bound of the subscript range.

Copyright © 2006 Addison-Wesley. All rights reserved.

Associative Arrays in Perl• Called hashes• Names begin with %; literals are delimited by

parentheses%salaries = (“Gary" => 75000, “Perry" => 57000,

“Mary” => 55750,”Cedric” => 47850);• Subscripting is done using braces and keys

$salaries{“Perry"} = 58850;– Elements can be removed with delete

delete $salaries{“Gary"};- Can delete the entire hash

@salaries = ();• keys: return an array of keys• values: return an array of values

6-37

Page 17: Copyright © 2006 Addison-Wesley. All rights reserved. 6-1 Subscript Bindings and Array Categories In some languages the lower bound of the subscript range.

Copyright © 2006 Addison-Wesley. All rights reserved.

Record Types• A record is a possibly heterogeneous

aggregate of data elements in which the individual elements are identified by names

• Design issues:– What is the syntactic form of references to the

field? – Are elliptical references allowed?

6-38

Page 18: Copyright © 2006 Addison-Wesley. All rights reserved. 6-1 Subscript Bindings and Array Categories In some languages the lower bound of the subscript range.

Copyright © 2006 Addison-Wesley. All rights reserved.

Definition of Records in COBOL

• COBOL uses level numbers to show nested records; others use recursive definition01 EMP-REC. 02 EMP-NAME. 05 FIRST PICTURE X(20). 05 MID PICTURE X(10). 05 LAST PICTURE X(20). 02 HOURLY-RATE PICTURE 99V99.

20 alphanumeric chars

4 decimal digits with decimal points

in the middle

6-39

Page 19: Copyright © 2006 Addison-Wesley. All rights reserved. 6-1 Subscript Bindings and Array Categories In some languages the lower bound of the subscript range.

Copyright © 2006 Addison-Wesley. All rights reserved.

Definition of Records in Ada• Record structures are indicated in an

orthogonal waytype Emp_Rec_Type is record

First: String (1..20);Mid: String (1..10);Last: String (1..20);Hourly_Rate: Float;

end record;Emp_Rec: Emp_Rec_Type;

6-40

Page 20: Copyright © 2006 Addison-Wesley. All rights reserved. 6-1 Subscript Bindings and Array Categories In some languages the lower bound of the subscript range.

Copyright © 2006 Addison-Wesley. All rights reserved.

References to Records• COBOL

field_name OF record_name_1 OF ... OF record_name_ne.g. MID of EMP-NAME of EMP-REC

• Others (dot notation)record_name_1.record_name_2. ... record_name_n.field_name

e.g. Emp_Rec.Name• Fully qualified references must include all record names,

from the largest enclosing record to the specific field• Elliptical references allow leaving out record names as long

as the reference is unambiguous, for example in COBOLFIRST, FIRST OF EMP-NAME, and FIRST of EMP-REC are elliptical references to the employee’s first name

6-41

Page 21: Copyright © 2006 Addison-Wesley. All rights reserved. 6-1 Subscript Bindings and Array Categories In some languages the lower bound of the subscript range.

Copyright © 2006 Addison-Wesley. All rights reserved.

Operations on Records

• Assignment is very common if the types are identical

• Ada allows record comparison• Ada records can be initialized with

aggregate literals• COBOL provides MOVE CORRESPONDING

– Copies a field of the source record to the corresponding field in the target record if the target record has a field with the same name

6-42

Page 22: Copyright © 2006 Addison-Wesley. All rights reserved. 6-1 Subscript Bindings and Array Categories In some languages the lower bound of the subscript range.

Copyright © 2006 Addison-Wesley. All rights reserved.

Evaluation and Comparison to Arrays• Straight forward and safe design• Records are used when collection of data

values is heterogeneous• Access to array elements is much slower

than access to record fields, because subscripts are dynamic (field names are static)

6-43

Page 23: Copyright © 2006 Addison-Wesley. All rights reserved. 6-1 Subscript Bindings and Array Categories In some languages the lower bound of the subscript range.

Copyright © 2006 Addison-Wesley. All rights reserved.

Implementation of Record Type

Offset address relative to the beginning of the records is associated with each field

6-44

Page 24: Copyright © 2006 Addison-Wesley. All rights reserved. 6-1 Subscript Bindings and Array Categories In some languages the lower bound of the subscript range.

Copyright © 2006 Addison-Wesley. All rights reserved.

Unions Types• A union is a type whose variables are

allowed to store different type values at different times during execution

• Design issues – Should type checking be required?– Should unions be embedded in records?

6-45

Page 25: Copyright © 2006 Addison-Wesley. All rights reserved. 6-1 Subscript Bindings and Array Categories In some languages the lower bound of the subscript range.

Copyright © 2006 Addison-Wesley. All rights reserved.

Discriminated vs. Free Unions

• Fortran, C, and C++ provide union constructs in which there is no language support for type checking; the union in these languages is called free union

• Type checking of unions require that each union include a type indicator called a discriminant– The Union is called a discriminated union– Supported by Ada

6-46

Page 26: Copyright © 2006 Addison-Wesley. All rights reserved. 6-1 Subscript Bindings and Array Categories In some languages the lower bound of the subscript range.

Copyright © 2006 Addison-Wesley. All rights reserved.

Ada Union Types

type Shape is (Circle, Triangle, Rectangle);type Colors is (Red, Green, Blue);type Figure (Form: Shape) is record

Filled: Boolean;Color: Colors;case Form is

when Circle => Diameter: Float;when Triangle =>

Leftside, Rightside: Integer;Angle: Float;

when Rectangle => Side1, Side2: Integer;end case;

end record;

6-47

Page 27: Copyright © 2006 Addison-Wesley. All rights reserved. 6-1 Subscript Bindings and Array Categories In some languages the lower bound of the subscript range.

Copyright © 2006 Addison-Wesley. All rights reserved.

Ada Union Type Illustrated

A discriminated union of three shape variables

6-48

Page 28: Copyright © 2006 Addison-Wesley. All rights reserved. 6-1 Subscript Bindings and Array Categories In some languages the lower bound of the subscript range.

Copyright © 2006 Addison-Wesley. All rights reserved.

Evaluation of Unions

• Potentially unsafe construct– Do not allow type checking

• Java and C# do not support unions– Reflective of growing concerns for safety in

programming language

6-49

Page 29: Copyright © 2006 Addison-Wesley. All rights reserved. 6-1 Subscript Bindings and Array Categories In some languages the lower bound of the subscript range.

Copyright © 2006 Addison-Wesley. All rights reserved.

Pointer and Reference Types

• A pointer type variable has a range of values that consists of memory addresses and a special value, nil

• Provide the power of indirect addressing• Provide a way to manage dynamic

memory• A pointer can be used to access a location

in the area where storage is dynamically created (usually called a heap)

6-50

Page 30: Copyright © 2006 Addison-Wesley. All rights reserved. 6-1 Subscript Bindings and Array Categories In some languages the lower bound of the subscript range.

Copyright © 2006 Addison-Wesley. All rights reserved.

Design Issues of Pointers

• What are the scope of and lifetime of a pointer variable?

• What is the lifetime of a heap-dynamic variable?

• Are pointers restricted as to the type of value to which they can point?

• Are pointers used for dynamic storage management, indirect addressing, or both?

• Should the language support pointer types, reference types, or both?

6-51

Page 31: Copyright © 2006 Addison-Wesley. All rights reserved. 6-1 Subscript Bindings and Array Categories In some languages the lower bound of the subscript range.

Copyright © 2006 Addison-Wesley. All rights reserved.

Pointer Operations

• Two fundamental operations: assignment and dereferencing

• Assignment is used to set a pointer variable’s value to some useful address

• Dereferencing yields the value stored at the location represented by the pointer’s value– Dereferencing can be explicit or implicit– C++ uses an explicit operation via *j = *ptrsets j to the value located at ptr

6-52

Page 32: Copyright © 2006 Addison-Wesley. All rights reserved. 6-1 Subscript Bindings and Array Categories In some languages the lower bound of the subscript range.

Copyright © 2006 Addison-Wesley. All rights reserved.

Pointer Assignment Illustrated

The assignment operation j = *ptr

6-53

Page 33: Copyright © 2006 Addison-Wesley. All rights reserved. 6-1 Subscript Bindings and Array Categories In some languages the lower bound of the subscript range.

Copyright © 2006 Addison-Wesley. All rights reserved.

Problems with Pointers • Dangling pointers (dangerous)

– A pointer points to a heap-dynamic variable that has been de-allocatede.g.

int *arrayPtr1;int *arrayPtr2 = new int[100];arrayPtr1 = arrayPtr2;delete [] arrayPtr2; //arrayPtr1 is

dangling

6-54

Page 34: Copyright © 2006 Addison-Wesley. All rights reserved. 6-1 Subscript Bindings and Array Categories In some languages the lower bound of the subscript range.

Copyright © 2006 Addison-Wesley. All rights reserved.

Problems with Pointers• Lost heap-dynamic variable

– An allocated heap-dynamic variable that is no longer accessible to the user program (often called garbage)

• Pointer p1 is set to point to a newly created heap-dynamic variable

• Pointer p1 is later set to point to another newly created heap-dynamic variable

• Sometimes called memory leakage

6-55

Page 35: Copyright © 2006 Addison-Wesley. All rights reserved. 6-1 Subscript Bindings and Array Categories In some languages the lower bound of the subscript range.

Copyright © 2006 Addison-Wesley. All rights reserved.

Pointers in Ada

• Some dangling pointers are disallowed because dynamic objects can be automatically de-allocated at the end of pointer's type scope

• The lost heap-dynamic variable problem is not eliminated by Ada

6-56

Page 36: Copyright © 2006 Addison-Wesley. All rights reserved. 6-1 Subscript Bindings and Array Categories In some languages the lower bound of the subscript range.

Copyright © 2006 Addison-Wesley. All rights reserved.

Pointers in C and C++

• Extremely flexible but must be used with care• Pointers can point at any variable regardless of

when it was allocated• Used for dynamic storage management and

addressing• Pointer arithmetic is possible• Explicit dereferencing and address-of operators• Domain type need not be fixed - void * can

point to any type and can be type checked (cannot be de-referenced)

6-57

Page 37: Copyright © 2006 Addison-Wesley. All rights reserved. 6-1 Subscript Bindings and Array Categories In some languages the lower bound of the subscript range.

Copyright © 2006 Addison-Wesley. All rights reserved.

Pointer Arithmetic in C and C++

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]

6-58

Page 38: Copyright © 2006 Addison-Wesley. All rights reserved. 6-1 Subscript Bindings and Array Categories In some languages the lower bound of the subscript range.

Copyright © 2006 Addison-Wesley. All rights reserved.

Pointers in Fortran 95

• Pointers 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:Integer, Target :: AppleInteger :: OrangeInteger, Pointer :: Int-Ptr //can point to Apple

//but not Orange

6-59

Page 39: Copyright © 2006 Addison-Wesley. All rights reserved. 6-1 Subscript Bindings and Array Categories In some languages the lower bound of the subscript range.

Copyright © 2006 Addison-Wesley. All rights reserved.

Reference Types• C++ includes a special kind of pointer type called a

reference type that is used primarily for formal parameters– Advantages of both pass-by-reference and pass-by-value

#include <iostream> using namespace std; void f( int ); int main() { int x = 5; cout << "Before f(x): " << x << endl; f( x ); cout << "After f(x): " << x << endl; return 0;

} void f( int n ) { cout << "1. In f(x): " << n << endl; n = 32;

cout << "2. In f(x): " << n << endl;

}

#include <iostream> using namespace std; void f( int & ); int main() { int x = 5; cout << "Before f(x): " << x << endl; f( x ); cout << "After f(x): " << x << endl; return 0; } void f( int & n ){ cout << "1. In f(x): " << n << endl; n = 32; cout << "2. In f(x): " << n << endl;

} 6-60

Page 40: Copyright © 2006 Addison-Wesley. All rights reserved. 6-1 Subscript Bindings and Array Categories In some languages the lower bound of the subscript range.

Copyright © 2006 Addison-Wesley. All rights reserved.

Reference Types

• Java extends C++’s reference variables and allows them to replace pointers entirely

• C# includes both the references of Java and the pointers of C++

• All classes are reference variables– E.g. String str1;

str1= “This is a Java literal String.”;

6-61

Page 41: Copyright © 2006 Addison-Wesley. All rights reserved. 6-1 Subscript Bindings and Array Categories In some languages the lower bound of the subscript range.

Copyright © 2006 Addison-Wesley. All rights reserved.

Evaluation of Pointers

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

• Pointers are like goto's--they widen the range of memory 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

6-62

Page 42: Copyright © 2006 Addison-Wesley. All rights reserved. 6-1 Subscript Bindings and Array Categories In some languages the lower bound of the subscript range.

Copyright © 2006 Addison-Wesley. All rights reserved.

Solutions to Dangling Pointer Problem

• 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 de-allocated,

tombstone remains but set to nil– Costly in time and space

ptr

6-64

Page 43: Copyright © 2006 Addison-Wesley. All rights reserved. 6-1 Subscript Bindings and Array Categories In some languages the lower bound of the subscript range.

Copyright © 2006 Addison-Wesley. All rights reserved.

Solutions to Dangling Pointer Problem

• 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

– Every access to the dereferenced pointer compares the key value of the pointer to the lock value of the variable. If they match, access is legal.

• Safest approach: system garbage collection

lock 1

key 1

key 1

6-65

Page 44: Copyright © 2006 Addison-Wesley. All rights reserved. 6-1 Subscript Bindings and Array Categories In some languages the lower bound of the subscript range.

Copyright © 2006 Addison-Wesley. All rights reserved.

Heap Management• A very complex run-time process• Single-size cells vs. variable-size cells• Single-size cells

– Simplest situation. Allocation and deallocation is of a single-size cell.

– Further simplified when every cell already contains a pointer

– All available cells are linked together using the pointers in the cells

– Allocation: take the cells from the list– Deallocation: complex. A variable can be

pointed to by more than one pointer6-66

Page 45: Copyright © 2006 Addison-Wesley. All rights reserved. 6-1 Subscript Bindings and Array Categories In some languages the lower bound of the subscript range.

Copyright © 2006 Addison-Wesley. All rights reserved.

Heap Management

• Two approaches to reclaim garbage– Reference counters (eager approach):

reclamation is gradual, done whenever inaccessible cells are created

– Garbage collection (lazy approach): reclamation occurs when the list of variable space becomes empty

6-67

Page 46: Copyright © 2006 Addison-Wesley. All rights reserved. 6-1 Subscript Bindings and Array Categories In some languages the lower bound of the subscript range.

Copyright © 2006 Addison-Wesley. All rights reserved.

Approach one: Reference Counter• Reference counters: maintain a counter in

every cell that store the number of pointers currently pointing at the cell. When counter reaches 0, time for garbage collection.– Disadvantages: space required, execution

time required, complications for cells connected circularly322

6-68

Page 47: Copyright © 2006 Addison-Wesley. All rights reserved. 6-1 Subscript Bindings and Array Categories In some languages the lower bound of the subscript range.

Copyright © 2006 Addison-Wesley. All rights reserved.

Approach two: Garbage Collection• The run-time system allocates storage cells as

requested and disconnects pointers from cells as necessary; garbage collection then begins– 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, yet have to traverse each cell to decide if it’s garbage)

6-69

Page 48: Copyright © 2006 Addison-Wesley. All rights reserved. 6-1 Subscript Bindings and Array Categories In some languages the lower bound of the subscript range.

Copyright © 2006 Addison-Wesley. All rights reserved.

Variable-Size Cells

• All the difficulties of single-size cells plus more• Required by most programming languages• If garbage collection is used, additional problems

occur– The initial setting of the indicators of all cells in the

heap is difficult– The marking process is nontrivial (have to know the

size)– Maintaining the list of available space is another source

of overhead– Have to search for large enough segment for allocation,

eventually led to large number of small blocks

6-70

Page 49: Copyright © 2006 Addison-Wesley. All rights reserved. 6-1 Subscript Bindings and Array Categories In some languages the lower bound of the subscript range.

Copyright © 2006 Addison-Wesley. All rights reserved.

Summary

• The data types of a language are a large part of what determines that language’s style and usefulness

• The primitive data types of most imperative languages include numeric, character, and Boolean types

• The user-defined enumeration and subrange types are convenient and add to the readability and reliability of programs

• Arrays and records are included in most languages• Pointers are used for addressing flexibility and to

control dynamic storage management

6-71