C++ Linked Lists   «Prev  Next»
Lesson 10Reference counting
Objective Understand the fundamentals of reference counting.

How reference counting can be employed in a C++ Singly Linked List

Here's how reference counting can be employed in a C++ singly linked list, and the trade-offs to consider:
Concept of Reference Counting for Memory Management
  • Basic Idea: Each node in the linked list stores a reference count beside its data and `next` pointer. This count indicates how many pointers (either from other nodes or variables in your program) are currently referencing that node.
  • Allocation: When a new node is created, its reference count is initialized to 1.
  • Referencing: Whenever a new pointer starts referencing a node (e.g., when inserting it into the list), the node's reference count is incremented.
  • Dereferencing: Whenever a pointer stops referencing a node (e.g., when removing it from the list), the node's reference count is decremented.
  • Deallocation: When a node's reference count reaches zero, it means no other element or variable is pointing to it. The node can then be automatically deallocated (garbage collected).

Implementation in a Singly Linked List
  1. Modify the Node Structure:
    class Node {
    public:
       int data; 
       Node* next; 
       int refCount; // For reference counting
    
       Node(int data) {
    	   this->data = data;
    	   next = nullptr;
    	   refCount = 1;
       }
    };
    
  2. Update Linked List Operations:
    • Insertion: When inserting a node, increment the reference count of the node it's being linked to.
    • Deletion: When deleting a node, decrement the reference count of the node it was linked to.
    • Assignment: When assigning a node pointer to another variable, increment the reference count. Before the variable is reassigned or goes out of scope, decrement the reference count of the previous node it was pointing to.

Advantages of Reference Counting
  • Automatic Memory Management: Reference counting provides a form of automatic garbage collection, preventing memory leaks associated with forgetting to manually deallocate nodes.

Disadvantages of Reference Counting
  • Overhead: Storing the reference count adds memory overhead to each node.
  • Cyclic References: Reference counting cannot handle cyclic relationships, where nodes form a loop referencing each other. This can lead to memory leaks even if there are no external references to the cycle.
When to Consider Reference Counting in a Singly Linked List
  • Simple Memory Management: Reference counting is useful when you want to avoid the complexity of manual memory management in a singly linked list.
  • Non-Cyclic Structures: It's a viable option if your linked list is guaranteed not to have circular references.

Alternatives
  • Manual Memory Management: Explicitly using `new` and `delete` in C++ for control, but prone to errors if not careful.
  • Garbage Collectors: Modern C++ often uses smart pointers (like `unique_ptr` and `shared_ptr`) that offer more sophisticated memory management including cycle detection.

Allocation at runtime of Large Aggregates

The following page discusses reference counting using Microsoft's COM.
Allocation at runtime of large aggregates can readily exhaust memory resources. Our singly linked list example, which we looked at over the last several lessons, showed one scheme for handling this. In that example, the system reclaimed memory by traversing each list and disposing of each element. This model of reclamation is a form of garbage collection and can involve a computationally expensive procedure. A different disposal scheme is reference counting. With reference counting, each dynamically allocated object tracks its active references:
  1. When an object is created, its reference count is set to 1.
  2. Every time the object is newly referenced, a reference count is incremented.
  3. Every time the object loses a reference, the count is decremented.
  4. When the reference count becomes zero, the object's memory is disposed of.
Let us explore this further. In the next lesson, we'll look at a string class that has reference semantics for copying.

SEMrush Software