0

Stroustrup writes that

string is usually implemented using the short-string optimization. That is, short string values are kept in the string object itself and only longer strings are placed on free store. How many characters can a ‘‘short’’ string have? That’s implementation defined, but ‘‘about 14 characters’’ isn’t a bad guess.

But is there a way to find out max "short" string size for my compiler?

Johy
  • 263
  • 2
  • 10
  • You can look at the standard library code that shipped with your compiler. – NathanOliver Jan 07 '21 at 14:48
  • *But is there a way to find out max "short" string size for my compiler?* -- The best bet is to look at the source code. It is possible that the compiler may do different things based on optimization settings. – PaulMcKenzie Jan 07 '21 at 14:48
  • There might be a scheme with using a `std::basic_string` with a custom allocator, and adding characters to the string until the allocator is asked for storage. – François Andrieux Jan 07 '21 at 14:49
  • 1
    You could keep adding characters to your string until the address returned by `std::string::data()` is no longer inside the string object itself. – Galik Jan 07 '21 at 14:55
  • just `std::string s("a"); std::cout << s.capacity()` – phuclv Jan 07 '21 at 16:54

0 Answers0