CS 330 Lecture 29 – Garbage Collection


  • how does polymorphism work?
    • Jim Blinn’s terrible approach
    • the elegant vtable approach
  • garbage collection
    • programmers don’t get explicit reclamation right
    • OS does reclaim memory when program stops
  • what happens when…
    • … we don’t call free?
    • … we call free twice?
    • … we call free once but leave pointer untouched?
  • some approaches:
    • mark-and-sweep
    • stop-and-copy
    • reference counting
  • a C++ counter pointer
    • pair pointer with reference count
    • maintain count on copy, assignment, destruction
    • when count goes to 0, free it



for each allocated block:
  mark unvisited

for each pointer on stack:
  mark block pointed to visited
  recurse on any pointers in block

for each allocated block:
  free if block unvisited



Grades would be easy.
But you all have vtables.
You are dynamic.


Leave a Reply

Your email address will not be published. Required fields are marked *