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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T14:41:11+00:00 2026-06-11T14:41:11+00:00

I want to have a little class which manages raw memory with this API

  • 0

I want to have a little class which manages raw memory with this API

template<class allocator = std::allocator<char> >
class raw_memory
{
   static_assert(std::is_same<char, typename allocator::value_type>::value,
                 "raw_memory: allocator must deal in char");
public:
   raw_memory() = default;
   raw_memory(raw_memory&&) = default;
   raw_memory&operator=(raw_memory&&) = default;
   explicit raw_memory(size_t, allocator const& = allocator());
   ~raw_memory();       // deletes any memory
   char*get();          // returns pter to (begin of) memory
   void resize(size_t); // re-allocates if necessary, may delete old data
   size_t size() const; // returns number of bytes currently hold

   raw_memory(raw_memory const&) = delete;
   raw_memory&operator=(raw_memory const&) = delete;
   raw_memory(raw_memory&) = delete;
   raw_memory&operator=(raw_memory&) = delete;
};

The template parameter allocator allows for different memory alignment options.

I was thinking about using std::unique_ptr<char, Deleter>, as a member (or base) (plus a size_t holding the number of bytes). What to use as Deleter? Or is there a better way to achieve all that?

  • 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-11T14:41:12+00:00Added an answer on June 11, 2026 at 2:41 pm

    Since you let the users of your class to specify an allocator as a type argument, you must reference this argument for both allocation and deallocation. For this reason, the commend made by @Kerrek (although tricky and nice usage of pointer to function) is invalid, because you do want to use the allocator methods passed as an argument.

    Using unique_ptr can work for you. As you correctly commented you must provide your own deleter, and per my comments above, it must be based on the allocator class passed as an agument to your template.

    SIDE NOTE: Please be aware you have a syntax error in your template declaration. See my sample code below for the correct syntax (i.e. you must have the keyword ‘template’):

    template< class A = std::allocator<char> >
    class raw_memory
    {
    private:
        A m_al;
        std::unique_ptr< char, std::function<void(char*)> > m_buffer;
    
    public:
        raw_memory( size_t size, const A& al = A() )
            :m_al(al)
            ,m_buffer( m_al.allocate(size), [this, size](char* ptr){ m_al.deallocate(ptr,size); } )
            {
            }
    };
    

    Few things to note:

    • I’m using A as the type argument (I find it more readable to use all capital letters for tymplate argument).
    • Using c++11 Lambda notation, I’m passing a deleter functor to the constructor of unique_ptr. This functor has a closure state (a reference to al, and the size), which will be used during delete. In the closure I’m putting the this pointer (required to access m_al) and the size – both by value.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this easy little form in my JavaFX application. I want to use
I want to write a class which provides basically a list of entries. This
I have this little script: var moolang = new Class({ initialize: function(element) { this.el
I have created a little example,please have a look, http://jsfiddle.net/bWTwL/ I want to have
I have a little question I want to click on my smileys and insert
I have a little problem with the library 'Telerik'. Indeed I want to implement
I have a little experience developing small command-line applications with Python. I want to
I have a little problem, I have an array and I want to add
I want to have a java app running, using a function/method (with as little
I am making a basic little game in Java and I want to have

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.