Default capacity of std::string?

前端 未结 5 1387
灰色年华
灰色年华 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:01

    Generally, yes they allocate memory on the heap. I'll give an example: c_str() requires a NULL trailing character '\0'. Most implementations allocate this NUL \0 ahead of time, as part of the string. So you'll get at least one byte allocated, often more.

    If you really need specific behavior I'd advise writing your own class. Buffer/string classes are not that hard to write.

提交回复
热议问题