Literally: string literals – part 1

String literals is a topic that often comes up when writing critical sections of code. Usually most developers don’t care too much about literals, since in general, they just work. But then you might do some profiling and realize you’re doing excessive amounts of calls to string related functions.

Ordinary string literals (aka narrow string literals) are an array of n const char. A string literal also has static storage duration. A reason why they need to be “const”, is that the standard hints that they may be optimized. Meaning they shouldn’t be modified:


Whether all string literals are distinct (that is, are stored in nonoverlapping objects) and whether successive evaluations of a string-literal yield the same or a different object is unspecified. [ Note: The effect of attempting to modify a string literal is undefined. —end note “

standard [lex.string])
Continue readingLiterally: string literals – part 1