11

I'm facing problems with initializing a std::string variable using "" (i.e. an empty string). It's causing strange behavior in code that was previously working. Is the following statement wrong?

std::string operationalReason = "";

When I use the following code everything works fine:

std::string operationalReason;
operationalReason.clear();

I believe that string literals are stored in a separate memory location that is compiler-dependent. Could the problem I'm seeing actually be indicating a corruption of that storage? If so, it would get hidden by my usage of the clear() function.

Thanks.

5
  • Could the problem I'm seeing actually be indicating a corruption of that storage? <-- No. If it were, you would have numerous other problems such as massive system instability. There's no reason for the code you're giving to not work. Commented Mar 18, 2011 at 15:23
  • "corruption of that storage" - unlikely, but if operationalReason.size() is non-0 immediately after doing std::string operationalReason = "";, that's would prove that something is horribly wrong. If it is 0 then probably the string has been created OK. Commented Mar 18, 2011 at 15:34
  • @drachenstern: I tried using std::string operationalReason; but I'm still seeing operationalReason as a string with no printable characters and length 16. How weird is that? Commented Mar 21, 2011 at 8:42
  • @Steve Jessop: I guess something must be horribly wrong but I can't figure out what :( Commented Mar 21, 2011 at 8:43
  • ~ I would have to see a lot more code to understand why it's not working and as for why it allocates 16 bytes of memory on creation, I can only assume that's something in the compiler or the runtime (C++ still uses a runtime doesn't it?) Commented Mar 21, 2011 at 14:36

4 Answers 4

35
std::string operationalReason; //is enough!

It invokes the default constructor, and creates an empty string anyway.

So I would say std::string operationalReason = "" is overkill.

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

1 Comment

It doesn't call the default constructor if operationalReason is a local variable.
12

std::string operationalReason = "";

This is perfectly fine, technically, but more common and nice is just

std::string operationalReason;

The default ctor of the string will create an empty string

Yes, you are right about string literals being stored in a nonmutable memory blah blah etc etc... but the string copy-ctor always copies the string or C-string passed

1 Comment

Correct. no necessity to initialise the string with "", it is good to go already.
6

What happens if you just do std::string operationalReason;? That should have the same effect as the two examples you provided. If in fact you're experiencing problems when you use the std::string operationalReason = ""; form that may indicate that the string data storage has been corrupted, but it may equally mean that some OTHER part of memory is corrupted and that particular line causes it to manifest differently.

Does your code crash immediately when you use the "" form or later on at runtime? Are you able to run this under valgrind or similar to see if it spots memory problems? What happens if you initialized the string to some literal other than ""?

1 Comment

You have a point, it could be some other corruption manifesting itself in this way. The code doesn't crash after using = "". Moreover, the behaviour is still incorrect after using a simple declaration without the = "". I will see what I can find with valgrind.
-4

The two forms are corrected, but this one:

std::string operationalReason = ""

It calls the constructor which takes a const char * as argument. First it calls the default constructor and then it tries to copy the data, in this case nothing.

std::string operationalReason;

is preferable. You can use clear() to reset it to an empty string.

5 Comments

-1. The first one doesn't call operator=. That is not assignment!
Yes, it calls the constructor which takes a const char * as argument. First it calls the default constructor and then it tries to copy the data, in this case nothing.
No, to be strict, it doesn't call the default constructor, it uses another constructor that copies "all" chars from "". The result is the same though, an empty std::string.
-1 : It doesn't call the deault constructor. -1 : For suggesting that clear() is immediately required. std::string operationalReason; is already the empty string.
Thanks for your comment. I guess a declaration without initialization would create and empty string but I'm still seeing problems with that usage and which gets corrected by clear()'ing the string.

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.