What happens if I write less than 12 bytes to a 12 byte buffer?

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











up vote
6
down vote

favorite












Understandably, going over a buffer errors out (or creates an overflow), but what happens if there are less than 12 bytes used in a 12 byte buffer? Is it possible or does the empty trailing always fill with 0s? Orthogonal question that may help: what is contained in a buffer when it is instantiated but not used by the application yet?



I have looked at a few pet programs in Visual Studio and it seems that they are appended with 0s (or null characters) but I am not sure if this is a MS implementation that may vary across language/ compiler.










share|improve this question





















  • memset can be used to ensure the buffer is initialized with zeros.
    – TruBlu
    4 hours ago















up vote
6
down vote

favorite












Understandably, going over a buffer errors out (or creates an overflow), but what happens if there are less than 12 bytes used in a 12 byte buffer? Is it possible or does the empty trailing always fill with 0s? Orthogonal question that may help: what is contained in a buffer when it is instantiated but not used by the application yet?



I have looked at a few pet programs in Visual Studio and it seems that they are appended with 0s (or null characters) but I am not sure if this is a MS implementation that may vary across language/ compiler.










share|improve this question





















  • memset can be used to ensure the buffer is initialized with zeros.
    – TruBlu
    4 hours ago













up vote
6
down vote

favorite









up vote
6
down vote

favorite











Understandably, going over a buffer errors out (or creates an overflow), but what happens if there are less than 12 bytes used in a 12 byte buffer? Is it possible or does the empty trailing always fill with 0s? Orthogonal question that may help: what is contained in a buffer when it is instantiated but not used by the application yet?



I have looked at a few pet programs in Visual Studio and it seems that they are appended with 0s (or null characters) but I am not sure if this is a MS implementation that may vary across language/ compiler.










share|improve this question













Understandably, going over a buffer errors out (or creates an overflow), but what happens if there are less than 12 bytes used in a 12 byte buffer? Is it possible or does the empty trailing always fill with 0s? Orthogonal question that may help: what is contained in a buffer when it is instantiated but not used by the application yet?



I have looked at a few pet programs in Visual Studio and it seems that they are appended with 0s (or null characters) but I am not sure if this is a MS implementation that may vary across language/ compiler.







c++ c memory-management buffer






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked 5 hours ago









hexadec0079

403




403











  • memset can be used to ensure the buffer is initialized with zeros.
    – TruBlu
    4 hours ago

















  • memset can be used to ensure the buffer is initialized with zeros.
    – TruBlu
    4 hours ago
















memset can be used to ensure the buffer is initialized with zeros.
– TruBlu
4 hours ago





memset can be used to ensure the buffer is initialized with zeros.
– TruBlu
4 hours ago













4 Answers
4






active

oldest

votes

















up vote
4
down vote













Take the following example (within a block of code, not global):



char data[12];
memcpy(data, "Selbie", 6);


Or even this example:



char* data = new char[12];
memcpy(data, "Selbie", 6);


In both of the above cases, the first 6 bytes of data are S,e,l,b,i, and e. The remaining 6 bytes of data are undefined but will be something.




Is it possible or does the empty trailing always fill with 0s?




Not guaranteed at all. The only allocator that I know of that guarantees zero byte fill is calloc. Example:



char* data = calloc(12,1); // will allocate an array of 12 bytes and zero-init each byte
memcpy(data, "Selbie");



what is contained in a buffer when it is instantiated but not used by the application yet?




You should assume that it could be filled with random bytes.



Debug builds with Visual Studio will often initialize buffers with null bytes or with 0xcc values, but it's less defined in release builds. There are however compiler flags and memory allocation techniques for Windows and Visual Studio where you can guaranteed zero-init memory allocations, but it's not portable.






share|improve this answer





























    up vote
    2
    down vote













    C++ has storage classes including global, automatic and static. The initialization depends on how the variable is declared.



    char global[12]; // all 0
    static char s_global[12]; // all 0

    void foo()

    static char s_local[12]; // all 0
    char local[12]; // automatic storage is filled with random rubbish



    Some interesting details here.






    share|improve this answer
















    • 1




      More good reading: en.cppreference.com/w/cpp/language/storage_duration
      – user4581301
      4 hours ago

















    up vote
    2
    down vote













    The program knows the length of a string because it ends it with a null-terminator, a character of value zero.



    This is why in order to fit a string in a buffer, the buffer has to be at least 1 character longer than the number of characters in the string, so that it can fit the string plus the null-terminator too.



    Any space after that in the buffer is left untouched. If there was data there previously, it is still there. This is what we call garbage.



    It is wrong to assume this space is zero-filled just because you haven't used it yet, you don't know what that particular memory space was used for before your program got to that point. Uninitialized memory should be handled as if what is in it is random and unreliable.






    share|improve this answer



























      up vote
      0
      down vote













      Writing part of a buffer will not affect the unwritten part of the buffer; it will contain whatever was there beforehand (which naturally depends entirely on how you got the buffer in the first place).



      As the other answer notes, static and global variables will be initialized to 0, but local variables will not be initialized (and instead contain whatever was on the stack beforehand). This is in keeping with the zero-overhead principle: initializing local variables would, in some cases, be an unnecessary and unwanted run-time cost, while static and global variables are allocated at load-time as part of a data segment.



      Initialization of heap storage is at the option of the memory manager, but in general it will not be initialized, either.






      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%2f52359906%2fwhat-happens-if-i-write-less-than-12-bytes-to-a-12-byte-buffer%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
        4
        down vote













        Take the following example (within a block of code, not global):



        char data[12];
        memcpy(data, "Selbie", 6);


        Or even this example:



        char* data = new char[12];
        memcpy(data, "Selbie", 6);


        In both of the above cases, the first 6 bytes of data are S,e,l,b,i, and e. The remaining 6 bytes of data are undefined but will be something.




        Is it possible or does the empty trailing always fill with 0s?




        Not guaranteed at all. The only allocator that I know of that guarantees zero byte fill is calloc. Example:



        char* data = calloc(12,1); // will allocate an array of 12 bytes and zero-init each byte
        memcpy(data, "Selbie");



        what is contained in a buffer when it is instantiated but not used by the application yet?




        You should assume that it could be filled with random bytes.



        Debug builds with Visual Studio will often initialize buffers with null bytes or with 0xcc values, but it's less defined in release builds. There are however compiler flags and memory allocation techniques for Windows and Visual Studio where you can guaranteed zero-init memory allocations, but it's not portable.






        share|improve this answer


























          up vote
          4
          down vote













          Take the following example (within a block of code, not global):



          char data[12];
          memcpy(data, "Selbie", 6);


          Or even this example:



          char* data = new char[12];
          memcpy(data, "Selbie", 6);


          In both of the above cases, the first 6 bytes of data are S,e,l,b,i, and e. The remaining 6 bytes of data are undefined but will be something.




          Is it possible or does the empty trailing always fill with 0s?




          Not guaranteed at all. The only allocator that I know of that guarantees zero byte fill is calloc. Example:



          char* data = calloc(12,1); // will allocate an array of 12 bytes and zero-init each byte
          memcpy(data, "Selbie");



          what is contained in a buffer when it is instantiated but not used by the application yet?




          You should assume that it could be filled with random bytes.



          Debug builds with Visual Studio will often initialize buffers with null bytes or with 0xcc values, but it's less defined in release builds. There are however compiler flags and memory allocation techniques for Windows and Visual Studio where you can guaranteed zero-init memory allocations, but it's not portable.






          share|improve this answer
























            up vote
            4
            down vote










            up vote
            4
            down vote









            Take the following example (within a block of code, not global):



            char data[12];
            memcpy(data, "Selbie", 6);


            Or even this example:



            char* data = new char[12];
            memcpy(data, "Selbie", 6);


            In both of the above cases, the first 6 bytes of data are S,e,l,b,i, and e. The remaining 6 bytes of data are undefined but will be something.




            Is it possible or does the empty trailing always fill with 0s?




            Not guaranteed at all. The only allocator that I know of that guarantees zero byte fill is calloc. Example:



            char* data = calloc(12,1); // will allocate an array of 12 bytes and zero-init each byte
            memcpy(data, "Selbie");



            what is contained in a buffer when it is instantiated but not used by the application yet?




            You should assume that it could be filled with random bytes.



            Debug builds with Visual Studio will often initialize buffers with null bytes or with 0xcc values, but it's less defined in release builds. There are however compiler flags and memory allocation techniques for Windows and Visual Studio where you can guaranteed zero-init memory allocations, but it's not portable.






            share|improve this answer














            Take the following example (within a block of code, not global):



            char data[12];
            memcpy(data, "Selbie", 6);


            Or even this example:



            char* data = new char[12];
            memcpy(data, "Selbie", 6);


            In both of the above cases, the first 6 bytes of data are S,e,l,b,i, and e. The remaining 6 bytes of data are undefined but will be something.




            Is it possible or does the empty trailing always fill with 0s?




            Not guaranteed at all. The only allocator that I know of that guarantees zero byte fill is calloc. Example:



            char* data = calloc(12,1); // will allocate an array of 12 bytes and zero-init each byte
            memcpy(data, "Selbie");



            what is contained in a buffer when it is instantiated but not used by the application yet?




            You should assume that it could be filled with random bytes.



            Debug builds with Visual Studio will often initialize buffers with null bytes or with 0xcc values, but it's less defined in release builds. There are however compiler flags and memory allocation techniques for Windows and Visual Studio where you can guaranteed zero-init memory allocations, but it's not portable.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited 3 hours ago









            kiran Biradar

            2,1251518




            2,1251518










            answered 4 hours ago









            selbie

            51.7k959117




            51.7k959117






















                up vote
                2
                down vote













                C++ has storage classes including global, automatic and static. The initialization depends on how the variable is declared.



                char global[12]; // all 0
                static char s_global[12]; // all 0

                void foo()

                static char s_local[12]; // all 0
                char local[12]; // automatic storage is filled with random rubbish



                Some interesting details here.






                share|improve this answer
















                • 1




                  More good reading: en.cppreference.com/w/cpp/language/storage_duration
                  – user4581301
                  4 hours ago














                up vote
                2
                down vote













                C++ has storage classes including global, automatic and static. The initialization depends on how the variable is declared.



                char global[12]; // all 0
                static char s_global[12]; // all 0

                void foo()

                static char s_local[12]; // all 0
                char local[12]; // automatic storage is filled with random rubbish



                Some interesting details here.






                share|improve this answer
















                • 1




                  More good reading: en.cppreference.com/w/cpp/language/storage_duration
                  – user4581301
                  4 hours ago












                up vote
                2
                down vote










                up vote
                2
                down vote









                C++ has storage classes including global, automatic and static. The initialization depends on how the variable is declared.



                char global[12]; // all 0
                static char s_global[12]; // all 0

                void foo()

                static char s_local[12]; // all 0
                char local[12]; // automatic storage is filled with random rubbish



                Some interesting details here.






                share|improve this answer












                C++ has storage classes including global, automatic and static. The initialization depends on how the variable is declared.



                char global[12]; // all 0
                static char s_global[12]; // all 0

                void foo()

                static char s_local[12]; // all 0
                char local[12]; // automatic storage is filled with random rubbish



                Some interesting details here.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered 4 hours ago









                Matthew Fisher

                1,6802719




                1,6802719







                • 1




                  More good reading: en.cppreference.com/w/cpp/language/storage_duration
                  – user4581301
                  4 hours ago












                • 1




                  More good reading: en.cppreference.com/w/cpp/language/storage_duration
                  – user4581301
                  4 hours ago







                1




                1




                More good reading: en.cppreference.com/w/cpp/language/storage_duration
                – user4581301
                4 hours ago




                More good reading: en.cppreference.com/w/cpp/language/storage_duration
                – user4581301
                4 hours ago










                up vote
                2
                down vote













                The program knows the length of a string because it ends it with a null-terminator, a character of value zero.



                This is why in order to fit a string in a buffer, the buffer has to be at least 1 character longer than the number of characters in the string, so that it can fit the string plus the null-terminator too.



                Any space after that in the buffer is left untouched. If there was data there previously, it is still there. This is what we call garbage.



                It is wrong to assume this space is zero-filled just because you haven't used it yet, you don't know what that particular memory space was used for before your program got to that point. Uninitialized memory should be handled as if what is in it is random and unreliable.






                share|improve this answer
























                  up vote
                  2
                  down vote













                  The program knows the length of a string because it ends it with a null-terminator, a character of value zero.



                  This is why in order to fit a string in a buffer, the buffer has to be at least 1 character longer than the number of characters in the string, so that it can fit the string plus the null-terminator too.



                  Any space after that in the buffer is left untouched. If there was data there previously, it is still there. This is what we call garbage.



                  It is wrong to assume this space is zero-filled just because you haven't used it yet, you don't know what that particular memory space was used for before your program got to that point. Uninitialized memory should be handled as if what is in it is random and unreliable.






                  share|improve this answer






















                    up vote
                    2
                    down vote










                    up vote
                    2
                    down vote









                    The program knows the length of a string because it ends it with a null-terminator, a character of value zero.



                    This is why in order to fit a string in a buffer, the buffer has to be at least 1 character longer than the number of characters in the string, so that it can fit the string plus the null-terminator too.



                    Any space after that in the buffer is left untouched. If there was data there previously, it is still there. This is what we call garbage.



                    It is wrong to assume this space is zero-filled just because you haven't used it yet, you don't know what that particular memory space was used for before your program got to that point. Uninitialized memory should be handled as if what is in it is random and unreliable.






                    share|improve this answer












                    The program knows the length of a string because it ends it with a null-terminator, a character of value zero.



                    This is why in order to fit a string in a buffer, the buffer has to be at least 1 character longer than the number of characters in the string, so that it can fit the string plus the null-terminator too.



                    Any space after that in the buffer is left untouched. If there was data there previously, it is still there. This is what we call garbage.



                    It is wrong to assume this space is zero-filled just because you haven't used it yet, you don't know what that particular memory space was used for before your program got to that point. Uninitialized memory should be handled as if what is in it is random and unreliable.







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered 4 hours ago









                    Havenard

                    16.8k22645




                    16.8k22645




















                        up vote
                        0
                        down vote













                        Writing part of a buffer will not affect the unwritten part of the buffer; it will contain whatever was there beforehand (which naturally depends entirely on how you got the buffer in the first place).



                        As the other answer notes, static and global variables will be initialized to 0, but local variables will not be initialized (and instead contain whatever was on the stack beforehand). This is in keeping with the zero-overhead principle: initializing local variables would, in some cases, be an unnecessary and unwanted run-time cost, while static and global variables are allocated at load-time as part of a data segment.



                        Initialization of heap storage is at the option of the memory manager, but in general it will not be initialized, either.






                        share|improve this answer
























                          up vote
                          0
                          down vote













                          Writing part of a buffer will not affect the unwritten part of the buffer; it will contain whatever was there beforehand (which naturally depends entirely on how you got the buffer in the first place).



                          As the other answer notes, static and global variables will be initialized to 0, but local variables will not be initialized (and instead contain whatever was on the stack beforehand). This is in keeping with the zero-overhead principle: initializing local variables would, in some cases, be an unnecessary and unwanted run-time cost, while static and global variables are allocated at load-time as part of a data segment.



                          Initialization of heap storage is at the option of the memory manager, but in general it will not be initialized, either.






                          share|improve this answer






















                            up vote
                            0
                            down vote










                            up vote
                            0
                            down vote









                            Writing part of a buffer will not affect the unwritten part of the buffer; it will contain whatever was there beforehand (which naturally depends entirely on how you got the buffer in the first place).



                            As the other answer notes, static and global variables will be initialized to 0, but local variables will not be initialized (and instead contain whatever was on the stack beforehand). This is in keeping with the zero-overhead principle: initializing local variables would, in some cases, be an unnecessary and unwanted run-time cost, while static and global variables are allocated at load-time as part of a data segment.



                            Initialization of heap storage is at the option of the memory manager, but in general it will not be initialized, either.






                            share|improve this answer












                            Writing part of a buffer will not affect the unwritten part of the buffer; it will contain whatever was there beforehand (which naturally depends entirely on how you got the buffer in the first place).



                            As the other answer notes, static and global variables will be initialized to 0, but local variables will not be initialized (and instead contain whatever was on the stack beforehand). This is in keeping with the zero-overhead principle: initializing local variables would, in some cases, be an unnecessary and unwanted run-time cost, while static and global variables are allocated at load-time as part of a data segment.



                            Initialization of heap storage is at the option of the memory manager, but in general it will not be initialized, either.







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered 4 hours ago









                            comingstorm

                            19.3k12755




                            19.3k12755



























                                 

                                draft saved


                                draft discarded















































                                 


                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function ()
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52359906%2fwhat-happens-if-i-write-less-than-12-bytes-to-a-12-byte-buffer%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

                                One-line joke