Chapter 6: Part III
- A record is a possibly heterogeneous aggregate of data elements in which
the individual elements are identified by names
- Design Issues:
- What is the form of references?
- What unit operations are defined?
- Is there record oriented I/O
Records
- Record Definition Syntax
- COBOL and PL/I have level numbers
- Most others use recursive definitions
- Record Field References
- COBOL
field_name OF record_name_1 OF ... OF record_name_n
- Others (dot notation)
record_name_1.record_name_2. ... record_name_n.field_name
- Fully qualified references must include all record names
- Elliptical references allow leaving out record names as long as the reference
is unambiguous
- Pascal and VB provide a with clause to abbreviate references
Compile Time Descriptors

Record Operations
- Assignment
- Pascal, Ada, and C allow it if the types are identical
- In Ada, the RHS can be an aggregate constant
- COBOL: note the COBOL MOVE is actually a copy
- allows MOVE to an entire record as alphanumeric (no checking)
- MOVE symbolic literals (e.g. SPACES) to an entire record
- MOVE CORRESPONDING moves all fields in the source record to fields with
the same names and relative levels in the destination record
- Initialization
- Allowed in ADA using an aggregate constant
- COBOL allows initializing with VALUE clauses in record definition
- Comparison
- In Ada, = and /=; one operand can be an aggregate constant
Comparing Records and Arrays
- Access to array elements is much slower than access to record fields, because
subscripts are dynamic (field names are static)
- Dynamic subscripts could be used with record field access, but it would
disallow type checking and it would be much slower
Unions
- A union is a type whose variables are allowed to store different type values
at different times during execution
- Design Issues for unions:
- What kind of type checking, if any, must be done?
- Should unions be integrated with records?
Unions Examples
Unions (cont)
- Ada - discriminated unions: Safer than Pascal
- Tag must be present
- It is impossible for the user to create an inconsistent union (because
tag cannot be assigned by itself--All assignments to the union must include
the tag value, because they are aggregate values)
- C and C++ - free unions (no tags)
- Not part of their records
- No type checking of references
- Java has neither records nor unions
Sets
- A set is a type whose variables can store unordered collections of distinct
values from some ordinal type
- Design Issue:
- What is the maximum number of elements in any set base type?
Pointers Problems with
- Dangling pointers (dangerous)
- A pointer points to a heap-dynamic variable that has been deallocated
- Creating one (with explicit deallocation):
- Allocate a heap-dynamic variable and set a pointer to point at it
- Set a second pointer to the value of the first pointer
- Deallocate the heap-dynamic variable, using the first pointer
- Lost Heap-Dynamic Variables ( wasteful)
- A heap-dynamic variable that is no longer referenced by any program pointer
- Pointer p1 is set to point to a newly created heap-dynamic variable
- p1 is later set to point to another newly created heap-dynamic variable
- The process of losing heap-dynamic variables is called memory leakage
Examples
Pascal: used for dynamic storage management only
- Explicit dereferencing (postfix ^)
- Dangling pointers are possible (dispose)
- Dangling objects are also possible
Ada: a little better
- 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)
Examples (cont)
C and C++
Pointer assignment

Examples: Fortran
- 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
Examples C++ Reference Types
- Constant pointers that are implicitly dereferenced
- Used for parameters
- Advantages of both pass-by-reference and pass-by-value
Examples: 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
Pointers: Evaluation
- 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
Pointers: Implementation
- Large computers use single values
- Intel microprocessors use segment and offset
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 deallocated, tombstone remains but set to nil


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
Heap Management
- Single-size cells vs. variable-size cells
- Reference counters (eager approach) vs. garbage collection (lazy approach)
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
Garbage Collection
- 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)
- Unpredictable slowdowns in program
Notes