4

I want to use a string constant in multiple places in my cpp file. Should I use std::string or char[]?

static const std::string kConstantString = "ConstantStringValue";

static const char kConstantString[] = "ConstantStringValue";

I was told to prefer the latter since it "avoids static allocation". Doesn't the char array also have to be statically allocated?

8
  • The main reason I would prefer static const char[] is to avoid the object fingerprint of std::string. Are you dealing with such strict limitations? Commented Jun 11, 2015 at 22:25
  • 3
    This may help you understand.....Personally, I would use std::string..... Commented Jun 11, 2015 at 22:25
  • 1
    @101010: The, ehm, what? "Object fingerprint"? What's that, then? Commented Jun 11, 2015 at 22:35
  • @LightnessRacesinOrbit I mean size with std::string you carry an extra size attributed to the class. Commented Jun 11, 2015 at 22:40
  • 1
    In this demo you can see that with std::string you carry an extra burden of 32 bytes which is attributed to the class.` Commented Jun 11, 2015 at 22:55

3 Answers 3

4

Yes, yes it does also have to be statically allocated.

Always use std::string unless your profiler tells you that it's worth pissing around with legacy crap like const char[]. Choosing const char[] is an annoying micro-optimization over std::string and a stupid decision unless you know for sure that this piece of code is a hotpath (since it's static either way I highly doubt it).

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

2 Comments

Downvoted. Using static const std::string is a recipe for disaster because of the static initialization order fiasco, someone might inadvertely use it in other static constants. I actually saw it happens several times...
-1: No, no allocations are happening. Static const char* strings will get a special location in the binary (RO section). Nothing got malloc'd, it's simply "there" to be accessed. Static std::string does require static initialization and thus you're opening yourself up for SIOF, like @Synxis mentioned.
4

Simply define a pointer to this string literal.:) It can have static storage specifier.

There is no any need to use class std::string. It is simply redundant and uselessly. This constant always can be converted to std::string if it is indeed required.

1 Comment

Aside from all the reasons you forgot/ignored, sure.
0

Since it's a static const, you won't be able to do any manipulation on this string anyway, so it is better to just use const char[].

1 Comment

What about read-only 'manipulation'? Things like getting the length or concatenating it into another string would be a lot easier if one could use the std::string functions rather than trudging through strcat() et al. Sure, if it's just a label and won't be entered into any manipulation, then maybe the char is fine - but let's not oversimplify (like all the other answers here...)

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.