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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T22:16:26+00:00 2026-05-25T22:16:26+00:00

This buffer should contain slots (three in this example) of equal length ( 20

  • 0

This buffer should contain slots (three in this example) of equal length ( 20 in this example)
The buffer has to have contiguous memory so that it can be passed to a C function in non-const fashion.

const int slot_size = 20;
std::vector<char> vbuffer;

This function takes a string, copies to a temporary buffer of the required size then appeds it to vbuffer

void prepBuffer( const std::string& s)
{
  std::vector<char> temp(slot_size);
  std::copy(s.c_str(), s.c_str() + s.length() + 1, temp.begin());
  vbuffer.insert(vbuffer.end(), temp.begin(), temp.end());
}

Testing the function

int main()
{
  vbuffer.reserve(60);
  prepBuffer( "Argentina");
  prepBuffer( "Herzegovina");
  prepBuffer( "Zambia");

  cout << &vbuffer[0] << endl;
  cout << &vbuffer[20] << endl;
  cout << &vbuffer[40] << endl;
}

Question. There is a lot of string copying in my prepBuffer function. I am looking for a better way to fill up vbuffer with minimal copying
EDIT
The size of slots is determined elsewhere in the program. But it is not known at compile time.

EDIT

In line with my accepted answer below, I have settled on this version

void prepBuffer(const std::string& s)
{
  assert(s.size() < slot_size );
  vbuffer.insert(vbuffer.end(), s.begin(), s.end());
  vbuffer.insert(vbuffer.end(), slot_size - s.size(), '\0' ); 
}

Suggestions are still welcome

  • 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-25T22:16:27+00:00Added an answer on May 25, 2026 at 10:16 pm

    How about this:

    vbuffer.reserve(vbuffer.size() + 20);
    vbuffer.insert(vbuffer.end(), s.begin(), s.end());
    vbuffer.insert(vbuffer.end(), 20 - s.size(), '\0');
    

    An additional check on the string length is recommended, along with a policy for handling over-long strings (e.g. assert(s.size() < 20);).

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

Sidebar

Related Questions

Say I have a line in an emacs buffer that looks like this: foo
Is this safe to do? Does fgets terminate the buffer with null or should
So.. I have a buffer with MP3 data (If I would save this buffer
How can I create a circular buffer on the heap in VB.NET ? This
If you have a pending operation, eg stream.BeginRead(_buffer, 0, _buffer.Length, _asyncCallbackRead, this); and you
I have 2 networked apps that should send serialized protobuf-net messages to each other.
I have this part in a method, It should return the page and headers
Assuming MyClass uses the default destructor (or no destructor), and this code: MyClass *buffer
Given this piece of code : public static void writeFile(File file,List buffer)throws IOException{ File
I've created a simple buffer manager class to be used with asyncroneous sockets. This

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.