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 7544563
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T08:42:16+00:00 2026-05-30T08:42:16+00:00

In the code below is the function make_vector(). It creates a vector and returns

  • 0

In the code below is the function make_vector(). It creates a vector and returns it to the caller. I want to be able to specify an allocator for the vector to use, but use the default std::allocator by default. This is because under some circumstances the default allocator is all I need, but other times I need to allocate from some pre-defined memory pools.

The closest I’ve come is the make_vector2() function template. It works with the std::allocator, but I don’t know how to pass the ‘arena’ argument into my custom allocator.

Hopefull this working c++11 example will explain it better:

#include <malloc.h>
#include <cinttypes>
#include <cstddef>
#include <iostream>
#include <limits>
#include <stdexcept>
#include <vector>

namespace mem
{
    // Memory arena to allocate from.
    enum class ARENA
    {
        TEXTURES,
        FONTS,
        SCRIPTS
    };

    // Allocate block from specific arena.
    void *malloc( const std::size_t size, const ARENA arena )
    {
        return std::malloc( size /*, arena */ );
    }

    // Free block from specific arena.
    void free( void *ptr, const ARENA arena )
    {
        std::free( ptr /*, arena */ );
    }


    // The allocator - forward declaration.
    // Not derived from std::allocator - should it be?
    // Based on code from here:
    // http://drdobbs.com/184403759?pgno=2
    template<typename T> class allocator;

    // Specialised for void.
    template<> class allocator<void>
    {
        public:
            typedef std::size_t size_type;
            typedef ptrdiff_t difference_type;
            typedef void* pointer;
            typedef const void* const_pointer;
            typedef void value_type;

            template<typename U> struct rebind
            {
                typedef allocator<U> other;
            };
    };


    template<typename T> class allocator
    {
        public:
            typedef std::size_t size_type;
            typedef std::ptrdiff_t difference_type;
            typedef T* pointer;
            typedef const T* const_pointer;
            typedef T& reference;
            typedef const T& const_reference;
            typedef T value_type;

            template<typename U> struct rebind
            {
                 typedef allocator<U> other;
            };

            allocator( ARENA arena ) noexcept :
                arena_( arena )
            {}

            ~allocator() noexcept
            {}

            pointer address( reference x ) const
            {
                    return &x;
            }

            const_pointer address( const_reference x ) const
            {
                return &x;
            }

            pointer allocate( size_type n, allocator<void>::const_pointer hint = 0 )
            {
                void *p = mem::malloc( n * sizeof( T ), arena_ );
                if ( p == nullptr )
                {
                        throw std::bad_alloc();
                }
                return static_cast<pointer>( p );
            }

            void deallocate( pointer p, size_type n )
            {
                mem::free( p, arena_ );
            }

            size_type max_size() const noexcept
            {
                return std::numeric_limits<std::size_t>::max() / sizeof( T );
            }

            void construct( pointer p, const T& val )
            {
                new (p) T(val);
            }

            void destroy( pointer p )
            {
                p->~T();
            }

            allocator( const allocator& src ) noexcept
            {
                arena_ = src.arena_;
            }

            ARENA arena_;
    };
} // namespace mem

template<class T1, class T2> bool operator==( const mem::allocator<T1> &alloc1, const mem::allocator<T2> &alloc2 ) noexcept
{
    return alloc1.arena_ == alloc2.arena_;
}

template<class T1, class T2> bool operator!=( const mem::allocator<T1> &alloc1, const mem::allocator<T2> &alloc2 ) noexcept
{
    if alloc1.arena_ != alloc2.arena_;
}

// How do I allow the custom allocator to be passed? Function parameter? Template?
std::vector<uint8_t> make_vector()
{
    std::vector<uint8_t> vec;
    // Do stuff with the vector
    return vec;
}

// This template function seems to work with std::allocator
template< typename T > std::vector<uint8_t,T> make_vector2()
{
    std::vector<uint8_t,T> vec;
    // Do stuff with the vector.
    return vec;
}

int main( int argc, char **argv )
{
    // vec1 - Allocates from TEXTURES arena
    // See the C++11 FAQ by  Bjarne Stroustrup here:
    // http://www2.research.att.com/~bs/C++0xFAQ.html#scoped-allocator
    std::vector<uint8_t, mem::allocator<uint8_t>> vec1( mem::allocator<uint8_t>{mem::ARENA::TEXTURES} );

    // vec2 - Make the vector using the default allocator.
    auto vec2 = make_vector2< std::allocator<uint8_t> >();

    return 0;
}

In main() vec1 is created to use the TEXTURES arena for allocation. The arena to use is passed into the constructor of the allocator. Vec2 is created by the make_vector2() templated function and uses the std::allocator.

Q: How can I define the make_vector() function so it can create a vector that uses the std::allocator or the custom pool allocator above?

  • 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-05-30T08:42:18+00:00Added an answer on May 30, 2026 at 8:42 am

    In C++11, function templates can have default template arguments:

    template<class T, class Alloc = std::allocator<T>>
    std::vector<T, Alloc> make_vector(Alloc const& al = Alloc()){
      std::vector<T, Alloc> v(al);
      // ...
      return v;
    }
    

    Live example on Ideone.

    In C++03 (or with compilers that don’t support that feature), it’s a bit more cumbersome, but you can overload based on the template parameters:

    template<class T>
    std::vector<T> make_vector(){
      std::vector<T> v;
      // ...
      return v;
    }
    
    template<class T, class Alloc>
    std::vector<T, Alloc> make_vector(Alloc const& al = Alloc()){
      std::vector<T, Alloc> v(al);
      // ...
      return v;
    }
    

    Live example on Ideone.

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

Sidebar

Related Questions

In the code below, why does the open function work but the close function
I'm writing code to do Xml serialization. With below function. public static string SerializeToXml(object
Hello friends i am running code given below which contains the setLogTimeEntery function and
I am using jquery datepicker, please see below code $(document).ready(function() { $(.txtDate).datepicker({ showOn: 'button',
I am using the below jquery code to call a ajax function in my
Below I present you some code which has completely been butchered by me. $(.gig).hover(function()
My code is as below: Interpolation.h #ifndef INTERPOLATOR #define INTERPOLATOR #include <vector> #include <utility>
This is my code below and I want to make the value in the
I have a function code to make subdomain dynamically by php.Code is below <?php
I have the code below, I'm attempting to write a generic function which takes

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.