O'Reilly logo

Efficient C++ Performance Programming Techniques by David Mayhew, Dov Bulka

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required