54

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?

Alexis Côté
  • 3,670
  • 2
  • 14
  • 30
Charzhard
  • 783
  • 1
  • 7
  • 19
  • 19
    It isn't uninitialized. – chris Jul 19 '13 at 05:04
  • 2
    As others have noted, the default constructor has been called, but to answer the specific question, you could try `cout << name` and `cout << name.length()`. – Simon Jul 19 '13 at 05:06
  • 21
    @Simon trying something doesn't tell you if the behaviour's undefined or implementation defined (but it would have been a good start :-)). – Tony Delroy Jul 19 '13 at 05:10
  • @TonyD Note, however, that Simon has a point here. How about firing up the documentation? Doesn't even need to come up with sample code for that. (Or, for that matter, this is in every reasonably good beginner C++ guide...) –  Jul 19 '13 at 05:18
  • 3
    @H2CO3: documentation *obviously* - every answer already referenced that; but that point simply isn't made by Simon's comment, and plenty of people do write code with undefined or implementation defined behaviour accidentally because they try the code and it does/seems-to work so they don't look at the documentation. My comments actually arguing against *not* looking at documentation, indirectly. – Tony Delroy Jul 19 '13 at 05:24
  • @TonyD: +1. I agree that the practical test doesn't help if the behaviour is undefined or implementation-dependent. In this case, the behaviour was well-defined, but I agree there are plenty of situations where implementation-dependence is an issue. Actually, I'm just dealing with one at the moment in (cringe) Fortran 77 ... – Simon Jul 19 '13 at 05:34
  • Coming from Java and C# world, I just kept feeling "unsafe" when seeing definition like that with no default values. But yes, it is actually initialized to empty (`""`) string. – Lin Jul 22 '21 at 16:44

5 Answers5

73

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                                            |
+-----------------------------------------------------------------------------+
Community
  • 1
  • 1
Pierre Fourgeaud
  • 14,290
  • 1
  • 38
  • 62
20

It's not uninitialized, its default constructor is called.

From http://en.cppreference.com/w/cpp/string/basic_string/basic_string:

Default constructor. Constructs empty string.

Oliver Charlesworth
  • 267,707
  • 33
  • 569
  • 680
17

Default constructed user-defined types are not uninitialized. The default constructor defines an empty string (i.e "") with a size/length of zero.

Rapptz
  • 20,807
  • 5
  • 72
  • 86
5

The Standard (C++11, §21.4.2/1) describes the results of default-constructing a std::basic_string (of which std::string is a specialization) as follows:

[...] an object of class basic_string. The postconditions [...] are indicated in Table 63.

And Table 63 says:

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

jogojapan
  • 68,383
  • 11
  • 101
  • 131
  • 1
    What is the significance of the "can have 0 added to it" part? It seems out of the blue. Can't you add 0 to any non-null pointer? – vargonian Jan 02 '18 at 19:58
  • 2
    @vargonian: That doesn't mean the pointer's value has `0` mathematically added to it. It means the buffer pointed-to by the result of `data()` may have an additional `\0` byte at the end. It's very poor wording. – Lightness Races in Orbit May 09 '18 at 16:23
  • @vargonian or even to a null pointer? – DomQ Jan 04 '23 at 11:31
-9

value is null , and size is 0 But you can directly chk if the string is empty or not by empty()

Just in case you want to check that in your application , Do this

std::string name // Construct an empty string  
if(name.empty()) { // Check if its empty
  name="something";
}

Similar and more detailed discussion is here initializing strings as null vs. empty string

Community
  • 1
  • 1
Anand Rathi
  • 790
  • 4
  • 11
  • 1
    This doesn't merely check whether the string is empty. It assigns a value to it. Also, how does this answer the question? – jogojapan Jul 19 '13 at 06:16