Value and size of an uninitialized std::string variable in c++

Charzhard picture Charzhard · Jul 19, 2013 · Viewed 48.6k times · Source

If a string is defined like this

std::string name;

What will be the value of the uninitialized string "name" and what size it would be?

Answer

Pierre Fourgeaud picture Pierre Fourgeaud · Jul 19, 2013

Because it is not initialized, it is the default constructor that is called. Then :

empty string constructor (default constructor) :

Constructs an empty string, with a length of zero characters.

Take a look : http://www.cplusplus.com/reference/string/string/string/

EDIT : As stated in C++11, §21.4.2/1 :

Effects: Constructs an object of class basic_string. The postconditions of this function are indicated in Table 63.

-> Table 63
+-----------------------------------------------------------------------------+
| data()     | a non-null pointer that is copyable and can have 0 added to it |
+------------+----------------------------------------------------------------+
| size()     | 0                                                              |
+------------+----------------------------------------------------------------+
| capacity() | an unspecified value                                            |
+-----------------------------------------------------------------------------+