Sharing Pointers and Garbage Collection

Steven J Zeil

Old Dominion University
Dept. of Computer Science

Table of Contents

1. Shared Structures
1.1. Singly Linked Lists
1.2. Doubly Linked Lists
1.3. Airline Connections
2. Garbage Collection
2.1. Reference Counting
2.2. Mark and Sweep
2.3. Generation-Based Collectors
2.4. Incremental Collection
3. Strong and Weak Pointers
4. Coming Soon to C++: std Reference Counting

We've talked a lot about using pointers to share information, but mainly as something that causes problems. We have a pretty good idea of how to handle ourselves when we have pointers among our data members and don't want to share. In that case, we rely on implementing our own deep copying so that every "container" has distinct copies of all of its components.

For some data structures, this is OK. If we are using a pointer mainly to give us access to a dynamically allocated array, we can copy the entire array as necessary. In the example shown here, we would want each catalog to get its own distinct array of books. So we would implement a deep copy for the assignment operator and copy constructor, and delete the allBooks pointer in the destructor.

But not every data structure can be treated this way. Sometimes, sharing is essential to the behavior of the data structure that we want to implement. (If nothing else, Our study of graphs has highlighted the fact that many data structures are inherently "sharing".)


In the Forum:

(no threads at this time)