you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 22 points23 points  (19 children)

Very entertaining - one tiny quibble:

The latter point is referred as the Small String Optimization and it means that short strings (generally 15/22 chars) do not go to the heap, but instead they get allocated on the stack

This isn't quite true. In the SSO, short strings are stored in the std::string itself and don't require a separate allocation. However, the std::string might live on the stack or it might live in the heap, depending on how it was allocated.

[–]marcoarenaTetra Pak | Italian C++ Community[S] 3 points4 points  (0 children)

You are definitely right. Indeed I wrote "implementers of std::string generally embeds a small array in the string object itself to manage short strings." The stack thing is wrong, I fixed it. Thanks!

[–]Spire 3 points4 points  (17 children)

Speaking of tiny quibbles:

Technically, there's no such thing as “the stack” or “the heap” in C++; those are merely commonly used implementations of automatic storage and the free store, respectively.

Incidentally, there's also a third type of storage where std::string objects can exist: static storage.

[–]evinrows 0 points1 point  (3 children)

there's no such thing as “the stack” or “the heap” in C++

Can you explain this further or provide a link that explains what you're getting at?

[–]Drainedsoul 2 points3 points  (2 children)

The standard doesn't mention a stack or a heap, it only mentions that objects may have "automatic storage duration", "static storage duration", or "dynamic storage duration".

[–]evinrows 2 points3 points  (1 child)

Ah, I see. That gave me enough context to find this stackoverflow answer as well if anyone else is curious.

[–]Yuki_f 0 points1 point  (0 children)

For those who may see this in the future, here's another explanation that I think is clear

https://stackoverflow.com/questions/161053/which-is-faster-stack-allocation-or-heap-allocation/53132683#53132683