Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8288457
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T12:17:17+00:00 2026-06-08T12:17:17+00:00

I’ve been using Howard Hinnant’s stack allocator and it works like a charm, but

  • 0

I’ve been using Howard Hinnant’s stack allocator and it works like a charm, but some details of the implementation are a little unclear to me.

  1. Why are global operators new and delete used? The allocate() and deallocate() member functions use ::operator new and ::operator delete respectively. Similarly, the member function construct() uses the global placement new. Why not allow for any user-defined global or class-specific overloads?
  2. Why is alignment set to hard-coded 16 bytes instead of std::alignment_of<T>?
  3. Why do the constructors and max_size have a throw() exception specification? Isn’t this discouraged (see e.g. More Effective C++ Item 14.)? Is it really necessary to terminate and abort when an exception occurs in the allocator? Does this change with the new C++11 noexcept keyword?
  4. The construct() member function would be an ideal candidate for perfect forwarding (to the constructor that is being called). Is this the way to write C++11 conformant allocators?
  5. What other changes are necessary to make the current code C++11 conformant?
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T12:17:18+00:00Added an answer on June 8, 2026 at 12:17 pm

    I’ve been using Howard Hinnant’s stack allocator and it works
    like a charm, but some details of the implementation are a little
    unclear to me.

    Glad it’s been working for you.

    1. Why are global operators new and delete used? The allocate() and deallocate() member functions use ::operator new and
    ::operator delete respectively. Similarly, the member function
    construct() uses the global placement new. Why not allow for any
    user-defined global or class-specific overloads?

    There’s no particular reason. Feel free to modify this code in whatever way works best for you. This was meant to be more of an example, and it is by no means perfect. The only requirements are that the allocator and deallocator supply properly aligned memory, and that the construct member constructs an argument.

    In C++11, the construct (and destroy) members are optional. I would encourage you to remove them from the allocator if you’re operating in an environment that supplies allocator_traits. To find out, just remove them and see if things still compile.

    2. Why is alignment set to hard-coded 16 bytes instead of std::alignment_of<T>?

    std::alignment_of<T> would probably work fine. I was probably being paranoid that day.

    3. Why do the constructors and max_size have a throw() exception specification? Isn’t this discouraged (see e.g. More Effective C++
    Item 14.)? Is it really necessary to terminate and abort when an
    exception occurs in the allocator? Does this change with the new C++11
    noexcept keyword?

    These members just won’t ever throw. For C++11 I should update them to noexcept. In C++11 it becomes more important to decorate things with noexcept, especially special members. In C++11 one can detect whether an expression is nothrow or not. Code can branch depending on that answer. Code that is known to be nothrow is more likely to cause generic code to branch to a more efficient path. std::move_if_noexcept is the canonical example in C++11.

    Don’t use throw(type1, type2) ever. It has been deprecated in C++11.

    Do use throw() when you really want to say: This will never throw, and if I’m wrong, terminate the program so I can debug it. throw() is also deprecated in C++11, but has a drop-in replacement: noexcept.

    4. The construct() member function would be an ideal candidate for perfect forwarding (to the constructor that is being called). Is this
    the way to write C++11 conformant allocators?

    Yes. However allocator_traits will do it for you. Let it. The std::lib has already debugged that code for you. C++11 containers will call allocator_traits<YourAllocator>::construct(your_allocator, pointer, args...). If your allocator implements these functions, allocator_traits will call your implementation, else it calls a debugged, efficient, default implementation.

    5. What other changes are necessary to make the current code C++11 conformant?

    To tell you the truth, this allocator isn’t really C++03 or C++11 conformant. When you copy an allocator, the original and the copy are supposed to be equal to each other. In this design, that is never true. However this thing still just happens to work in many contexts.

    If you want to make it strictly conforming, you need another level of indirection such that copies will point to the same buffer.

    Aside from that, C++11 allocators are so much easier to build than C++98/03 allocators. Here’s the minimum you must do:

    template <class T>
    class MyAllocator
    {
    public:
        typedef T value_type;
    
        MyAllocator() noexcept;  // only required if used
        MyAllocator(const MyAllocator&) noexcept;  // copies must be equal
        MyAllocator(MyAllocator&&) noexcept;  // not needed if copy ctor is good enough
        template <class U>
            MyAllocator(const MyAllocator<U>& u) noexcept;  // requires: *this == MyAllocator(u)
    
        value_type* allocate(std::size_t);
        void deallocate(value_type*, std::size_t) noexcept;
    };
    
    template <class T, class U>
    bool operator==(const MyAllocator<T>&, const MyAllocator<U>&) noexcept;
    
    template <class T, class U>
    bool operator!=(const MyAllocator<T>&, const MyAllocator<U>&) noexcept;
    

    You might optionally consider making MyAllocator Swappable and put the following nested type in the allocator:

    typedef std::true_type propagate_on_container_swap;
    

    There’s a few other knobs like that you can tweak on C++11 allocators. But all of the knobs have reasonable defaults.

    Update

    Above I note that my stack allocator is not conforming due to the fact that copies are not equal. I’ve decided to update this allocator to a conforming C++11 allocator. The new allocator is called short_allocator and is documented here.

    The short_allocator differs from the stack allocator in that the “internal” buffer is no longer internal to the allocator, but is now a separate “arena” object that can be located on the local stack, or given thread or static storage duration. The arena isn’t thread safe though so watch out for that. You could make it thread safe if you wanted to, but that has diminishing returns (eventually you’ll reinvent malloc).

    This is conforming because copies of allocators all point to the same external arena. Note that the unit of N is now bytes, not number of T.

    One could convert this C++11 allocator to a C++98/03 allocator by adding the C++98/03 boiler-plate (the typedefs, the construct member, the destroy member, etc.). A tedious, but straightforward task.

    The answers to this question for the new short_allocator remain unchanged.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string
I've got a string that has curly quotes in it. I'd like to replace
I am reading a book about Javascript and jQuery and using one of the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.