Why C++ merging string literals on compilation
Clash Royale CLAN TAG#URR8PPP
up vote
6
down vote
favorite
https://godbolt.org/z/cyBiWY
I can see two 'some'
literals in assembler code generated by MSVC, but only one with clang and gcc. That results in a 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 compilation outputs? Why clang/gcc optimize something when no optimizations are requested? Is it some kind of UB?
c++ visual-c++ g++ clang++
add a comment |Â
up vote
6
down vote
favorite
https://godbolt.org/z/cyBiWY
I can see two 'some'
literals in assembler code generated by MSVC, but only one with clang and gcc. That results in a 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 compilation outputs? Why clang/gcc optimize something when no optimizations are requested? Is it some kind of UB?
c++ visual-c++ g++ clang++
1
Compilers are allowed to save space for literal string constants, by keeping only one single copy of a specific literal.
– Some programmer dude
54 mins ago
add a comment |Â
up vote
6
down vote
favorite
up vote
6
down vote
favorite
https://godbolt.org/z/cyBiWY
I can see two 'some'
literals in assembler code generated by MSVC, but only one with clang and gcc. That results in a 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 compilation outputs? Why clang/gcc optimize something when no optimizations are requested? Is it some kind of UB?
c++ visual-c++ g++ clang++
https://godbolt.org/z/cyBiWY
I can see two 'some'
literals in assembler code generated by MSVC, but only one with clang and gcc. That results in a 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 compilation outputs? Why clang/gcc optimize something when no optimizations are requested? Is it some kind of UB?
c++ visual-c++ g++ clang++
c++ visual-c++ g++ clang++
asked 57 mins ago
Eugene Kosov
1138
1138
1
Compilers are allowed to save space for literal string constants, by keeping only one single copy of a specific literal.
– Some programmer dude
54 mins ago
add a comment |Â
1
Compilers are allowed to save space for literal string constants, by keeping only one single copy of a specific literal.
– Some programmer dude
54 mins ago
1
1
Compilers are allowed to save space for literal string constants, by keeping only one single copy of a specific literal.
– Some programmer dude
54 mins ago
Compilers are allowed to save space for literal string constants, by keeping only one single copy of a specific literal.
– Some programmer dude
54 mins ago
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
9
down vote
accepted
This is not UB, but implementation-defined. For string literals,
The compiler is allowed, but not required, to combine storage for equal or overlapping string literals. That means that identical string literals may or may not compare equal when compared by pointer.
That means the result of A == B
might be true
or false
, which you shouldn't depend on.
add a comment |Â
up vote
2
down vote
Whether or not a compiler chooses to use the same string location for A
and B
is up to the implementation. Formally you can say that the behaviour of your code is implementation-defined.
Both choices implement the C++ standard correctly.
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
9
down vote
accepted
This is not UB, but implementation-defined. For string literals,
The compiler is allowed, but not required, to combine storage for equal or overlapping string literals. That means that identical string literals may or may not compare equal when compared by pointer.
That means the result of A == B
might be true
or false
, which you shouldn't depend on.
add a comment |Â
up vote
9
down vote
accepted
This is not UB, but implementation-defined. For string literals,
The compiler is allowed, but not required, to combine storage for equal or overlapping string literals. That means that identical string literals may or may not compare equal when compared by pointer.
That means the result of A == B
might be true
or false
, which you shouldn't depend on.
add a comment |Â
up vote
9
down vote
accepted
up vote
9
down vote
accepted
This is not UB, but implementation-defined. For string literals,
The compiler is allowed, but not required, to combine storage for equal or overlapping string literals. That means that identical string literals may or may not compare equal when compared by pointer.
That means the result of A == B
might be true
or false
, which you shouldn't depend on.
This is not UB, but implementation-defined. For string literals,
The compiler is allowed, but not required, to combine storage for equal or overlapping string literals. That means that identical string literals may or may not compare equal when compared by pointer.
That means the result of A == B
might be true
or false
, which you shouldn't depend on.
answered 54 mins ago
songyuanyao
86k9163225
86k9163225
add a comment |Â
add a comment |Â
up vote
2
down vote
Whether or not a compiler chooses to use the same string location for A
and B
is up to the implementation. Formally you can say that the behaviour of your code is implementation-defined.
Both choices implement the C++ standard correctly.
add a comment |Â
up vote
2
down vote
Whether or not a compiler chooses to use the same string location for A
and B
is up to the implementation. Formally you can say that the behaviour of your code is implementation-defined.
Both choices implement the C++ standard correctly.
add a comment |Â
up vote
2
down vote
up vote
2
down vote
Whether or not a compiler chooses to use the same string location for A
and B
is up to the implementation. Formally you can say that the behaviour of your code is implementation-defined.
Both choices implement the C++ standard correctly.
Whether or not a compiler chooses to use the same string location for A
and B
is up to the implementation. Formally you can say that the behaviour of your code is implementation-defined.
Both choices implement the C++ standard correctly.
answered 54 mins ago


Bathsheba
170k26239360
170k26239360
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52814457%2fwhy-c-merging-string-literals-on-compilation%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
1
Compilers are allowed to save space for literal string constants, by keeping only one single copy of a specific literal.
– Some programmer dude
54 mins ago