https://godbolt.org/z/cyBiWY
I can see two 'some'
literals in assembler code generated by MSVC, but only one with clang and gcc. This leads to totally different results of code execution.
static const char *A = "some";
static const char *B = "some";
void f() {
if (A == B) {
throw "Hello, string merging!";
}
}
Can anyone explain the difference and similarities between those compilation outputs? Why does clang/gcc optimize something even when no optimizations are requested? Is this some kind of undefined behaviour?
I also notice that if I change the declarations to those shown below, clang/gcc/msvc do not leave any "some"
in the assembler code at all. Why is the behaviour different?
static const char A[] = "some";
static const char B[] = "some";
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…