how much memory is allocated to an uninitialized std::string variable?









up vote
-1
down vote

favorite












if i declare a variable of type std::string but do not initialize it, how much memory is allocated? I know that if I initialize it to "hello" for example, there will be a byte reserved for each character plus one for the null character, 6 total. Is there a default length defined somewhere in the string class?
(I've tried looking for the actually definition in the string header file but don't know where to find it)










share|improve this question





















  • Do we count the memory needed for the class itself or only for the data it is storing?
    – Kamil Cuk
    Nov 8 at 21:27










  • The place to look for an implementation is its default constructor and where the data members are declared, but it's by no means guaranteed to be consistent across implementations or versions.
    – chris
    Nov 8 at 21:28






  • 1




    It's likely 0 bytes, but it's not defined (as far as I can tell). And don't confuse memory allocated for the data and memory allocated for the std::string (which is constant). Are you asking how much memory is used or how much is allocated? And some std::string implementations use a small string optimization, so it can use the std::string's memory to store short strings directly without allocating anything else. There are just so many ways this question can go it's hard to know what exactly you are asking about.
    – François Andrieux
    Nov 8 at 21:28











  • Are you asking about what happens on some particular platform, what typically happens, or what is guaranteed to happen?
    – David Schwartz
    Nov 8 at 21:29














up vote
-1
down vote

favorite












if i declare a variable of type std::string but do not initialize it, how much memory is allocated? I know that if I initialize it to "hello" for example, there will be a byte reserved for each character plus one for the null character, 6 total. Is there a default length defined somewhere in the string class?
(I've tried looking for the actually definition in the string header file but don't know where to find it)










share|improve this question





















  • Do we count the memory needed for the class itself or only for the data it is storing?
    – Kamil Cuk
    Nov 8 at 21:27










  • The place to look for an implementation is its default constructor and where the data members are declared, but it's by no means guaranteed to be consistent across implementations or versions.
    – chris
    Nov 8 at 21:28






  • 1




    It's likely 0 bytes, but it's not defined (as far as I can tell). And don't confuse memory allocated for the data and memory allocated for the std::string (which is constant). Are you asking how much memory is used or how much is allocated? And some std::string implementations use a small string optimization, so it can use the std::string's memory to store short strings directly without allocating anything else. There are just so many ways this question can go it's hard to know what exactly you are asking about.
    – François Andrieux
    Nov 8 at 21:28











  • Are you asking about what happens on some particular platform, what typically happens, or what is guaranteed to happen?
    – David Schwartz
    Nov 8 at 21:29












up vote
-1
down vote

favorite









up vote
-1
down vote

favorite











if i declare a variable of type std::string but do not initialize it, how much memory is allocated? I know that if I initialize it to "hello" for example, there will be a byte reserved for each character plus one for the null character, 6 total. Is there a default length defined somewhere in the string class?
(I've tried looking for the actually definition in the string header file but don't know where to find it)










share|improve this question













if i declare a variable of type std::string but do not initialize it, how much memory is allocated? I know that if I initialize it to "hello" for example, there will be a byte reserved for each character plus one for the null character, 6 total. Is there a default length defined somewhere in the string class?
(I've tried looking for the actually definition in the string header file but don't know where to find it)







c++ string memory






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 8 at 21:25









AndoKay

13




13











  • Do we count the memory needed for the class itself or only for the data it is storing?
    – Kamil Cuk
    Nov 8 at 21:27










  • The place to look for an implementation is its default constructor and where the data members are declared, but it's by no means guaranteed to be consistent across implementations or versions.
    – chris
    Nov 8 at 21:28






  • 1




    It's likely 0 bytes, but it's not defined (as far as I can tell). And don't confuse memory allocated for the data and memory allocated for the std::string (which is constant). Are you asking how much memory is used or how much is allocated? And some std::string implementations use a small string optimization, so it can use the std::string's memory to store short strings directly without allocating anything else. There are just so many ways this question can go it's hard to know what exactly you are asking about.
    – François Andrieux
    Nov 8 at 21:28











  • Are you asking about what happens on some particular platform, what typically happens, or what is guaranteed to happen?
    – David Schwartz
    Nov 8 at 21:29
















  • Do we count the memory needed for the class itself or only for the data it is storing?
    – Kamil Cuk
    Nov 8 at 21:27










  • The place to look for an implementation is its default constructor and where the data members are declared, but it's by no means guaranteed to be consistent across implementations or versions.
    – chris
    Nov 8 at 21:28






  • 1




    It's likely 0 bytes, but it's not defined (as far as I can tell). And don't confuse memory allocated for the data and memory allocated for the std::string (which is constant). Are you asking how much memory is used or how much is allocated? And some std::string implementations use a small string optimization, so it can use the std::string's memory to store short strings directly without allocating anything else. There are just so many ways this question can go it's hard to know what exactly you are asking about.
    – François Andrieux
    Nov 8 at 21:28











  • Are you asking about what happens on some particular platform, what typically happens, or what is guaranteed to happen?
    – David Schwartz
    Nov 8 at 21:29















Do we count the memory needed for the class itself or only for the data it is storing?
– Kamil Cuk
Nov 8 at 21:27




Do we count the memory needed for the class itself or only for the data it is storing?
– Kamil Cuk
Nov 8 at 21:27












The place to look for an implementation is its default constructor and where the data members are declared, but it's by no means guaranteed to be consistent across implementations or versions.
– chris
Nov 8 at 21:28




The place to look for an implementation is its default constructor and where the data members are declared, but it's by no means guaranteed to be consistent across implementations or versions.
– chris
Nov 8 at 21:28




1




1




It's likely 0 bytes, but it's not defined (as far as I can tell). And don't confuse memory allocated for the data and memory allocated for the std::string (which is constant). Are you asking how much memory is used or how much is allocated? And some std::string implementations use a small string optimization, so it can use the std::string's memory to store short strings directly without allocating anything else. There are just so many ways this question can go it's hard to know what exactly you are asking about.
– François Andrieux
Nov 8 at 21:28





It's likely 0 bytes, but it's not defined (as far as I can tell). And don't confuse memory allocated for the data and memory allocated for the std::string (which is constant). Are you asking how much memory is used or how much is allocated? And some std::string implementations use a small string optimization, so it can use the std::string's memory to store short strings directly without allocating anything else. There are just so many ways this question can go it's hard to know what exactly you are asking about.
– François Andrieux
Nov 8 at 21:28













Are you asking about what happens on some particular platform, what typically happens, or what is guaranteed to happen?
– David Schwartz
Nov 8 at 21:29




Are you asking about what happens on some particular platform, what typically happens, or what is guaranteed to happen?
– David Schwartz
Nov 8 at 21:29












3 Answers
3






active

oldest

votes

















up vote
3
down vote



accepted










It's unspecified. Different implementations may allocate different amounts of memory upon default-construction, and an implementation is not required to tell you how much memory that is. However, I believe that right now it's most common for std::string to employ a short string optimization, under which a default-constructed std::string need not allocate any memory at all besides the size of the std::string class itself. See Meaning of acronym SSO in the context of std::string for details. Note that sizeof(std::string) is also unspecified.






share|improve this answer



























    up vote
    3
    down vote













    While that is unspecified, there is something worth mentioning: In practice, implementations avoid allocating memory for uninitialized strings.



    I have performed a few tests for usual sizeof(std::string) using Compiler Explorer (link to the test). Here are the results, though you can of course experiment with more:



    • gcc / Linux / x86-64 / libstdc++ : 32 bytes

    • gcc / Linux / x86 / libstdc++ : 24 bytes

    • clang / Linux / x86-64 / libc++ (NB: not libstdc++!) : 24 bytes

    • clang / Linux / x86 / libc++ : 12 bytes

    • msvc / Windows / x86-64 / VC runtime : 32 bytes

    • msvc / Windows / x86 / VC runtime : 24 bytes

    • gcc / Linux / ARM64 / libstdc++ : 32 bytes

    • gcc / Linux / ARM (32-bit) / libstdc++ : 24 bytes

    • msvc / Windows / ARM64 / VC runtime : 32 bytes (NB: output is in hex on CE)

    Roughly speaking, in practice, a std::string object is going to be 12 to 32 bytes big, excluding the dynamically allocated memory.



    These results are depending mostly on the standard library implementation and CPU architecture (because of whatever standard library feels like doing).



    Note that these sizes do include SSO, which @Brian discussed in his answer. I believe this is the motive for libstdc++ and MS' implementation to use 32 bytes rather than 24 (since I suspect that there are usually 3 pointers involved: data begin, data end and capacity end), though I don't know the specifics.






    share|improve this answer



























      up vote
      0
      down vote













      You can always check youself. It depends on the computer you are at, to check datatype sizes you can always use size:of.



      In your case it would look like this



      #include <iostream>
      #include <string>

      int main()
      std::string a = "";

      std::cout << sizeof(a) << 'n';

      system("PAUSE");
      return 0;



      I get 28 bytes.






      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: true,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: 10,
        bindNavPrevention: true,
        postfix: "",
        imageUploader:
        brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
        contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
        allowUrls: true
        ,
        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%2f53216377%2fhow-much-memory-is-allocated-to-an-uninitialized-stdstring-variable%23new-answer', 'question_page');

        );

        Post as a guest















        Required, but never shown

























        3 Answers
        3






        active

        oldest

        votes








        3 Answers
        3






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes








        up vote
        3
        down vote



        accepted










        It's unspecified. Different implementations may allocate different amounts of memory upon default-construction, and an implementation is not required to tell you how much memory that is. However, I believe that right now it's most common for std::string to employ a short string optimization, under which a default-constructed std::string need not allocate any memory at all besides the size of the std::string class itself. See Meaning of acronym SSO in the context of std::string for details. Note that sizeof(std::string) is also unspecified.






        share|improve this answer
























          up vote
          3
          down vote



          accepted










          It's unspecified. Different implementations may allocate different amounts of memory upon default-construction, and an implementation is not required to tell you how much memory that is. However, I believe that right now it's most common for std::string to employ a short string optimization, under which a default-constructed std::string need not allocate any memory at all besides the size of the std::string class itself. See Meaning of acronym SSO in the context of std::string for details. Note that sizeof(std::string) is also unspecified.






          share|improve this answer






















            up vote
            3
            down vote



            accepted







            up vote
            3
            down vote



            accepted






            It's unspecified. Different implementations may allocate different amounts of memory upon default-construction, and an implementation is not required to tell you how much memory that is. However, I believe that right now it's most common for std::string to employ a short string optimization, under which a default-constructed std::string need not allocate any memory at all besides the size of the std::string class itself. See Meaning of acronym SSO in the context of std::string for details. Note that sizeof(std::string) is also unspecified.






            share|improve this answer












            It's unspecified. Different implementations may allocate different amounts of memory upon default-construction, and an implementation is not required to tell you how much memory that is. However, I believe that right now it's most common for std::string to employ a short string optimization, under which a default-constructed std::string need not allocate any memory at all besides the size of the std::string class itself. See Meaning of acronym SSO in the context of std::string for details. Note that sizeof(std::string) is also unspecified.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 8 at 21:29









            Brian

            63.1k793177




            63.1k793177






















                up vote
                3
                down vote













                While that is unspecified, there is something worth mentioning: In practice, implementations avoid allocating memory for uninitialized strings.



                I have performed a few tests for usual sizeof(std::string) using Compiler Explorer (link to the test). Here are the results, though you can of course experiment with more:



                • gcc / Linux / x86-64 / libstdc++ : 32 bytes

                • gcc / Linux / x86 / libstdc++ : 24 bytes

                • clang / Linux / x86-64 / libc++ (NB: not libstdc++!) : 24 bytes

                • clang / Linux / x86 / libc++ : 12 bytes

                • msvc / Windows / x86-64 / VC runtime : 32 bytes

                • msvc / Windows / x86 / VC runtime : 24 bytes

                • gcc / Linux / ARM64 / libstdc++ : 32 bytes

                • gcc / Linux / ARM (32-bit) / libstdc++ : 24 bytes

                • msvc / Windows / ARM64 / VC runtime : 32 bytes (NB: output is in hex on CE)

                Roughly speaking, in practice, a std::string object is going to be 12 to 32 bytes big, excluding the dynamically allocated memory.



                These results are depending mostly on the standard library implementation and CPU architecture (because of whatever standard library feels like doing).



                Note that these sizes do include SSO, which @Brian discussed in his answer. I believe this is the motive for libstdc++ and MS' implementation to use 32 bytes rather than 24 (since I suspect that there are usually 3 pointers involved: data begin, data end and capacity end), though I don't know the specifics.






                share|improve this answer
























                  up vote
                  3
                  down vote













                  While that is unspecified, there is something worth mentioning: In practice, implementations avoid allocating memory for uninitialized strings.



                  I have performed a few tests for usual sizeof(std::string) using Compiler Explorer (link to the test). Here are the results, though you can of course experiment with more:



                  • gcc / Linux / x86-64 / libstdc++ : 32 bytes

                  • gcc / Linux / x86 / libstdc++ : 24 bytes

                  • clang / Linux / x86-64 / libc++ (NB: not libstdc++!) : 24 bytes

                  • clang / Linux / x86 / libc++ : 12 bytes

                  • msvc / Windows / x86-64 / VC runtime : 32 bytes

                  • msvc / Windows / x86 / VC runtime : 24 bytes

                  • gcc / Linux / ARM64 / libstdc++ : 32 bytes

                  • gcc / Linux / ARM (32-bit) / libstdc++ : 24 bytes

                  • msvc / Windows / ARM64 / VC runtime : 32 bytes (NB: output is in hex on CE)

                  Roughly speaking, in practice, a std::string object is going to be 12 to 32 bytes big, excluding the dynamically allocated memory.



                  These results are depending mostly on the standard library implementation and CPU architecture (because of whatever standard library feels like doing).



                  Note that these sizes do include SSO, which @Brian discussed in his answer. I believe this is the motive for libstdc++ and MS' implementation to use 32 bytes rather than 24 (since I suspect that there are usually 3 pointers involved: data begin, data end and capacity end), though I don't know the specifics.






                  share|improve this answer






















                    up vote
                    3
                    down vote










                    up vote
                    3
                    down vote









                    While that is unspecified, there is something worth mentioning: In practice, implementations avoid allocating memory for uninitialized strings.



                    I have performed a few tests for usual sizeof(std::string) using Compiler Explorer (link to the test). Here are the results, though you can of course experiment with more:



                    • gcc / Linux / x86-64 / libstdc++ : 32 bytes

                    • gcc / Linux / x86 / libstdc++ : 24 bytes

                    • clang / Linux / x86-64 / libc++ (NB: not libstdc++!) : 24 bytes

                    • clang / Linux / x86 / libc++ : 12 bytes

                    • msvc / Windows / x86-64 / VC runtime : 32 bytes

                    • msvc / Windows / x86 / VC runtime : 24 bytes

                    • gcc / Linux / ARM64 / libstdc++ : 32 bytes

                    • gcc / Linux / ARM (32-bit) / libstdc++ : 24 bytes

                    • msvc / Windows / ARM64 / VC runtime : 32 bytes (NB: output is in hex on CE)

                    Roughly speaking, in practice, a std::string object is going to be 12 to 32 bytes big, excluding the dynamically allocated memory.



                    These results are depending mostly on the standard library implementation and CPU architecture (because of whatever standard library feels like doing).



                    Note that these sizes do include SSO, which @Brian discussed in his answer. I believe this is the motive for libstdc++ and MS' implementation to use 32 bytes rather than 24 (since I suspect that there are usually 3 pointers involved: data begin, data end and capacity end), though I don't know the specifics.






                    share|improve this answer












                    While that is unspecified, there is something worth mentioning: In practice, implementations avoid allocating memory for uninitialized strings.



                    I have performed a few tests for usual sizeof(std::string) using Compiler Explorer (link to the test). Here are the results, though you can of course experiment with more:



                    • gcc / Linux / x86-64 / libstdc++ : 32 bytes

                    • gcc / Linux / x86 / libstdc++ : 24 bytes

                    • clang / Linux / x86-64 / libc++ (NB: not libstdc++!) : 24 bytes

                    • clang / Linux / x86 / libc++ : 12 bytes

                    • msvc / Windows / x86-64 / VC runtime : 32 bytes

                    • msvc / Windows / x86 / VC runtime : 24 bytes

                    • gcc / Linux / ARM64 / libstdc++ : 32 bytes

                    • gcc / Linux / ARM (32-bit) / libstdc++ : 24 bytes

                    • msvc / Windows / ARM64 / VC runtime : 32 bytes (NB: output is in hex on CE)

                    Roughly speaking, in practice, a std::string object is going to be 12 to 32 bytes big, excluding the dynamically allocated memory.



                    These results are depending mostly on the standard library implementation and CPU architecture (because of whatever standard library feels like doing).



                    Note that these sizes do include SSO, which @Brian discussed in his answer. I believe this is the motive for libstdc++ and MS' implementation to use 32 bytes rather than 24 (since I suspect that there are usually 3 pointers involved: data begin, data end and capacity end), though I don't know the specifics.







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 8 at 21:52









                    Asu

                    1,3511021




                    1,3511021




















                        up vote
                        0
                        down vote













                        You can always check youself. It depends on the computer you are at, to check datatype sizes you can always use size:of.



                        In your case it would look like this



                        #include <iostream>
                        #include <string>

                        int main()
                        std::string a = "";

                        std::cout << sizeof(a) << 'n';

                        system("PAUSE");
                        return 0;



                        I get 28 bytes.






                        share|improve this answer
























                          up vote
                          0
                          down vote













                          You can always check youself. It depends on the computer you are at, to check datatype sizes you can always use size:of.



                          In your case it would look like this



                          #include <iostream>
                          #include <string>

                          int main()
                          std::string a = "";

                          std::cout << sizeof(a) << 'n';

                          system("PAUSE");
                          return 0;



                          I get 28 bytes.






                          share|improve this answer






















                            up vote
                            0
                            down vote










                            up vote
                            0
                            down vote









                            You can always check youself. It depends on the computer you are at, to check datatype sizes you can always use size:of.



                            In your case it would look like this



                            #include <iostream>
                            #include <string>

                            int main()
                            std::string a = "";

                            std::cout << sizeof(a) << 'n';

                            system("PAUSE");
                            return 0;



                            I get 28 bytes.






                            share|improve this answer












                            You can always check youself. It depends on the computer you are at, to check datatype sizes you can always use size:of.



                            In your case it would look like this



                            #include <iostream>
                            #include <string>

                            int main()
                            std::string a = "";

                            std::cout << sizeof(a) << 'n';

                            system("PAUSE");
                            return 0;



                            I get 28 bytes.







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Nov 9 at 5:28









                            Allamo Olsson

                            849




                            849



























                                 

                                draft saved


                                draft discarded















































                                 


                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function ()
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53216377%2fhow-much-memory-is-allocated-to-an-uninitialized-stdstring-variable%23new-answer', 'question_page');

                                );

                                Post as a guest















                                Required, but never shown





















































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown

































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown