Default capacity of std::string?

前端 未结 5 1391
灰色年华
灰色年华 2020-12-20 14:22

When I create a std::string using the default constructor, is ANY memory allocated on the heap? I\'m hoping the answer does not depend on the implementation and

5条回答
  •  无人及你
    2020-12-20 15:02

    No, but, and I don't know of any implementation that does allocate memory on the heap by default. Quite a few do, however, include what's called the short string optimization (SSO), where they allocate some space as part of the string object itself, so as long as you don't need more than that length (seems to be between 10 and 20 characters as a rule) it can avoid doing a separate heap allocation at all.

    That's not standardized either though.

提交回复
热议问题