The default value is std :: string?

When I create std :: string using the default constructor, ANY memory allocated on the heap? I hope the answer is implementation independent and standardized. Consider the following:

std::string myString; 
+7
source share
5 answers

Unfortunately, the answer does not match N3290.

Table 63 Page 643 says:

  • data() non-null pointer that can be copied and can add 0 to it
  • size() 0
  • capacity() undefined value

The table is identical for C ++ 03.

+8
source

It depends on the implementation. Some string implementations use a small amount of automatically distributed storage for small strings, and then dynamically allocate more for large strings.

+2
source

No, but I do not know of any implementation that by default allocates memory on the heap. However, some of them include what is called short string optimization (SSO), where they allocate some space as part of the string object itself, so if you don't need more than that length (it seems between 10 and 20 characters, usually) , it can generally avoid allocating a separate heap.

It is not standardized though.

+2
source

It depends on the compiler. Take a look here, there is a good explanation:

http://www.learncpp.com/cpp-tutorial/17-3-stdstring-length-and-capacity/

0
source

Generally, yes they allocate memory on the heap. I will give an example: c_str() requires the end character of the NULL character '\ 0'. Most implementations highlight this NUL \0 earlier as part of the string. This way you get at least one byte, often more.

If you really need a specific behavior, I would suggest writing your own class. Buffer / string classes are not so difficult to write.

-one
source

All Articles