Allocator (C++) - Background

Background

Further information: Standard Template Library

Alexander Stepanov and Meng Lee presented the Standard Template Library to the C++ standards committee in March 1994. The library received preliminary approval, although a few issues were raised. In particular, Stepanov was requested to make the library containers independent of the underlying memory model, which led to the creation of allocators. Consequently, all of the STL container interfaces had to be rewritten to accept allocators.

In adapting STL to be included in the C++ Standard Library, Stepanov worked closely with several members of the standards committee, including Andrew Koenig and Bjarne Stroustrup, who observed that custom allocators could potentially be used to implement persistent storage STL containers, which Stepanov at the time considered an "important and interesting insight".

From the point of view of portability, all the machine-specific things which relate to the notion of address, pointer, and so on, are encapsulated within a tiny, well-understood mechanism.

—Alex Stepanov, designer of the Standard Template Library

The original allocator proposal incorporated some language features that had not yet been accepted by the committee, namely the ability to use template arguments that are themselves templates. Since these features could not be compiled by any existing compiler, there was, according to Stepanov, "an enormous demand on Bjarne 's and Andy 's time trying to verify that we were using these non-implemented features correctly." Where the library had previously used pointer and reference types directly, it would now only refer to the types defined by the allocator. Stepanov later described allocators as follows: "A nice feature of STL is that the only place that mentions the machine-related types (...) is encapsulated within roughly 16 lines of code."

While Stepanov had originally intended allocators to completely encapsulate the memory model, the standards committee realized that this approach would lead to unacceptable efficiency degradations. To remedy this, additional wording was added to the allocator requirements. In particular, container implementations may assume that the allocator's type definitions for pointers and related integral types are equivalent to those provided by the default allocator, and that all instances of a given allocator type always compare equal, effectively contradicting the original design goals for allocators and limiting the usefulness of allocators that carry state.

Stepanov later commented that, while allocators "are not such a bad ideas in theory (...) nfortunately they cannot work in practice". He observed that to make allocators really useful, a change to the core language with regards to references was necessary.

The 2011 revision of the C++ Standard removed the weasel words requiring that allocators of a given type always compare equal and use normal pointers. These changes make stateful allocators much more useful and allow allocators to manage out-of-process shared memory. The current purpose of allocators is to give the programmer control over memory allocation within containers, rather than to adapt the address model of the underlying hardware. In fact, the revised standard eliminated the ability of allocators to represent extensions to the C++ address model, formally (and deliberately) eliminating their original purpose.

Read more about this topic:  Allocator (C++)

Famous quotes containing the word background:

    Silence is the universal refuge, the sequel to all dull discourses and all foolish acts, a balm to our every chagrin, as welcome after satiety as after disappointment; that background which the painter may not daub, be he master or bungler, and which, however awkward a figure we may have made in the foreground, remains ever our inviolable asylum, where no indignity can assail, no personality can disturb us.
    Henry David Thoreau (1817–1862)

    I had many problems in my conduct of the office being contrasted with President Kennedy’s conduct in the office, with my manner of dealing with things and his manner, with my accent and his accent, with my background and his background. He was a great public hero, and anything I did that someone didn’t approve of, they would always feel that President Kennedy wouldn’t have done that.
    Lyndon Baines Johnson (1908–1973)

    They were more than hostile. In the first place, I was a south Georgian and I was looked upon as a fiscal conservative, and the Atlanta newspapers quite erroneously, because they didn’t know anything about me or my background here in Plains, decided that I was also a racial conservative.
    Jimmy Carter (James Earl Carter, Jr.)