Boost logo

Boost Users :

From: Cliff Green (cliffg_at_[hidden])
Date: 2006-04-08 13:30:50


>> I'm interested in the actual internal layout of the data in the actual
std::string.
>
>The standard doesn't force any internal representation for std::string. It
could be a
>linked list of characters, if the implementation felt like being evil.
>
>I would expect, however, that most implementations wil basically keep it as
a null-terminated
>std::vector<char>-like internal representation for simplicity since c_str()
is unfortunatly
>still rampant.

Embedded null characters are fully allowed and supported as part of the
string object (read the C++ standard or any good C++ library book, such as
Josuttis) - so not only can you not assume that the internal representation
is null char terminated, you can't even assume there's no embedded null
chars elsewhere in the string. I've seen many programmers bitten by making
bad assumptions on string objects.

I used to internally use a std::string object for a "binary buffer" class
until I realized there's no guarantee on internal char array contiguousness
(as already noted) - so I started using a std::vector<char>, which does
guarantee contiguousness (per the first revision of the C++ standard).

Note that the "data()" method on the std::string class gives a pointer to a
buffer that is not guaranteed to be null char terminated ...

Cliff


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net