Why C++ merging string literals on compilation

The name of the pictureThe name of the pictureThe name of the pictureClash 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?










share|improve this question

















  • 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














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?










share|improve this question

















  • 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












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?










share|improve this question













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++






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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












  • 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












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.






share|improve this answer



























    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.






    share|improve this answer




















      Your Answer





      StackExchange.ifUsing("editor", function ()
      StackExchange.using("externalEditor", function ()
      StackExchange.using("snippets", function ()
      StackExchange.snippets.init();
      );
      );
      , "code-snippets");

      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "1"
      ;
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function()
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled)
      StackExchange.using("snippets", function()
      createEditor();
      );

      else
      createEditor();

      );

      function createEditor()
      StackExchange.prepareEditor(
      heartbeatType: 'answer',
      convertImagesToLinks: true,
      noModals: false,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );













       

      draft saved


      draft discarded


















      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






























      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.






      share|improve this answer
























        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.






        share|improve this answer






















          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.






          share|improve this answer












          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.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered 54 mins ago









          songyuanyao

          86k9163225




          86k9163225






















              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.






              share|improve this answer
























                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.






                share|improve this answer






















                  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.






                  share|improve this answer












                  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.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered 54 mins ago









                  Bathsheba

                  170k26239360




                  170k26239360



























                       

                      draft saved


                      draft discarded















































                       


                      draft saved


                      draft discarded














                      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













































































                      Comments

                      Popular posts from this blog

                      What does second last employer means? [closed]

                      List of Gilmore Girls characters

                      Confectionery