I have a lot of literal strings in my source code that are otherwise identical except for leading white-spaces (due to a desire to maintain correct indentation). Are compilers smart enough to see that it can reuse the space in memory for both, just offsetting one string by a couple bytes?
String literals are stored in C as an array of chars, terminted by a null byte. A null byte is a char having a value of exactly zero, noted as '\0'. Do not confuse the null byte, '\0', with the character '0', the integer 0, the double 0.0, or the pointer NULL.
The characters of a literal string are stored in order at contiguous memory locations. An escape sequence (such as \\ or \") within a string literal counts as a single character.
The only difference is that you cannot modify string literals, whereas you can modify arrays. Functions that take a C-style string will be just as happy to accept string literals unless they modify the string (in which case your program will crash).
String literals are not stored in the heap or the stack, they are stored directly in your program's binary. Literally embedded in the binary, and the reference is a reference to the location in the binary. They're in a section of your program's binary.
ISO c99 6.5.2.5 Compound literals
83) This allows implementations to share storage for string literals and constant compound literals with the same or overlapping representations.
Short answer: probably.
Long answer: it depends on the implementation. Typically, C compilers have an optimizer feature called "string pool" or similar, which enables the compiler to store all string literals adjacently in ROM.
The contents of that string pool may then be optimized, the very same string appearing twice will almost certainly get optimized out. I think that most compilers will also be smart enough to recognize sub strings. But there are also platform considerations such as alignment, so just because there exists a sub string, it doesn't necessarily mean that it will be most effective to re-use that memory location.
There is nothing in the C standard that guarantees that such optimizations are done. But at the same time, there is nothing in the standard preventing it either.
To be sure, you have to check your specific compiler's documentation, or disassemble your program, or check the linker output.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With