Version 1: Specialized Rational Memory Manager

To avoid frequent hits to the default manager, the Rational class will maintain a static linked list of preallocated Rational objects, which will serve as the free list of available objects. When we need a new Rational object, we will get one from the free list. When we are done with an object, we will return it to the free list for future allocations.

We declare a helper structure to link adjacent elements on the free list.

class NextOnFreeList {
public:
    NextOnFreeList *next;
};

The free list is declared as a linked list of NextOnFreeList elements.

class Rational {
    ...
    static NextOnFreeList *freeList;
};

If the free list is a list of NextOnFreeList structures, you may wonder where the Rational ...

Get Efficient C++ Performance Programming Techniques now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.