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 the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.