1

I just started studying C++, but there is a thing that I don't fully understand: when you initialize a std::string variable with a string literal, like this:

std::string str = "some string";

We have the literal "some string", which is immutable.

Then we have the variable str that stores "some string" and is mutable. You can go there and append another string to it, change letters, etc.

The whole point is: when we initialize str with this literal, it does a modifiable copy of the literal to this variable? If so, that means that in memory, there are two spaces: one to the "some string" literal and another for the modifiable "some string"?

And, just to make it clear: a value is a literal just if it is hard-coded in the source code, is it not?

6
  • 1
    The standard did not define anything about that! It might be, that the string after init is only a pointer to the "original" and if it should be modified it will be copied/changed. BTW: Most implementations use a short string optimization which stores the string inside the string object without allocating heap. As you can see: It depends :-) Commented Jul 20, 2024 at 18:30
  • In your code "some string" is a constant. It won't consume any memory by itself until it is stored in your variable i.e., str. If you do not initialize str, then it theoretically won't use memory and probably will be eliminated by compiler during optimization if it is not used anywhere in your code. Commented Jul 20, 2024 at 18:42
  • Let's add some more confusion: const std::string read_only_str = "read_only"; Commented Jul 20, 2024 at 19:15
  • Yes, there will be two copies. See gcc.godbolt.org/z/vzK8Ej47a you can see the fixed string. It’s assigned to the global variable which has a copy that can be modified. Commented Jul 20, 2024 at 23:53
  • Assigning a string literal to std::string Commented Jul 21, 2024 at 6:07

2 Answers 2

5

"some string" is a string literal of type const char[12]. In certain contexts, like in a function parameter, an array decays into a pointer to its 1st element. In this case, to a const char* pointer.

std::string str = "some string"; is just syntax sugar, it is functionally identical to std::string str("some string"); It calls the std::string constructor which takes a const char* as input. That constructor does indeed make a copy of the character data into its own memory. Depending on your compiler's std::string implementation, that will either be in:

  • dynamic memory held outside of the std::string object, but pointed at by an internal member of the object.

  • an SSO (Short String Optimization) buffer located in automatic memory within the std::string object itself.

So yes, there will be 2 copies of "some string" in memory, somewhere.

And yes, a literal is a value that is hard-coded in the source code itself. The compiler will translate the value into the appropriate type and memory storage as needed.

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

7 Comments

I was under the impression that since C++11 SSO is not possible anymore. Am I wrong?
@bolov Yes, you are wrong. Just about every modern implementation uses SSO (with various buffer sizes, depending on implementation). Maybe you are thinking of COW (Copy-On-Write) instead?
yes, I was mixing things up and thinking about COW. Thank you for the clarification.
Will anything be done about the first "some string"? Won’t it be gotten rid of? Yes??
@ChukwujiobiCanon string literals exist for the lifetime of the program, see stackoverflow.com/questions/349025
|
0

it does a modifiable copy of the literal to this variable? If so, that means that in memory, there are two spaces: one to the "some string" literal and another for the modifiable "some string"?

You got it. But the compiler is free to make optimizations. At runtime, with retail/relase optimizations turned out (e.g. -O2), the compiler can make smart determinations if it really needs to copy the string - especially if s never actually gets mutated. But as a programmer, work with the assumption that the compiler isn't as smart as you.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.