Link to home
Start Free TrialLog in
Avatar of Posit
Posit

asked on

is data in c++ string class contiguous?

Is the data in the STL's string class guaranteed to be contiguous?

I'm asking because I'm just starting to do spot optimization of C++ code using inline assembly language. What I've tried so far is using the string class's data() member function to return a pointer to the first character in the string. This pointer and the length of the string is then passed to an assembly routine, which performs various manipulations on the string. So far it's working fine on all the fairly short strings I've tried.

There are a few places that mention that the C++ string class encapsulates C-style strings, and c-style string are just character arrays which are contiguous in memory. But I want to make sure that the characters in the STL's string class are guaranteed to be contiguous in memory. If not, these assembly routines are going to have problems in the future.
Avatar of bcladd
bcladd

data() returns a char* pointing at contiguous memory that contains the data of the string. c_str() does the same thing after making sure there is a \0 at the end of the string in use. Note that the pointer is not valid across modification of the string.
ASKER CERTIFIED SOLUTION
Avatar of efn
efn

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of dpearson
efn's summarized this nicely.  As a practical consideration, the memory layout of different STL implementations of std::string is *not* the same.

I remember an excellent presentation by Scott Meyers (of Effective C++ fame) where he analyzed the memory performance of different std::string classes from different (and commonly used) STL implementations.  They're all over the place.  For example, some use one layout for short strings (<= 15 chars) and another for longer strings.  There's all kinds of jiggery-pokery going on.

So this is a case where the STL implementations really do differ and making assumptions about the memory layout very well may mean that running a different version of your compiler (e.g. Visual C++ 7 vs Visual C++ 6) may get you a completely different result (because they usually plug-in a different STL implementation with each release), never mind mixing across platforms.

Doug
Avatar of Posit

ASKER

The STL implementation is VC++ 6.0.

It sounds like it may be better to convert a string class object to a C-style char array before using inline assembly on it. I was hoping to avoid the overhead of doing this, though, since the whole point of using inline assembly was to optimize performance. I would rather be certain, however, than risk "undefined behavior"