Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

std::string as C++ byte array

Tags:

c++

Google's Protocol buffer uses the C++ standard string class std::string as variable size byte array (see here) similar to Python where the string class is also used as byte array (at least until Python 3.0).

This approach seems to be good:

  • It allows fast assignment via assign and fast direct access via data that is not allowed with vector<byte>
  • It allows easier memory management and const references, unlike using byte*.

But I am curious: Is that the preferred way for a byte arrays in C++? What are the drawbacks of this approach (more than a few static_casts)

like image 986
dmeister Avatar asked Jan 10 '10 13:01

dmeister


People also ask

How do I turn a string into a byte array?

There are two ways to convert byte array to String: By using String class constructor. By using UTF-8 encoding.

How many bytes is a std::string?

While std::string has the size of 24 bytes, it allows strings up to 22 bytes(!!) with no allocation.

Is byte array same as string?

Remember that a string is basically just a byte array For example, the following code iterates over every byte in a string and prints it out as both a string and as a byte.

What is std::string in C?

The std::string class manages the underlying storage for you, storing your strings in a contiguous manner. You can get access to this underlying buffer using the c_str() member function, which will return a pointer to null-terminated char array. This allows std::string to interoperate with C-string APIs.


1 Answers

Personally, I prefer std::vector, since std::string is not guaranteed to be stored contiguously and std::string::data() need not be O(1). std::vector will have data member function in C++0x.

like image 62
avakar Avatar answered Oct 13 '22 14:10

avakar