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

  • SEARCH
  • Home
  • 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 8351843
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T08:46:11+00:00 2026-06-09T08:46:11+00:00

I’m writing a container and would like to permit the user to use custom

  • 0

I’m writing a container and would like to permit the user to use custom allocators, but I can’t tell if I should pass allocators around by reference or by value.

Is it guaranteed (or at least, a reasonable assumption to make) that an allocator object will not contain its memory pool directly, and hence it would be OK to copy an allocator and expect the memory pools of the allocators to be cross-compatible? Or do I always need to pass allocators by reference?

(I have found that passing by reference harms performance by a factor of > 2 because the compiler starts worrying about aliasing, so it makes a whether or not I can rely on this assumption.)

  • 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-09T08:46:12+00:00Added an answer on June 9, 2026 at 8:46 am

    In C++11 section 17.6.3.5 Allocator requirements [allocator.requirements] specifies the requirements for conforming allocators. Among the requirements are:

    X                    an Allocator class for type T
    ...
    a, a1, a2            values of type X&
    ...
    a1 == a2             bool          returns true only if storage
                                       allocated from each can be
                                       deallocated via the other.
                                       operator== shall be reflexive,
                                       symmetric, and transitive, and
                                       shall not exit via an exception.
    ...
    X a1(a);                           Shall not exit via an exception.
                                       post: a1 == a
    

    I.e. when you copy an allocator, the two copies are required to be able to delete each other’s pointers.

    Conceivably one could put internal buffers into allocators, but copies would have to keep a list of other’s buffers. Or perhaps an allocator could have an invariant that deallocation is always a no-op because the pointer always comes from an internal buffer (either from your own, or from some other copy).

    But whatever the scheme, copies must be “cross-compatible”.

    Update

    Here is a C++11 conforming allocator that does the “short string optimization”. To make it C++11 conforming, I had to put the “internal” buffer external to the allocator so that copies are equal:

    #include <cstddef>
    
    template <std::size_t N>
    class arena
    {
        static const std::size_t alignment = 16;
        alignas(alignment) char buf_[N];
        char* ptr_;
    
        std::size_t 
        align_up(std::size_t n) {return n + (alignment-1) & ~(alignment-1);}
    
    public:
        arena() : ptr_(buf_) {}
        arena(const arena&) = delete;
        arena& operator=(const arena&) = delete;
    
        char* allocate(std::size_t n)
        {
            n = align_up(n);
            if (buf_ + N - ptr_ >= n)
            {
                char* r = ptr_;
                ptr_ += n;
                return r;
            }
            return static_cast<char*>(::operator new(n));
        }
        void deallocate(char* p, std::size_t n)
        {
            n = align_up(n);
            if (buf_ <= p && p < buf_ + N)
            {
                if (p + n == ptr_)
                    ptr_ = p;
            }
            else
                ::operator delete(p);
        }
    };
    
    template <class T, std::size_t N>
    class stack_allocator
    {
        arena<N>& a_;
    public:
        typedef T value_type;
    
    public:
        template <class U> struct rebind {typedef stack_allocator<U, N> other;};
    
        explicit stack_allocator(arena<N>& a) : a_(a) {}
        template <class U>
            stack_allocator(const stack_allocator<U, N>& a)
                : a_(a.a_) {}
        stack_allocator(const stack_allocator&) = default;
        stack_allocator& operator=(const stack_allocator&) = delete;
    
        T* allocate(std::size_t n)
        {
            return reinterpret_cast<T*>(a_.allocate(n*sizeof(T)));
        }
        void deallocate(T* p, std::size_t n)
        {
            a_.deallocate(reinterpret_cast<char*>(p), n*sizeof(T));
        }
    
        template <class T1, std::size_t N1, class U, std::size_t M>
        friend
        bool
        operator==(const stack_allocator<T1, N1>& x, const stack_allocator<U, M>& y);
    
        template <class U, std::size_t M> friend class stack_allocator;
    };
    
    template <class T, std::size_t N, class U, std::size_t M>
    bool
    operator==(const stack_allocator<T, N>& x, const stack_allocator<U, M>& y)
    {
        return N == M && &x.a_ == &y.a_;
    }
    
    template <class T, std::size_t N, class U, std::size_t M>
    bool
    operator!=(const stack_allocator<T, N>& x, const stack_allocator<U, M>& y)
    {
        return !(x == y);
    }
    

    It could be used like this:

    #include <vector>
    
    template <class T, std::size_t N> using A = stack_allocator<T, N>;
    template <class T, std::size_t N> using Vector = std::vector<T, stack_allocator<T, N>>;
    
    int main()
    {
        const std::size_t N = 1024;
        arena<N> a;
        Vector<int, N> v{A<int, N>(a)};
        v.reserve(100);
        for (int i = 0; i < 100; ++i)
            v.push_back(i);
        Vector<int, N> v2 = std::move(v);
        v = v2;
    }
    

    All allocations for the above problem are drawn from the local arena which is 1 Kb in size. You should be able to pass this allocator around by value or by reference.

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

Sidebar

Related Questions

I would like to count the length of a string with PHP. The string
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I would like to run a str_replace or preg_replace which looks for certain words
I need to clean up various Word 'smart' characters in user input, including but
I would like my Web page http://www.gmarks.org/math_in_e-mail.txt on my Apache 2.2.14 server to display
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I have a jquery bug and I've been looking for hours now, I can't

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.