Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does std::string really wrap up a C char array?

Tags:

c++

string

stl

I always thought an std::string was implemented as an STL wrapper for a C char array string. But looking closely at the design, I've noticed that it doesn't give any hint or sign of being a wrapped up c-string. For all I know an std::string could be doing anything internally!

There is the c_str() method of course which I thought returned the internal char array, but how do I know if the method doesn't create a new c char array from whatever data it stores inside and return it?

Seriously, how has std::string been implemented? Is it (as it appears to be) just a wrapper for a C char array, or is it something else? Or a mixture of the two? Or even can become both conditionally?

like image 237
ApprenticeHacker Avatar asked Mar 13 '12 04:03

ApprenticeHacker


Video Answer


1 Answers

For all I know an std::string could be doing anything internally!

For all you know. The standard, of course, describes and demands certain semantics that rule anything out. It says the following on the basic_string template:

§21.4 [basic.string] p1

The class template basic_string describes objects that can store a sequence consisting of a varying number of arbitrary char-like objects with the first element of the sequence at position zero. Such a sequence is also called a “string” if the type of the char-like objects that it holds is clear from context. In the rest of this Clause, the type of the char-like objects held in a basic_string object is designated by charT.

And a "char-like object" is defined by the following text:

§21.1 [strings.general] p1

This Clause describes components for manipulating sequences of any non-array POD (3.9) type. In this Clause such types are called char-like types , and objects of char-like types are called char-like objects or simply characters.

This effectively means that you can stuff anything you want into basic_string, as long as it's not an array and it is a POD (see this and this for infos on what PODs are). These char-like objects are then manipulated with the help of character traits, which define the specific behaviour of and relationship between them.


[...] but how do I know if the method doesn't create a new c char array from whatever data it stores inside and return it?

In C++03 exactly this was possible to do for the implementation, a known defect that has since been corrected in C++11:

§2.4.1 [string.require] p5

The char-like objects in a basic_string object shall be stored contiguously. That is, for any basic_string object s, the identity &*(s.begin() + n) == &*s.begin() + n shall hold for all values of n such that 0 <= n < s.size().

See also these related questions:

  • Is string::c_str() allowed to allocate anything on the heap?
  • Is it legal to write to std::string?
  • Is it reasonable to use std::basic_string<t> as a contiguous buffer when targeting C++03?
like image 94
Xeo Avatar answered Oct 07 '22 08:10

Xeo