28

I am writing a C++ application and I was wondering what the C++ conventional way of storing a byte array in memory.

Is there something like a string, except specifically made for binary data.

Right now I am using a *unsigned char** array to store the data, but something more STL/C++ like would be better.

lothar
  • 19,853
  • 5
  • 45
  • 59
The Unknown
  • 19,224
  • 29
  • 77
  • 93

5 Answers5

40

I'd use std::vector<unsigned char>. Most operations you need can be done using the STL with iterator ranges. Also, remember that if you really need the raw data &v[0] is guaranteed to give a pointer to the underlying array.

CAdaker
  • 14,385
  • 3
  • 30
  • 32
24

You can use std::string also for binary data. The length of the data in std::string is stored explicitly and not determined by null-termination, so null-bytes don't have special meaning in a std::string.

std::string is often more convenient than std::vector<char> because it provides many methods that are useful to work with binary data but not provided by vector. To parse/create binary data it is useful to have things like substr(), overloads for + and std::stringstream at your disposal. On vectors the algorithms from <algorithm> can be used to achieve the same effects, but it's more clumsy than the string methods. If you just act on "sequences of characters", std::string gives you the methods you usually want, even if these sequences happen to contain "binary" data.

sth
  • 222,467
  • 53
  • 283
  • 367
  • 5
    or std::vector – lothar May 07 '09 at 23:18
  • I would also use std::vector. – xian May 07 '09 at 23:24
  • 1
    std::vector has the advantage that one can get its contents as a /writable/ char pointer (by &vec[0]). On the other hand, you don't get the copy-on-write optimization common in std::string implementations. – bdonlan May 07 '09 at 23:56
  • 5
    @bdonlan: A writable buffer from &obj[0] is also true for std::string implicitly through the other requirements. In fact, 0x makes this explicit. –  Sep 15 '10 at 20:33
21

You should use std::vector<unsigned char> or std::vector<uint8_t> (if you have a modern stdint.h header). There's nothing wrong with using unsigned char[] or uint8_t[] if you are working with fixed size buffers. Where std::vector really shines is when you need to grow or append to your buffers frequently. STL iterators have the same semantics as pointers, so STL algorithms will work equally well with std::vector and plain old arrays.

And as CAdaker pointed out, the expression &v[0] is guaranteed to give you the underlying pointer to the vector's buffer (and it's guaranteed to be one contiguous block of memory). This guarantee was added in an addendum to the C++ standard.

Personally, I'd avoid using std::string to manipulate arbitrary byte buffers, since I think it's potentially confusing, but it's not an unheard of practice.

Don McCaughey
  • 9,532
  • 3
  • 30
  • 36
1

There are multiple solutions but the closest one (I feel) is the std::vector<std::byte>> because it expresses the intent directly in code.

From : https://en.cppreference.com/w/cpp/types/byte

std::byte is a distinct type that implements the concept of byte as specified in the C++ language definition.

Like char and unsigned char, it can be used to access raw memory occupied by other objects (object representation), but unlike those types, it is not a character type and is not an arithmetic type. A byte is only a collection of bits, and the only operators defined for it are the bitwise ones.

Sitesh
  • 1,816
  • 1
  • 18
  • 25
0

how about std::basic_string<uint8_t> ?

libgcc
  • 153
  • 3
  • 7