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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T07:22:36+00:00 2026-05-24T07:22:36+00:00

If I need a binary buffer object, like that used for TCP/UDP communication, what

  • 0

If I need a binary buffer object,
like that used for TCP/UDP communication,
what would I base it on, in c++ ?

  • vector<unsigned char> ?

  • std::string ? — std::string can hold 0-bytes, contrary to popular belief, so it can be used to hold binary data

  • new char[] ?

  • malloc() ?

Did anybody see std::vector used for binary buffers ?
I did not see. Why ? Performance ?

And I did see malloc() often used for binary buffers.
In C++. Can anybody confirm ? Explain ?

Thanks

  • 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-24T07:22:37+00:00Added an answer on May 24, 2026 at 7:22 am

    A binary buffer can be just seen as a piece of memory that you reserve for storing some unstructured information.
    In this sense, using new char[] and malloc are perfectly equivalent, IMO, in that they give you a piece of memory and a pointer to it.

    Using std::vector is a different matter; aside from the cost, which I don’t think is relevant since you will use

    &vect[0]
    

    to access the underlying buffer like you would do with malloc, it has its advantages, like buffer extensibility and more access safety than a straight pointer; you might read the discussion here. I have used them in a project myself.

    std::strings have in principle the problem of the C++ (98, 03) standard not guaranteeing the contiguousness of the underlying buffer, but this does not seem to be an issue with compiler implementations. C++0x seems to have standardized buffer contiguousness (but I have not searched for that specifically). So you could also use them, but read before Herb Sutter’s comment here (it is not in the main article body, but in the third comment posted at the end of it).

    Anyway, between std::string and std::vector, I would choose std::vector.

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

Sidebar

Related Questions

You can specify one buffer for your file stream like that: char buf[BUFFER_SIZE]; std::ofstream
I need a binary search algorithm that is compatible with the C++ STL containers,
I have a class that I need to binary serialize. The class contains one
I need to be able to read a file format that mixes binary and
I need to convert a 20digits decimal to binary using C programming. What buffer
I am reading a binary file as: const size_t stBuffer = 256; char buffer[stBuffer];
I have a couple of binary files that are used in image processing where
I have a binary file that I need to insert a header at the
I need to store binary files to the SQL Server Database. Which is the
I need to read binary PGM image files. Its format: P5 # comments nrows

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.