Title: Array implementation
1Array implementation
- One-dim array address calculation
- addr(aj) addr(a0) j elementSize
2Locating an Element in a Multi-dimensioned Array
3Array implementation
- Row-major allocation (am,n a3, 3)
- addr(ai,j) addr(a0,0) ((i n) j)
elementSize
4Array implementation
- Column-major allocation (am,n a3, 3)
- addr(ai,j) addr(a0,0) ((j m) i)
elementSize
5Multi-dim Array Implementation
- Given a multi-dim array am,n,p
- Calculate the address of ai,j,k
- Row-major implementation
- addr(ai,j,k) addr(a0,0,0) (i n p j
p k) elementSize - Column-major implementation
- addr(ai,j,k) addr(a0,0,0) (k m n j
m i) elementSize
6Slice Examples
- Fortran 95
- Integer, Dimension (10) Vector
- Integer, Dimension (3, 3) Mat
- Integer, Dimension (3, 3) Cube
- Vector (36) is a four element array
7Slices Examples in Fortran 95
8Associative arrays
- an unordered collection of data elements that are
indexed by an equal of keys - example in Perl
9Compile-Time Descriptors
Single-dimensioned array
Multi-dimensional array
10Record Types
- A record is a possibly heterogeneous aggregate of
data elements in which the individual elements
are identified by names
11Array vs. Record
12Implementation of Record Type
Offset address relative to the beginning of the
records is associated with each field
13Unions Types
- A union is a type whose variables are allowed to
store different type values at different times
during execution
14Ada 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 gt Diameter Float
- when Triangle gt
- Leftside, Rightside Integer
- Angle Float
- when Rectangle gt Side1, Side2 Integer
- end case
- end record
15Ada Union Type Illustrated
- A discriminated union of three shape variables
16Pointer 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)
17Pointer Assignment Illustrated
- The assignment operation j ptr
18Pointer type
- Usage indirect addressing dynamic storage
management - Problems
- Dangling pointer points to a heap-dynamic
variables that has been deallocated - Memory leakage an allocated that is no longer
accessible
19Solution to pointer problems
- Tombstone approach
- each dynamic storage has a special cell, called
tombstone, that points to this storage - the actual pointer(s) point to the tombstone
- deallocate set tombstone to NULL
- disads cost in time space as tombstone never
deallocated and reused
20Solution to pointer problems
- Locks-and-keys approach
- pointer value (key, address)
- dynamic storage (lock, storage)
- access check if (key lock)
- Deallocate lock? invalid value
- heap management for reclaiming garbage
- reference counters
- garbage collection
21Elements of Garbage Collection
- Every garbage collection scheme has the following
steps - Allocate space as needed for new objects
- When space runs out
- (a) Compute what objects might be used again
- (b) Free the space used by objects not found in
(a)
22Mark and Sweep
- when memory runs out, GC executes two phases
- mark phase traces reachable objects
- sweep phase collects garbage objects
- Every object has an extra bit the mark bit
- reserved for memory management
- initially the mark bit is 0
- set to 1 for the reachable objects in the mark
phase
23Mark and Sweep Example
24Evaluation of Mark and Sweep
- Space for a new object is allocated from the new
list - a block large enough is picked
- an area of the necessary size is allocated from
it - the left-over is put back in the free list
- Advantage objects are not moved during GC
- no need to update the pointers to objects
- works for languages like C and C