1

When declaring std::string cpp{}; does this call new/malloc?

Assume we already have a const char* c. Is it possible to move the contents from c to cpp without extra allocations?

5
  • 5
    Yes. A copy is made of the source string. Use a std::string_view to do it without dynamic allocations. No, you cannot move anything. The original string exists in a read-only part of your program’s memory for the lifetime of your program. This is normal. Commented Aug 18, 2024 at 23:52
  • @Dúthomhas Thanks! Not sure what you mean by normal but I think this answers my question. I would have thought that const_cast + string from char*&& should work but ok. Thanks again! Commented Aug 18, 2024 at 23:56
  • 1
    It is normal for initialization data to be stored in a read-only location in your program’s image, only to be copied when variables are initialized, regardless of the variable’s lifetime. This does not generally incur any significant penalty on your program’s RAM usage. Commented Aug 19, 2024 at 0:39
  • 1
    @SpeakX does this call new/malloc? -- No, for most compilers, this does not allocate memory at all. As a matter of fact, if the string is short, no memory is allocated. See Small String Optimization (SSO). Commented Aug 19, 2024 at 2:24
  • 1
    @Dúthomhas "The original string exists in a read-only part of your program’s memory for the lifetime of your program" - there is nothing in the question or comments to suggest the const char* pointer in question is pointing at read-only memory, such as a string literal. There are plenty of legit reasons why there is a const pointer to mutable memory. There is not enough context given to make your assumption Commented Aug 19, 2024 at 2:49

1 Answer 1

8

When declaring std::string cpp{}; does this call new/malloc?

That depends on the particular std::string implementation, but likely not. Nothing stops the implementation from providing a default capacity dynamically, but like most things in C++, you don't pay for what you don't need, so they likely will not preallocate dynamic memory on a default-constructed string. Especially if Short String Optimization (SSO) is implemented.

Assume we already have a const char* c. Is it possible to move the contents from c to cpp without extra allocations?

Move, never. std::string can only ever move from another std::string object.

In the case of a const char*, std::string will always copy the characters into its own memory.

Whether or not that copy will allocate memory dynamically depends on 2 factors:

  • whether or not std::string implements SSO. Most vendors do nowadays.

  • whether or not the content of the const char * fits entirely inside the SSO buffer, if implemented.

If both conditions are true, then no memory is allocated dynamically. Otherwise, memory is allocated dynamically.

Sign up to request clarification or add additional context in comments.

1 Comment

Since C++17 std::string's default constructor is also marked noexcept. So even if the implementation attempted to dynamically allocate in it, it would always have to have a fallback strategy because it is not allowed to propagate an allocation exception.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.