Data Types - PowerPoint PPT Presentation

1 / 19
About This Presentation
Title:

Data Types

Description:

dangling pointers. Dangling references. Reclaiming space ... Dangling reference: a live pointer to a reclaimed object. Mechanisms to avoid dangling references ... – PowerPoint PPT presentation

Number of Views:22
Avg rating:3.0/5.0
Slides: 20
Provided by: evangelo
Category:
Tags: dangling | data | types

less

Transcript and Presenter's Notes

Title: Data Types


1
Data Types
2
What is a type system?
  • A mechanism for defining types and associating
    with language constructs
  • Rules for
  • Type equivalence (whether two values have same
    type)
  • Type compatibility (whether a value can be used
    in a certain context)
  • Type inference (type of expression based on parts)

3
Types in a language
  • Strongly typed prohibits (enforceably)
    application of an op to any object not supporting
    the operation
  • Statically typed strongly typed and compiler can
    check types (Pascal, C, ...)
  • Dynamically typed operands of operations checked
    at run time (Lisp, Smalltalk)
  • Programmer does not specify types, compiler
    infers types from context (ML).

4
Definition of types
  • Declaration introduces name, scope
  • Definition describes type of value to which name
    is bound.
  • Viewpoints
  • Denotational type is set of values
  • Constructive type is either built-in, or
    composite
  • Abstraction type is interface, ie set of ops
    with well-defined semantics.

5
Composite types
  • Records
  • Variant records (one variant valid at any given
    time)
  • Arrays (index, component type)
  • Sets
  • Pointers (references)
  • Lists (recursive def)
  • Files

6
Records
  • Nested record definitions
  • Memory layout (alignment with word boundaries for
    efficient access, packed for space)

packed
naive
compromise
7
Arrays
  • Time at which array shape is bound is important
  • global lifetime, static shape (static memory)
  • local lifetime, static shape (stack frame)
  • local lifetime, shape bound at elaboration/binding
    creation time (variable-size part of the stack
    frame. Fixed-size part is for objects whose size
    is statically known). E.g Ada.
  • arbitrary lifetime, shape bound at elaboration
    time (heap). E.g. Java.
  • arbitrary lifetime, dynamic shape (heap)

8
Memory layout for arrays
9
Memory layout (2)
contiguous array
row pointers
Memory layout determines nature and efficiency of
address calculations
10
Pointers
  • Reference model of variables (Lisp, ML, CLU,
    Java)
  • Value model of variables (C, Pascal, Ada)
  • requires pointers for recursive types
  • pointers are not addresses, but higher level
  • Explicit storage reclamation
  • memory leak
  • dangling pointers

11
Dangling references
  • Reclaiming space of non-live objects
  • Explicit reclamation (free, dispose, delete )
  • Garbage collection
  • Dangling reference a live pointer to a reclaimed
    object.
  • Mechanisms to avoid dangling references
  • Tombstones
  • Locks and keys

12
Tombstones
  • Invalid tombstone set to outside address
    space(cause hardware interrupt)
  • Space overhead
  • Easy to change locationof object in heap (just
    change tombstone address)
  • For both heap and stackobjects

13
Locks and keys
Pointer address key Object has lock When
reclaiming object, Change the lock Tombstones
vs. locks/keys Efficiency comparison
unclear Both incur significant time and
space Overhead.
14
Garbage collection
  • Automatic reclamation of objects
  • Essential for functional languages
  • Popular in imperative languages (Clu, Ada,
    Modula-3, Java)
  • Difficult to implement
  • Slower than manual reclamation

15
Reference counts
  • Associate reference count with each object.
  • set to 1 when object is allocated.
  • adjust
  • when one pointer assigned to another.
  • on subroutine return.
  • requires type descriptors to identify pointers
  • In stack frames
  • In objects
  • circular lists present a problem.

16
Mark-and-sweep collection
  • Mark all blocks of the heap as unreachable.
  • Beginning with all pointers outside heap,
    recursively visit each block and mark it
    reachable.
  • Move each unreachable block into the free list.

17
Issues with mark-and-sweep
  • In steps 1,3 tell where every block begins and
    ends. gt block begins with its size
  • In step 2 find all pointers within a block.
  • Stop-the-world phenomenon awkward pauses for
    garbage collection, bad especially for
    interactive programs.

18
Variation stop-and-copy
  • Divide heap in half.
  • All allocation happens in first half.
  • When first half is nearly full, GC starts
  • Recursively explores reachable data
  • Reachable data is copied to second half
    (compactly).
  • When done, all useful data in second half
  • Swap use of first and second half, continue

19
Variation Generational technique
  • Idea Garbage collected only among recently
    allocated objects.
  • Heap divided into two halves
  • allocation only in first
  • permanent only in second
  • Blocks that survive a small number of collections
    become permanent, copied over to the permanent
    half.
Write a Comment
User Comments (0)
About PowerShow.com