3

If I use

const char * str = "Hello";

there is no memory allocation/deallocaton needed in runtime

If I use

const std::string str = "Hello";

will be there an allocation via new/malloc inside string class or not? I could find it in assembly, but I am not good at reading it.

If answer is "yes, there will be malloc/new", why? Why can there be only pass through to inner const char pointer inside std::string and do actual memory allocation if I need to edit edit string?

3
  • Doesn't that depend on your implementation? Commented Feb 28, 2015 at 17:07
  • 1
    Please be aware that the two code samples are not equivalent, as the second allows the string to be modified. Think about the meanings of these also: char str[] = "Hello"; or const std::string str = "Hello"; or even static const std::string str = "Hello". Commented Feb 28, 2015 at 18:46
  • @NeilKirk You are right, I have edited answer Commented Feb 28, 2015 at 19:28

3 Answers 3

5

will be there an allocation via new/malloc inside string class or not?

It depends. The string object will have to provide some memory to store the data, since that's its job. Some implementations use a "small string optimisation", where the object contains a small buffer, and only allocates from the heap if the string is too large for that.

Why can there be only pass through to inner const char pointer inside std::string and do actual memory allocation if I need to edit edit string?

What you describe isn't necessarily an optimisation (since it needs an extra runtime check whenever you modify the string), and in any case isn't allowed by the iterator invalidation rules.

There is a proposal for a string_view, allowing you to access an existing character sequence with an interface like const string, without any memory management. It's not yet standard, and doesn't allow you to modify the string.

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

1 Comment

string_view looks interesting, since I have most of the time const strings - filled once and almost never modified (because of almost, const char * is not the best solution for me)
1

Naive implementation of std::string will require a heap allocation however compilers are allowed to optimize statically initialized std::string objects by replacing them with objects of alternative implementations if the initialized strings are not modified during runtime.

You may use const std::string when you instantiate immutable strings to ensure better optimization.

3 Comments

Define "maximum optimization". In a function static cosnt std::string would be even more "optimized", in terms of potentially less dynamic allocations per call of function.
You are right. My approach is in favor of the stack, not the whole memory space.
You assume that the string data will be on the stack.
0

The C++ standard doesn't actually say you can't just store a pointer to an external string (and a length). However, that means EVERY time you may modify the string (e.g. char& std::string::operator[](size_t index)) would have to ensure that the string is actually writeable. Since a large number of string usage does NOT use a constant string only to store the string, but does indeed modify the string [or use a string that isn't a constant input anyway].

So, some problems are;

std::string s = "Hello";
char &c = s[1]; 
c = 'a';    // Should make string to "Hallo". 

what if:

char buffer[1000];
cin.getline(buffer);   // Reads "Hello"
std::string s = buffer;
cin.getline(buffer);   // Reads "World"

What is the value in s now?

There are so many such cases where if you were to just copy the original string, it would cause more problems, and little or no benefit.

2 Comments

Because my strings are in 90% cases read only (I use them as hash keys), additional checks are OK for me. For your second example... is is not const char *, so I expect there will be copy made to std::string and buffer is not encapsulated in the string s
It would of course be possible to write your own class "const_string" or similar that indeed doesn't allow modification, and uses a pointer. Given that there are only a few operations allowed for such a string, it wouldn't take that long to write, I'd have thought.

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.