When std :: string distribution is executed during initialization

When I create a member variable std::stringin the class, will the default memory be already allocated when it is created? Or std::stringdelay the selection until actual use?

I am using Visual Studio 2010 at the moment, and I noticed when I create an empty string using

  std::string s0;
  std::string s1 = "";

Both have a capacity of == 15. Does this mean that it has already allocated 15 bytes of memory? If so, can I prevent this from being allocated?

Will this implementation be specific to different compilers?

+4
source share
4 answers

The initial empty capacity is std::stringnot defined by the standard and depends on the implementation.

16 ( , ).

+4

VS2010, VS2012, , ( 15, )

( ), , Small String Optimization 15. .

MS ++ , , , . -. VS, , . , , Visual Studio - . _ITERATOR_DEBUG_LEVEL visual studio , . , "Release" 2010 , , , "_ITERATOR_DEBUG_LEVEL" "_SECURE_SCL".

+2

.. , stackoverflow .

Original answer:

What you see is SSO (Small Line Optimization) in practice. This is really a concrete implementation.

See this answer for more details .

+1
source

The compiler decides how much memory should be initialized for the string. Different compilers will allocate different amounts of memory - this is a common suggestion.

0
source

Source: https://habr.com/ru/post/1608319/


All Articles