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?

theraju
  • 461
  • 4
  • 13
  • 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? – 101010 Jun 11 '15 at 22:25
  • 3
    [This](http://stackoverflow.com/questions/1287306/difference-between-string-and-char-types-in-c) may help you understand.....Personally, I would use std::string..... – ha9u63a7 Jun 11 '15 at 22:25
  • 1
    @101010: The, ehm, what? "Object fingerprint"? What's that, then? – Lightness Races in Orbit Jun 11 '15 at 22:35
  • @LightnessRacesinOrbit I mean size with `std::string` you carry an extra size attributed to the class. – 101010 Jun 11 '15 at 22:40
  • 1
    In this [demo](http://coliru.stacked-crooked.com/a/a065257f8e51aaf2) you can see that with `std::string` you carry an extra burden of 32 bytes which is attributed to the class.` – 101010 Jun 11 '15 at 22:55
  • @LightnessRacesinOrbit: Oh yes! :) – 101010 Jun 11 '15 at 23:09
  • @101010: Well okay then. BRB calling the bytes police – Lightness Races in Orbit Jun 11 '15 at 23:11
  • @101010 thanks for the sizeof demo. I really wanted to understand how much room one takes compared to the other. I don't think we have such strict limitations in our case, but good to know what the size tradeoff is. – theraju Jun 12 '15 at 04:18

3 Answers3

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).

Puppy
  • 144,682
  • 38
  • 256
  • 465
  • 7
    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... – Synxis Oct 29 '15 at 14:34
  • 1
    -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](https://en.cppreference.com/w/cpp/language/siof), like @Synxis mentioned. – micha Feb 11 '22 at 18:32
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.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
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[].

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
maayanwis
  • 5
  • 3
  • 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...) – underscore_d Dec 13 '15 at 13:14