Given the following code ---- #include <stdio.h> static const char* const a = "bla"; static const char b[] = "bla"; int main() { puts(a); puts(b); } ---- GCC 12.2 with -O2 produces ---- .LC0: .string "bla" main: sub rsp, 8 mov edi, OFFSET FLAT:.LC0 call puts mov edi, OFFSET FLAT:_ZL1b call puts xor eax, eax add rsp, 8 ret _ZL1b: .string "bla" ---- On the other hand, clang 15.0 with -O1 and higher produces ---- main: # @main push rbx lea rbx, [rip + .L.str] mov rdi, rbx call puts@PLT mov rdi, rbx call puts@PLT xor eax, eax pop rbx ret .L.str: .asciz "bla" ---- For some reason, GCC doesn't want to combine readonly data for these two string literals. Godbolt playground: https://godbolt.org/z/WaeazezE6
I think what clang did is an invalid transformation. You can use -fmerge-all-constants to get this same transformation in gcc.
Thanks for the pointer to -fmerge-all-constants, that helped me to google out why such transformation is invalid: https://stackoverflow.com/a/70328102 Should I close this issue as INVALID (and probably open one against clang)?
Strictly speaking, in this case I think it isn't invalid, puts is a standard function and implementation could treat it as a builtin with known behavior (that it doesn't care about pointer equality of what it prints). But clang++ does that even for static const char* const a = "bla"; static const char b[] = "bla"; extern void foo (const char *); int main() { foo(a); foo(b); } Another TU could have #include <stdlib.h> void foo (const char *p) { static const char *q; if (!q) q = p; else if (q == p) abort (); } This will fail even with GCC -fmerge-all-constants, but in that case we document that the option may cause non-conforming behavior. C++ says https://eel.is/c++draft/lex.string#9 and my reading of it is that different string literals can be overlapping or partially overlapping etc., but am not convinced it is ok to make those overlap other const objects. C 17 says: "String literals, and compound literals with const-qualified types, need not designate distinct objects." and "This allows implementations to share storage for string literals and constant compound literals with the same or overlapping representations." note, but that still doesn't look like allowing them to overlap non-string literal non-constant compound literals objects.
This case is tricky... `a` points to the string literal object which is usable in constant expression ([expr.const]/4.6), while the object `b` is not usable in constant expression. I'm afraid that there would be new issues involving constant evaluation if `b` and the string literal object are merged.
So, does "String literals, and compound literals with const-qualified types, need not designate distinct objects." apply here or not? If not, how does the case where it applies look like? https://en.cppreference.com/w/c/language/compound_literal explicitly says (though I do understand this is not a 100% reliable source): Compound literals of const-qualified character or wide character array types may share storage with string literals. ---- (const char []){"abc"} == "abc" // might be 1 or 0, implementation-defined ---- Comparison with string literals isn't actually implementation-defined but instead unspecified behavior, but the point here is that they think it is possible memory is shared.
(In reply to Marat Radchenko from comment #5) > So, does "String literals, and compound literals with const-qualified types, > need not designate distinct objects." apply here or not? If not, how does > the case where it applies look like? I strongly believe that when compiling as C, the objects in your example are allowed to be merged. But I'm not sure whether this is applicable to C++. In C++ one may write: constexpr char x = a[0]; // OK. if constexpr (a == b) { // If the objects are merged then the following code is in affect. constexpr char y = b[0]; // Error! Because `b` is not usable in constant expressions. } I'm afraid that it would be unreasonable for compiler to handle such confusing case. > https://en.cppreference.com/w/c/language/compound_literal explicitly says > (though I do understand this is not a 100% reliable source): > > Compound literals of const-qualified character or wide character array types > may share storage with string literals. > > ---- > (const char []){"abc"} == "abc" // might be 1 or 0, implementation-defined > ---- > > Comparison with string literals isn't actually implementation-defined but > instead unspecified behavior, but the point here is that they think it is > possible memory is shared. I've corrected the comment on cppreference. Now it's saying "unspecified".
While playing with it more, I found that clang behaves in a very strange way. While they do combine `const char* const` + `const char[]`, the DO NOT combine two `const char[]` together. I don't have any explanation for that. Reported as https://github.com/llvm/llvm-project/issues/60476.
Also, quote from C17 standard: Like string literals, const-qualified compound literals can be placed into read-only memory and *can even be shared*. (6.5.2.5 p 13).
See also CWG2753. https://cplusplus.github.io/CWG/issues/2753.html