Array implementation - PowerPoint PPT Presentation

1 / 24
About This Presentation
Title:

Array implementation

Description:

Tombstone approach. each dynamic storage has a special cell, called tombstone, ... disads: cost in time & space as tombstone never deallocated and reused. 1-20 ... – PowerPoint PPT presentation

Number of Views:53
Avg rating:3.0/5.0
Slides: 25
Provided by: david2550
Category:

less

Transcript and Presenter's Notes

Title: Array implementation


1
Array implementation
  • One-dim array address calculation
  • addr(aj) addr(a0) j elementSize

2
Locating an Element in a Multi-dimensioned Array
3
Array implementation
  • Row-major allocation (am,n a3, 3)
  • addr(ai,j) addr(a0,0) ((i n) j)
    elementSize

4
Array implementation
  • Column-major allocation (am,n a3, 3)
  • addr(ai,j) addr(a0,0) ((j m) i)
    elementSize

5
Multi-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

6
Slice Examples
  • Fortran 95
  • Integer, Dimension (10) Vector
  • Integer, Dimension (3, 3) Mat
  • Integer, Dimension (3, 3) Cube
  • Vector (36) is a four element array

7
Slices Examples in Fortran 95
8
Associative arrays
  • an unordered collection of data elements that are
    indexed by an equal of keys
  • example in Perl

9
Compile-Time Descriptors
Single-dimensioned array
Multi-dimensional array
10
Record Types
  • A record is a possibly heterogeneous aggregate of
    data elements in which the individual elements
    are identified by names

11
Array vs. Record
12
Implementation of Record Type
Offset address relative to the beginning of the
records is associated with each field
13
Unions Types
  • A union is a type whose variables are allowed to
    store different type values at different times
    during execution

14
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 gt Diameter Float
  • when Triangle gt
  • Leftside, Rightside Integer
  • Angle Float
  • when Rectangle gt Side1, Side2 Integer
  • end case
  • end record

15
Ada Union Type Illustrated
  • A discriminated union of three shape variables

16
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)

17
Pointer Assignment Illustrated
  • The assignment operation j ptr

18
Pointer 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

19
Solution 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

20
Solution 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

21
Elements 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)

22
Mark 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

23
Mark and Sweep Example
24
Evaluation 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
Write a Comment
User Comments (0)
About PowerShow.com