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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T11:04:41+00:00 2026-05-15T11:04:41+00:00

I create many times I use class DataBuffer in many places in code, so

  • 0

I create many times I use class DataBuffer in many places in code, so it should be fast and lightweight. Moreover I use various buffer sizes, so to reduce memory allocations I wrote such template:

template<unsigned int T_buffer_size> class DataBuffer
{
    public:
        DataBuffer (const unsigned int &buffer_size);

        char buffer [T_buffer_size];
        const unsigned int buffer_size;
};

The problem is, that i have to copy DataBuffer objects few times. That’s why I’m wondering if move constructor can help here. Is there an easy way to move my ‘buffer’ array between objects?

It’s easy to implement this class:

class DataBuffer
{
    public:
        DataBuffer (const unsigned int &buffer_size);

        char *buffer;
        const unsigned int buffer_size;
};

But because ‘buffer’ is a regular pointer it takes more time to access data stored inside it…

  • 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-15T11:04:41+00:00Added an answer on May 15, 2026 at 11:04 am

    But because ‘buffer’ is a regular
    pointer it takes more time to access
    data stored inside it…

    I think you’re mistaken. Accessing an array on the heap is not necessarily slower than accessing one on the stack, e.g. The real difference will be in creating and destroying the array.

    You can’t really have it both ways, directly speaking (but do read further). If your DataBuffer allocates on the heap, it will pay the price for the overhead of heap-based allocation and deallocation, but you will be able to shallow swap the data and implement something like a move constructor (see Alexandrescu’s mojo as a simple proposal to the problem). If it allocates data on the stack, then it will be very fast to create and destroy, but will have to deep copy data which can be a bit expensive (though memcpy is awfully fast and perfectly fine for a char buffer).

    However, you can get the best of both worlds by writing your own memory allocator. I only recommend doing this if you really want to put the effort into it and have seen, with the aid of the profiler, that this is a real bottleneck. I don’t have the time or room to teach you this here, but I must warn you that it is not as trivial as it seems to write one (consider alignment as an example).

    The determination of whether you go for the first example or the second should primarily be dictated by whether you can always determine the buffer size at compile time. If so, go for the first solution as it allows the buffer to be allocated on the stack or the heap. You can still shallow swap data by doing:

    DataBuffer<some_size>* d1 = new DataBuffer<some_size>; 
    DataBuffer<some_size>* d2 = 0; 
    std::swap(d1, d2);
    

    … or for more sophisticated examples, you can use things like boost::shared_ptr.

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

Sidebar

Related Questions

Many times I use 'mqsc' for create MQ queue manager from script files but
So many times writing html code along with javascript, I create <a> elements. My
I'm trying to create a table and I've tried so many times to figure
I want to create a template for a component that is used many times
I want to create as many divs as var foo.length, but my code only
I'm trying to create a simple to use singleton class to connect to mysql
Many times I found out that I need a software to create simple educational
My problem is to create a class which would let to use an object
I have used NSSets many times in my apps, but I have never created
I've tested so many times and get strange behavior.Lwuit version 1.5. I've created few

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.