Is storage for the same content string literals guaranteed to be the same?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
6
down vote

favorite
2












Is the code below safe? It might be tempting to write code akin to this:



#include <map>

const std::map<const char*, int> m =
"text1", 1,
"text2", 2
;

int main ()
volatile const auto a = m.at("text1");
return 0;



The map is intended to be used with string literals only.



I think it's perfectly legal and seems to be working, however I never saw a guarantee that the pointer for the literal used in two different places to be the same. I couldn't manage to make compiler generate two separate pointers for literals with the same content, so I started to wonder how firm the assumption is.



I am only interested whether the literals with same content can have different pointers. Or more formally, can the code above except?



I know that there's a way to write code to be sure it works, and I think above approach is dangerous because compiler could decide to assign two different storages for the literal, especially if they are placed in different translation units. Am I right?










share|improve this question



















  • 1




    "however I never saw a guarantee that the pointer for the literal used in two different places to be the same" - There's a very good reason for that
    – StoryTeller
    1 hour ago






  • 1




    Why not use std::string?
    – tkausl
    1 hour ago










  • @tkausl Because that's beyond the scope of question as explicitly mentioned. I know how to write it properly. Also, because std::string ctor is not constexpr.
    – luk32
    1 hour ago










  • @StoryTeller That is teasing. Please share!
    – luk32
    1 hour ago










  • ͏+͏1͏͏͏͏͏͏͏͏͏͏͏͏͏ for the ingenious way of posing the question.
    – Bathsheba
    1 hour ago














up vote
6
down vote

favorite
2












Is the code below safe? It might be tempting to write code akin to this:



#include <map>

const std::map<const char*, int> m =
"text1", 1,
"text2", 2
;

int main ()
volatile const auto a = m.at("text1");
return 0;



The map is intended to be used with string literals only.



I think it's perfectly legal and seems to be working, however I never saw a guarantee that the pointer for the literal used in two different places to be the same. I couldn't manage to make compiler generate two separate pointers for literals with the same content, so I started to wonder how firm the assumption is.



I am only interested whether the literals with same content can have different pointers. Or more formally, can the code above except?



I know that there's a way to write code to be sure it works, and I think above approach is dangerous because compiler could decide to assign two different storages for the literal, especially if they are placed in different translation units. Am I right?










share|improve this question



















  • 1




    "however I never saw a guarantee that the pointer for the literal used in two different places to be the same" - There's a very good reason for that
    – StoryTeller
    1 hour ago






  • 1




    Why not use std::string?
    – tkausl
    1 hour ago










  • @tkausl Because that's beyond the scope of question as explicitly mentioned. I know how to write it properly. Also, because std::string ctor is not constexpr.
    – luk32
    1 hour ago










  • @StoryTeller That is teasing. Please share!
    – luk32
    1 hour ago










  • ͏+͏1͏͏͏͏͏͏͏͏͏͏͏͏͏ for the ingenious way of posing the question.
    – Bathsheba
    1 hour ago












up vote
6
down vote

favorite
2









up vote
6
down vote

favorite
2






2





Is the code below safe? It might be tempting to write code akin to this:



#include <map>

const std::map<const char*, int> m =
"text1", 1,
"text2", 2
;

int main ()
volatile const auto a = m.at("text1");
return 0;



The map is intended to be used with string literals only.



I think it's perfectly legal and seems to be working, however I never saw a guarantee that the pointer for the literal used in two different places to be the same. I couldn't manage to make compiler generate two separate pointers for literals with the same content, so I started to wonder how firm the assumption is.



I am only interested whether the literals with same content can have different pointers. Or more formally, can the code above except?



I know that there's a way to write code to be sure it works, and I think above approach is dangerous because compiler could decide to assign two different storages for the literal, especially if they are placed in different translation units. Am I right?










share|improve this question















Is the code below safe? It might be tempting to write code akin to this:



#include <map>

const std::map<const char*, int> m =
"text1", 1,
"text2", 2
;

int main ()
volatile const auto a = m.at("text1");
return 0;



The map is intended to be used with string literals only.



I think it's perfectly legal and seems to be working, however I never saw a guarantee that the pointer for the literal used in two different places to be the same. I couldn't manage to make compiler generate two separate pointers for literals with the same content, so I started to wonder how firm the assumption is.



I am only interested whether the literals with same content can have different pointers. Or more formally, can the code above except?



I know that there's a way to write code to be sure it works, and I think above approach is dangerous because compiler could decide to assign two different storages for the literal, especially if they are placed in different translation units. Am I right?







c++ storage language-lawyer string-literals






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 1 hour ago

























asked 1 hour ago









luk32

11.8k2241




11.8k2241







  • 1




    "however I never saw a guarantee that the pointer for the literal used in two different places to be the same" - There's a very good reason for that
    – StoryTeller
    1 hour ago






  • 1




    Why not use std::string?
    – tkausl
    1 hour ago










  • @tkausl Because that's beyond the scope of question as explicitly mentioned. I know how to write it properly. Also, because std::string ctor is not constexpr.
    – luk32
    1 hour ago










  • @StoryTeller That is teasing. Please share!
    – luk32
    1 hour ago










  • ͏+͏1͏͏͏͏͏͏͏͏͏͏͏͏͏ for the ingenious way of posing the question.
    – Bathsheba
    1 hour ago












  • 1




    "however I never saw a guarantee that the pointer for the literal used in two different places to be the same" - There's a very good reason for that
    – StoryTeller
    1 hour ago






  • 1




    Why not use std::string?
    – tkausl
    1 hour ago










  • @tkausl Because that's beyond the scope of question as explicitly mentioned. I know how to write it properly. Also, because std::string ctor is not constexpr.
    – luk32
    1 hour ago










  • @StoryTeller That is teasing. Please share!
    – luk32
    1 hour ago










  • ͏+͏1͏͏͏͏͏͏͏͏͏͏͏͏͏ for the ingenious way of posing the question.
    – Bathsheba
    1 hour ago







1




1




"however I never saw a guarantee that the pointer for the literal used in two different places to be the same" - There's a very good reason for that
– StoryTeller
1 hour ago




"however I never saw a guarantee that the pointer for the literal used in two different places to be the same" - There's a very good reason for that
– StoryTeller
1 hour ago




1




1




Why not use std::string?
– tkausl
1 hour ago




Why not use std::string?
– tkausl
1 hour ago












@tkausl Because that's beyond the scope of question as explicitly mentioned. I know how to write it properly. Also, because std::string ctor is not constexpr.
– luk32
1 hour ago




@tkausl Because that's beyond the scope of question as explicitly mentioned. I know how to write it properly. Also, because std::string ctor is not constexpr.
– luk32
1 hour ago












@StoryTeller That is teasing. Please share!
– luk32
1 hour ago




@StoryTeller That is teasing. Please share!
– luk32
1 hour ago












͏+͏1͏͏͏͏͏͏͏͏͏͏͏͏͏ for the ingenious way of posing the question.
– Bathsheba
1 hour ago




͏+͏1͏͏͏͏͏͏͏͏͏͏͏͏͏ for the ingenious way of posing the question.
– Bathsheba
1 hour ago












4 Answers
4






active

oldest

votes

















up vote
7
down vote













Whether or not two string literals with the exact same content are the exact same object, is unspecified, and in my opinion best not relied upon. To quote the standard:




[lex.string]



16 Evaluating a string-literal results in a string literal object
with static storage duration, initialized from the given characters as
specified above. Whether all string literals are distinct (that is,
are stored in nonoverlapping objects) and whether successive
evaluations of a string-literal yield the same or a different object
is unspecified.




If you wish to avoid the overhead of std::string, you can write a simple view type (or use std::string_view in C++17) that is a reference type over a string literal. Use it to do intelligent comparisons instead of relying upon literal identity.






share|improve this answer


















  • 1




    ͏+͏1͏͏͏͏͏͏͏͏͏͏͏͏͏
    – Bathsheba
    1 hour ago










  • In production I did use a custom wrapper type around const char* and overloaded relevant operators. I wondered if I wrote superfluous code. In my particular case the string overhead is not much of a problem, it's about constexpr ctor, also I am still restricted to c++14.
    – luk32
    18 mins ago











  • @luk32 - You can write a wrapper geared at string literals explicitly (taking a char* necessitates a length check). Consider this for example instead. It essentially gets rid of all the overhead, except for the comparison itself.
    – StoryTeller
    3 mins ago


















up vote
3
down vote













The Standard does not guarantee the addresses of string literals with the same content will be the same. In fact, [lex.string]/16 says:




Whether all string literals are distinct (that is, are stored in nonoverlapping objects) and whether successive evaluations of a string-literal yield the same or a different object is unspecified.




The second part even says you might not get the same address when a function containing a string literal is called a second time! Though I've never seen a compiler do that.



So using the same character array object when a string literal is repeated is an optional compiler optimization. With my installation of g++ and default compiler flags, I also find I get the same address for two identical string literals in the same translation unit. But as you guessed, I get different ones if the same string literal content appears in different translation units.



(A related interesting point: it's also permitted for different string literals to use overlapping arrays. That is, given



const char abcdef = "abcdef";
const char def = "def";
const char def0gh = "defgh";


it's possible you might find abcdef+3, def+0, and def0gh+0 are all the same pointer.)






share|improve this answer
















  • 1




    I was just thinking how to cook up some pathological literal. A simple will do, +1.
    – StoryTeller
    1 hour ago

















up vote
3
down vote













No, the C++ standard makes no such guarantees.



That said, if the code is in the same translation unit then it would be difficult to find a counter example. If main() is in a different translation then a counter example might be easier to produce.



If the map is in a different dynamic linked library or shared object then it's almost certainly not the case.



The volatile qualifier is a red herring.






share|improve this answer






















  • I used volatile for copy-paste friendliness with online compilers, to prevent complete removal of code. I think it does work this way and doesn't get in the way.
    – luk32
    22 mins ago

















up vote
1
down vote













The C++ standard does not require an implementation to de-duplicate string literals.



When a string literal resides in another translation unit or another shared library that would require the linker (ld) or runtime-linker (ld.so) to do the string literal de-duplication. Which they don't.






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%2f52423837%2fis-storage-for-the-same-content-string-literals-guaranteed-to-be-the-same%23new-answer', 'question_page');

    );

    Post as a guest






























    4 Answers
    4






    active

    oldest

    votes








    4 Answers
    4






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    7
    down vote













    Whether or not two string literals with the exact same content are the exact same object, is unspecified, and in my opinion best not relied upon. To quote the standard:




    [lex.string]



    16 Evaluating a string-literal results in a string literal object
    with static storage duration, initialized from the given characters as
    specified above. Whether all string literals are distinct (that is,
    are stored in nonoverlapping objects) and whether successive
    evaluations of a string-literal yield the same or a different object
    is unspecified.




    If you wish to avoid the overhead of std::string, you can write a simple view type (or use std::string_view in C++17) that is a reference type over a string literal. Use it to do intelligent comparisons instead of relying upon literal identity.






    share|improve this answer


















    • 1




      ͏+͏1͏͏͏͏͏͏͏͏͏͏͏͏͏
      – Bathsheba
      1 hour ago










    • In production I did use a custom wrapper type around const char* and overloaded relevant operators. I wondered if I wrote superfluous code. In my particular case the string overhead is not much of a problem, it's about constexpr ctor, also I am still restricted to c++14.
      – luk32
      18 mins ago











    • @luk32 - You can write a wrapper geared at string literals explicitly (taking a char* necessitates a length check). Consider this for example instead. It essentially gets rid of all the overhead, except for the comparison itself.
      – StoryTeller
      3 mins ago















    up vote
    7
    down vote













    Whether or not two string literals with the exact same content are the exact same object, is unspecified, and in my opinion best not relied upon. To quote the standard:




    [lex.string]



    16 Evaluating a string-literal results in a string literal object
    with static storage duration, initialized from the given characters as
    specified above. Whether all string literals are distinct (that is,
    are stored in nonoverlapping objects) and whether successive
    evaluations of a string-literal yield the same or a different object
    is unspecified.




    If you wish to avoid the overhead of std::string, you can write a simple view type (or use std::string_view in C++17) that is a reference type over a string literal. Use it to do intelligent comparisons instead of relying upon literal identity.






    share|improve this answer


















    • 1




      ͏+͏1͏͏͏͏͏͏͏͏͏͏͏͏͏
      – Bathsheba
      1 hour ago










    • In production I did use a custom wrapper type around const char* and overloaded relevant operators. I wondered if I wrote superfluous code. In my particular case the string overhead is not much of a problem, it's about constexpr ctor, also I am still restricted to c++14.
      – luk32
      18 mins ago











    • @luk32 - You can write a wrapper geared at string literals explicitly (taking a char* necessitates a length check). Consider this for example instead. It essentially gets rid of all the overhead, except for the comparison itself.
      – StoryTeller
      3 mins ago













    up vote
    7
    down vote










    up vote
    7
    down vote









    Whether or not two string literals with the exact same content are the exact same object, is unspecified, and in my opinion best not relied upon. To quote the standard:




    [lex.string]



    16 Evaluating a string-literal results in a string literal object
    with static storage duration, initialized from the given characters as
    specified above. Whether all string literals are distinct (that is,
    are stored in nonoverlapping objects) and whether successive
    evaluations of a string-literal yield the same or a different object
    is unspecified.




    If you wish to avoid the overhead of std::string, you can write a simple view type (or use std::string_view in C++17) that is a reference type over a string literal. Use it to do intelligent comparisons instead of relying upon literal identity.






    share|improve this answer














    Whether or not two string literals with the exact same content are the exact same object, is unspecified, and in my opinion best not relied upon. To quote the standard:




    [lex.string]



    16 Evaluating a string-literal results in a string literal object
    with static storage duration, initialized from the given characters as
    specified above. Whether all string literals are distinct (that is,
    are stored in nonoverlapping objects) and whether successive
    evaluations of a string-literal yield the same or a different object
    is unspecified.




    If you wish to avoid the overhead of std::string, you can write a simple view type (or use std::string_view in C++17) that is a reference type over a string literal. Use it to do intelligent comparisons instead of relying upon literal identity.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited 1 hour ago

























    answered 1 hour ago









    StoryTeller

    83.4k12167232




    83.4k12167232







    • 1




      ͏+͏1͏͏͏͏͏͏͏͏͏͏͏͏͏
      – Bathsheba
      1 hour ago










    • In production I did use a custom wrapper type around const char* and overloaded relevant operators. I wondered if I wrote superfluous code. In my particular case the string overhead is not much of a problem, it's about constexpr ctor, also I am still restricted to c++14.
      – luk32
      18 mins ago











    • @luk32 - You can write a wrapper geared at string literals explicitly (taking a char* necessitates a length check). Consider this for example instead. It essentially gets rid of all the overhead, except for the comparison itself.
      – StoryTeller
      3 mins ago













    • 1




      ͏+͏1͏͏͏͏͏͏͏͏͏͏͏͏͏
      – Bathsheba
      1 hour ago










    • In production I did use a custom wrapper type around const char* and overloaded relevant operators. I wondered if I wrote superfluous code. In my particular case the string overhead is not much of a problem, it's about constexpr ctor, also I am still restricted to c++14.
      – luk32
      18 mins ago











    • @luk32 - You can write a wrapper geared at string literals explicitly (taking a char* necessitates a length check). Consider this for example instead. It essentially gets rid of all the overhead, except for the comparison itself.
      – StoryTeller
      3 mins ago








    1




    1




    ͏+͏1͏͏͏͏͏͏͏͏͏͏͏͏͏
    – Bathsheba
    1 hour ago




    ͏+͏1͏͏͏͏͏͏͏͏͏͏͏͏͏
    – Bathsheba
    1 hour ago












    In production I did use a custom wrapper type around const char* and overloaded relevant operators. I wondered if I wrote superfluous code. In my particular case the string overhead is not much of a problem, it's about constexpr ctor, also I am still restricted to c++14.
    – luk32
    18 mins ago





    In production I did use a custom wrapper type around const char* and overloaded relevant operators. I wondered if I wrote superfluous code. In my particular case the string overhead is not much of a problem, it's about constexpr ctor, also I am still restricted to c++14.
    – luk32
    18 mins ago













    @luk32 - You can write a wrapper geared at string literals explicitly (taking a char* necessitates a length check). Consider this for example instead. It essentially gets rid of all the overhead, except for the comparison itself.
    – StoryTeller
    3 mins ago





    @luk32 - You can write a wrapper geared at string literals explicitly (taking a char* necessitates a length check). Consider this for example instead. It essentially gets rid of all the overhead, except for the comparison itself.
    – StoryTeller
    3 mins ago













    up vote
    3
    down vote













    The Standard does not guarantee the addresses of string literals with the same content will be the same. In fact, [lex.string]/16 says:




    Whether all string literals are distinct (that is, are stored in nonoverlapping objects) and whether successive evaluations of a string-literal yield the same or a different object is unspecified.




    The second part even says you might not get the same address when a function containing a string literal is called a second time! Though I've never seen a compiler do that.



    So using the same character array object when a string literal is repeated is an optional compiler optimization. With my installation of g++ and default compiler flags, I also find I get the same address for two identical string literals in the same translation unit. But as you guessed, I get different ones if the same string literal content appears in different translation units.



    (A related interesting point: it's also permitted for different string literals to use overlapping arrays. That is, given



    const char abcdef = "abcdef";
    const char def = "def";
    const char def0gh = "defgh";


    it's possible you might find abcdef+3, def+0, and def0gh+0 are all the same pointer.)






    share|improve this answer
















    • 1




      I was just thinking how to cook up some pathological literal. A simple will do, +1.
      – StoryTeller
      1 hour ago














    up vote
    3
    down vote













    The Standard does not guarantee the addresses of string literals with the same content will be the same. In fact, [lex.string]/16 says:




    Whether all string literals are distinct (that is, are stored in nonoverlapping objects) and whether successive evaluations of a string-literal yield the same or a different object is unspecified.




    The second part even says you might not get the same address when a function containing a string literal is called a second time! Though I've never seen a compiler do that.



    So using the same character array object when a string literal is repeated is an optional compiler optimization. With my installation of g++ and default compiler flags, I also find I get the same address for two identical string literals in the same translation unit. But as you guessed, I get different ones if the same string literal content appears in different translation units.



    (A related interesting point: it's also permitted for different string literals to use overlapping arrays. That is, given



    const char abcdef = "abcdef";
    const char def = "def";
    const char def0gh = "defgh";


    it's possible you might find abcdef+3, def+0, and def0gh+0 are all the same pointer.)






    share|improve this answer
















    • 1




      I was just thinking how to cook up some pathological literal. A simple will do, +1.
      – StoryTeller
      1 hour ago












    up vote
    3
    down vote










    up vote
    3
    down vote









    The Standard does not guarantee the addresses of string literals with the same content will be the same. In fact, [lex.string]/16 says:




    Whether all string literals are distinct (that is, are stored in nonoverlapping objects) and whether successive evaluations of a string-literal yield the same or a different object is unspecified.




    The second part even says you might not get the same address when a function containing a string literal is called a second time! Though I've never seen a compiler do that.



    So using the same character array object when a string literal is repeated is an optional compiler optimization. With my installation of g++ and default compiler flags, I also find I get the same address for two identical string literals in the same translation unit. But as you guessed, I get different ones if the same string literal content appears in different translation units.



    (A related interesting point: it's also permitted for different string literals to use overlapping arrays. That is, given



    const char abcdef = "abcdef";
    const char def = "def";
    const char def0gh = "defgh";


    it's possible you might find abcdef+3, def+0, and def0gh+0 are all the same pointer.)






    share|improve this answer












    The Standard does not guarantee the addresses of string literals with the same content will be the same. In fact, [lex.string]/16 says:




    Whether all string literals are distinct (that is, are stored in nonoverlapping objects) and whether successive evaluations of a string-literal yield the same or a different object is unspecified.




    The second part even says you might not get the same address when a function containing a string literal is called a second time! Though I've never seen a compiler do that.



    So using the same character array object when a string literal is repeated is an optional compiler optimization. With my installation of g++ and default compiler flags, I also find I get the same address for two identical string literals in the same translation unit. But as you guessed, I get different ones if the same string literal content appears in different translation units.



    (A related interesting point: it's also permitted for different string literals to use overlapping arrays. That is, given



    const char abcdef = "abcdef";
    const char def = "def";
    const char def0gh = "defgh";


    it's possible you might find abcdef+3, def+0, and def0gh+0 are all the same pointer.)







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered 1 hour ago









    aschepler

    49.8k569122




    49.8k569122







    • 1




      I was just thinking how to cook up some pathological literal. A simple will do, +1.
      – StoryTeller
      1 hour ago












    • 1




      I was just thinking how to cook up some pathological literal. A simple will do, +1.
      – StoryTeller
      1 hour ago







    1




    1




    I was just thinking how to cook up some pathological literal. A simple will do, +1.
    – StoryTeller
    1 hour ago




    I was just thinking how to cook up some pathological literal. A simple will do, +1.
    – StoryTeller
    1 hour ago










    up vote
    3
    down vote













    No, the C++ standard makes no such guarantees.



    That said, if the code is in the same translation unit then it would be difficult to find a counter example. If main() is in a different translation then a counter example might be easier to produce.



    If the map is in a different dynamic linked library or shared object then it's almost certainly not the case.



    The volatile qualifier is a red herring.






    share|improve this answer






















    • I used volatile for copy-paste friendliness with online compilers, to prevent complete removal of code. I think it does work this way and doesn't get in the way.
      – luk32
      22 mins ago














    up vote
    3
    down vote













    No, the C++ standard makes no such guarantees.



    That said, if the code is in the same translation unit then it would be difficult to find a counter example. If main() is in a different translation then a counter example might be easier to produce.



    If the map is in a different dynamic linked library or shared object then it's almost certainly not the case.



    The volatile qualifier is a red herring.






    share|improve this answer






















    • I used volatile for copy-paste friendliness with online compilers, to prevent complete removal of code. I think it does work this way and doesn't get in the way.
      – luk32
      22 mins ago












    up vote
    3
    down vote










    up vote
    3
    down vote









    No, the C++ standard makes no such guarantees.



    That said, if the code is in the same translation unit then it would be difficult to find a counter example. If main() is in a different translation then a counter example might be easier to produce.



    If the map is in a different dynamic linked library or shared object then it's almost certainly not the case.



    The volatile qualifier is a red herring.






    share|improve this answer














    No, the C++ standard makes no such guarantees.



    That said, if the code is in the same translation unit then it would be difficult to find a counter example. If main() is in a different translation then a counter example might be easier to produce.



    If the map is in a different dynamic linked library or shared object then it's almost certainly not the case.



    The volatile qualifier is a red herring.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited 1 hour ago

























    answered 1 hour ago









    Bathsheba

    168k26238353




    168k26238353











    • I used volatile for copy-paste friendliness with online compilers, to prevent complete removal of code. I think it does work this way and doesn't get in the way.
      – luk32
      22 mins ago
















    • I used volatile for copy-paste friendliness with online compilers, to prevent complete removal of code. I think it does work this way and doesn't get in the way.
      – luk32
      22 mins ago















    I used volatile for copy-paste friendliness with online compilers, to prevent complete removal of code. I think it does work this way and doesn't get in the way.
    – luk32
    22 mins ago




    I used volatile for copy-paste friendliness with online compilers, to prevent complete removal of code. I think it does work this way and doesn't get in the way.
    – luk32
    22 mins ago










    up vote
    1
    down vote













    The C++ standard does not require an implementation to de-duplicate string literals.



    When a string literal resides in another translation unit or another shared library that would require the linker (ld) or runtime-linker (ld.so) to do the string literal de-duplication. Which they don't.






    share|improve this answer
























      up vote
      1
      down vote













      The C++ standard does not require an implementation to de-duplicate string literals.



      When a string literal resides in another translation unit or another shared library that would require the linker (ld) or runtime-linker (ld.so) to do the string literal de-duplication. Which they don't.






      share|improve this answer






















        up vote
        1
        down vote










        up vote
        1
        down vote









        The C++ standard does not require an implementation to de-duplicate string literals.



        When a string literal resides in another translation unit or another shared library that would require the linker (ld) or runtime-linker (ld.so) to do the string literal de-duplication. Which they don't.






        share|improve this answer












        The C++ standard does not require an implementation to de-duplicate string literals.



        When a string literal resides in another translation unit or another shared library that would require the linker (ld) or runtime-linker (ld.so) to do the string literal de-duplication. Which they don't.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered 1 hour ago









        Maxim Egorushkin

        80.1k1195174




        80.1k1195174



























             

            draft saved


            draft discarded















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52423837%2fis-storage-for-the-same-content-string-literals-guaranteed-to-be-the-same%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