Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is std::string ref-counted in GCC 4.x / C++11?

Is std::string reference-counted when using gcc 4 with -std=c++0x or -std=c++11?

like image 348
Drew Dormann Avatar asked Sep 20 '12 20:09

Drew Dormann


People also ask

Can you use 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.

Does std::string store size?

std::string actually maintains the size as one of its data member.

What does std::string () do?

std::string class in C++ C++ has in its definition a way to represent a sequence of characters as an object of the class. This class is called std:: string. String class stores the characters as a sequence of bytes with the functionality of allowing access to the single-byte character.

How many bytes is a std::string?

Example. In below example for std::string::size. The size of str is 22 bytes.


2 Answers

Looking at libstdc++ documentation I find (see the link for more info):

A string looks like this:

                       [_Rep]                        _M_length [basic_string<char>]   _M_capacity _M_dataplus            _M_refcount _M_p ----------------> unnamed array of char_type 

So, yes it is ref counted. Also, from the discussion here:

Yes, std::string will be made non-reference counting at some point, but as a non-reference-counted string is valid in C++98 as well, one option would be to switch to a non-ref-counted string for both -std=c++98 and -std=c++11 modes. I'm not saying that's what will happen, but it could be.

So, it seems there are plans to change it to be conforming (I don't know how the progress is going though).

Update As emsr points out in the comments, there is currently a non-reference counted extension called vstring.h, and it seems the only reason it hasn't replaced std::string is because of ABI compatibility. There is an SO question about it here.

like image 195
Jesse Good Avatar answered Sep 20 '22 20:09

Jesse Good


C++11 added specific language forbidding std::string from being reference counted. So if it is, then it's a pretty significant failing in GCC's C++11 standard library.

like image 38
Nicol Bolas Avatar answered Sep 21 '22 20:09

Nicol Bolas