Problems with Pointer - and Some Solutions





Three Possible Kinds of Errors





Null Pointers





Error 1: Null Pointers



Null Exclusion



Null Exclusion in Type Declarations



Null Exclusion in Parameter Declarations



Something Similar in Java?





Garbage Creation and Memory Leak





Garbage: Definition, and the Freelist



Error 2: Creating Garbage



Garbage Collection and Memory Leaks



Garbage, Explicit Deallocation, and the Freelist



Two Algorithms for Collecting Garbage

  1. Mark and Sweep:
    1. Follow all pointers and mark all reachable cells
    2. Look at all cells and free cells that are not marked

  2. Copy Collection:
    1. Only use half of heap space at a time
    2. To collect garbage: Follow all pointers and copy reachable cells from half one to half two
    3. Now allocate cells from half two


Avoid Garbage with Explicit Deallocation of Memory



Stack-Based Allocation



Controlled Types



Storage Pools





Dangling References (and Aliases)





Error 3: Dangling References



Aliases



Dangling References



Dangling References and Reallocated Memory



Dangling References and Reallocation of Different Types



Avoiding Dangling References



Avoiding Dangling References with Aliases



Summary of Errors