Resource Management resources other than memory need to be managed (files, net connections, GUI objects, DB connections, etc.) create/destroy OS API examples files: open/close network connections: socket, close windows: CreateWindow, CloseHandle code must be written to ensure that resources don't leak (DB connection example) Java has finally cleanup code that's guaranteed to be executed C++ doesn't have finally, but it does have destructors which are guaranteed to be called (Java doesn't have destructors) once an OS-level resource has been allocated, wrap it in an object whose destructor will ensure that it gets deallocated (even in the case of early exit or exception) the wrapper object should provide a method interface that allows the resource to be accessed in an object-oriented manner without having to directly access the underlying OS-level resource standard C++ classes work this way ifstream, ofstream string write your own class wrappers if necessary Smart Pointers problems with "dumb" pointers concept of a smart pointer Smart pointer implementation (SmartPtr.h) Make example (MakeDriver.cpp) [uses auto_ptr] Reference Counting review reference counting emphasize error-prone nature of reference counting Referencable base class Reference smart pointer class Chess example code (Piece.h) Garbage Collection Mark-and-Sweep is a garbage collection algorithm that can be used to automatically deallocate heap objects when a program is done using them. Unlike reference counting, Mark-and-Sweep works even when there are cycles in the program’s data structures. The algorithm has two phases, the Mark phase and the Sweep phase. Mark Phase Every heap object has a flag indicating whether or not it is “reachable” by the program. At the beginning of the mark phase the reachable flag for each object is set to false. Then, beginning with the pointers stored in the static data area and runtime stack, the program’s data structures are traversed by following pointers from object to object. Each time a new heap object is reached during this traversal, its reachable flag is set to true. Cyclical data structures are no problem because the algorithm only follows pointers in objects that are not already marked. Sweep Phase After all reachable objects have been marked, the sweep phase traverses the heap and deallocates all objects with a false reachable flag, which indicates they are no longer reachable or in use by the program. Allocating 2D Arrays on the Heap declaring a regular 2D array (e.g., string board[5][200]) string (*arr)[200] = new string[5][200] for (int i=0; i < 5; ++i) for (int j=0; j < 200; ++j) arr[i][j] = ""; works for any number of dimensions spine-with-ribs string ** arr = new string *[5]; for (int i=0; i < 5; ++i) { arr[i] = new string[200]; } for (int i=0; i < 5; ++i) for (int j=0; j < 200; ++j) arr[i][j] = ""; manual indexing string * arr = new string[5*200]; elements are laid out in row-major order (or column if you prefer) ROWS=5 COLS=200 index(r, c) = r * COLS + c