is data in c++ string class contiguous?
Posted on 2003-03-29
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.