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

Andrei
  • 8,606
  • 10
  • 35
  • 43
  • I suspect the answers going to depend on what you want to use the buffer for and how you want to use it. – forsvarir Aug 05 '11 at 07:24
  • Boost Asio uses/recommends vector of char for storing binary data. so I think it should not give any performance problems – Arunmu Aug 05 '11 at 07:28

3 Answers3

6

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.

Community
  • 1
  • 1
sergio
  • 68,819
  • 11
  • 102
  • 123
1

The difference between new and malloc is that new also calls the constructor (if there is any) for the entity it allocates memory to. But since char is a basic type using new or malloc makes no difference as char does not have a constructor. The same can be said about delete and free.

I would personally use a char* buffer (a char is 1 byte in memory):

char* buf = new char[100];

Also, remember when freeing the allocated buffer, to use a construct like:

delete[] buf;

to delete the array otherwise you will have memory leaks.

I would not personally use a std::string or a std::vector because:

  • a simple array of char seems faster to me
  • more "universal" (there are several implementations of std but an array of byte will always be the same)
  • and it might be easier to understand and debug

HTH,
JP

Community
  • 1
  • 1
Ioan Paul Pirau
  • 2,733
  • 2
  • 23
  • 26
  • To nitpick, you *can* value-construct integral types and arrays thereof (which default to 0), so `new char[100]()` should give you a zeroed-out array. Support for this may vary at the moment, though. Also note that `new` is an *expression*, while `malloc()` is a *function*. – Kerrek SB Aug 05 '11 at 08:16
  • Your point about it being faster is debatable. The speed difference is negligible at most, and you get better resource scoping and type safety. I'd argue that the cognitive burden of handling arrays is not worth the speed "gains" (if any). – Pablo Aug 05 '11 at 08:18
  • @Pablo: I would imagine that since std::string has attached logic it must be slower – Ioan Paul Pirau Aug 05 '11 at 08:25
0

It's depends on how your program work. Is a client or a server? Will send/receive fixed length request? Do you need to modify the data later? Do you want to manipulate as a string or not?

I'd use std::string since it's most flexible on this regards. But as you can see, it's not optimal.

shigeya
  • 4,862
  • 3
  • 32
  • 33