What is the memory layout of vector of arrays?












20















can anybody explaine the memory layout of



std::vector<std::array<int, 5>> vec(2)


does it provide contiguous memory block of a 2D array
with 2 rows of 5 elements?



To my understanding, the vector of vectors



std::vector<std::vector<int>> vec(2, std::vector<int>(5))


provide the memory layout of two contiguous arrays of length 5 elements in different locations in memory.



Will it be the same for the vector of arrays?










share|improve this question





























    20















    can anybody explaine the memory layout of



    std::vector<std::array<int, 5>> vec(2)


    does it provide contiguous memory block of a 2D array
    with 2 rows of 5 elements?



    To my understanding, the vector of vectors



    std::vector<std::vector<int>> vec(2, std::vector<int>(5))


    provide the memory layout of two contiguous arrays of length 5 elements in different locations in memory.



    Will it be the same for the vector of arrays?










    share|improve this question



























      20












      20








      20








      can anybody explaine the memory layout of



      std::vector<std::array<int, 5>> vec(2)


      does it provide contiguous memory block of a 2D array
      with 2 rows of 5 elements?



      To my understanding, the vector of vectors



      std::vector<std::vector<int>> vec(2, std::vector<int>(5))


      provide the memory layout of two contiguous arrays of length 5 elements in different locations in memory.



      Will it be the same for the vector of arrays?










      share|improve this question
















      can anybody explaine the memory layout of



      std::vector<std::array<int, 5>> vec(2)


      does it provide contiguous memory block of a 2D array
      with 2 rows of 5 elements?



      To my understanding, the vector of vectors



      std::vector<std::vector<int>> vec(2, std::vector<int>(5))


      provide the memory layout of two contiguous arrays of length 5 elements in different locations in memory.



      Will it be the same for the vector of arrays?







      c++ c++11 language-lawyer stdvector stdarray






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 9 hours ago









      Bathsheba

      176k27251373




      176k27251373










      asked 10 hours ago









      ConstConst

      17210




      17210
























          3 Answers
          3






          active

          oldest

          votes


















          35














          Arrays do not have any indirection, but just store their data "directly". That is, a std::array<int, 5> literally contains five ints in a row, flat. And, like vectors, they do not put padding between their elements, so they're "internally contiguous".



          However, the std::array object itself may be larger than the set of its elements! It is permitted to have trailing "stuff" like padding. So, although likely, it is not necessarily true that your data will all be contiguous in the first case.



          An int
          +----+
          | |
          +----+

          A vector of 2 x int
          +----+----+----+-----+ +----+----+
          | housekeeping | ptr | | 1 | 2 |
          +----+----+----+-----+ +----+----+
          | ^
          -----------

          An std::array<int, 5>
          +----+----+----+----+----+----------->
          | 1 | 2 | 3 | 4 | 5 | possible cruft/padding....
          +----+----+----+----+----+----------->

          A vector of 2 x std::array<int, 5>
          +----+----+----+-----+ +----+----+----+----+----+----------------------------+----+----+----+----+----+----------->
          | housekeeping | ptr | | 1 | 2 | 3 | 4 | 5 | possible cruft/padding.... | 1 | 2 | 3 | 4 | 5 | possible cruft/padding....
          +----+----+----+-----+ +----+----+----+----+----+----------------------------+----+----+----+----+----+----------->
          | ^
          -----------


          And, even if it were, due to aliasing rules, whether you'd be able to use a single int* to navigate all 10 numbers would potentially be a different matter!



          All in all, a vector of ten ints would be clearer, completely packed, and possibly safer to use.



          In the case of a vector of vectors, a vector is really just a pointer plus some housekeeping, hence the indirection (as you say).






          share|improve this answer





















          • 8





            According to the answers here data don't have to be contiguous: Is the data in nested std::arrays guaranteed to be contiguous?. There are some discussion about this topic. Another discussions: Does std::array of std::array have contiguous memory? and Is the size of std::array defined by standard.

            – Daniel Langr
            9 hours ago








          • 1





            IOW, while the memory allocated has to be contiguous, the array elements need not be.

            – MSalters
            8 hours ago






          • 1





            Ooh this answer just gets posher and posher. Upped to 13.

            – Bathsheba
            7 hours ago








          • 1





            @Bathsheba The diagram is appalling but oh well 😂

            – Lightness Races in Orbit
            7 hours ago






          • 2





            @DanielLangr That was a great discussion. Thanks for adding here...

            – Const
            5 hours ago



















          12














          The big difference between std::vector and std::array is that std::vector contains a pointer to the memory it wraps, while std::array contains the actual array in itself.



          That means a vector of vectors is like a jagged array.



          For a vector of arrays, the std::array objects will be placed contiguously but separate from the vector object. Note that the std::array object themselves may be larger than the array they contain, and if so then the data will not be contiguous.



          The last bit also means that an array (plain C-style or std::array) of std::array may also not keep the data contiguously. The std::array objects in the array will be contiguous, but not the data.



          The only way to guarantee contiguous data for a "multi-dimensional" array is nested plain C-style arrays.






          share|improve this answer





















          • 3





            It also means that a vector of arrays is similar to an array of arrays, in that the data is all contiguous... I dare to disagree. Please, see my comment under LightnessRacesinOrbit's answer.

            – Daniel Langr
            9 hours ago






          • 2





            @DanielLangr Thanks for reminding me. Rephrased that part.

            – Some programmer dude
            9 hours ago



















          8














          The C++ standard does not guarantee that std::array doesn't contain any payload at the end of the array, so alas you cannot assume that the first element of a subsequent array is just after the last element of a prior array.



          Even if that were the case, the behaviour on attempting to reach any element in a array by pointer arithmetic on a pointer to an element in a different array is undefined. This is because pointer arithmetic is only valid within arrays.



          The above also applies to a std::array<std::array>.






          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',
            autoActivateHeartbeat: false,
            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%2f54197195%2fwhat-is-the-memory-layout-of-vector-of-arrays%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









            35














            Arrays do not have any indirection, but just store their data "directly". That is, a std::array<int, 5> literally contains five ints in a row, flat. And, like vectors, they do not put padding between their elements, so they're "internally contiguous".



            However, the std::array object itself may be larger than the set of its elements! It is permitted to have trailing "stuff" like padding. So, although likely, it is not necessarily true that your data will all be contiguous in the first case.



            An int
            +----+
            | |
            +----+

            A vector of 2 x int
            +----+----+----+-----+ +----+----+
            | housekeeping | ptr | | 1 | 2 |
            +----+----+----+-----+ +----+----+
            | ^
            -----------

            An std::array<int, 5>
            +----+----+----+----+----+----------->
            | 1 | 2 | 3 | 4 | 5 | possible cruft/padding....
            +----+----+----+----+----+----------->

            A vector of 2 x std::array<int, 5>
            +----+----+----+-----+ +----+----+----+----+----+----------------------------+----+----+----+----+----+----------->
            | housekeeping | ptr | | 1 | 2 | 3 | 4 | 5 | possible cruft/padding.... | 1 | 2 | 3 | 4 | 5 | possible cruft/padding....
            +----+----+----+-----+ +----+----+----+----+----+----------------------------+----+----+----+----+----+----------->
            | ^
            -----------


            And, even if it were, due to aliasing rules, whether you'd be able to use a single int* to navigate all 10 numbers would potentially be a different matter!



            All in all, a vector of ten ints would be clearer, completely packed, and possibly safer to use.



            In the case of a vector of vectors, a vector is really just a pointer plus some housekeeping, hence the indirection (as you say).






            share|improve this answer





















            • 8





              According to the answers here data don't have to be contiguous: Is the data in nested std::arrays guaranteed to be contiguous?. There are some discussion about this topic. Another discussions: Does std::array of std::array have contiguous memory? and Is the size of std::array defined by standard.

              – Daniel Langr
              9 hours ago








            • 1





              IOW, while the memory allocated has to be contiguous, the array elements need not be.

              – MSalters
              8 hours ago






            • 1





              Ooh this answer just gets posher and posher. Upped to 13.

              – Bathsheba
              7 hours ago








            • 1





              @Bathsheba The diagram is appalling but oh well 😂

              – Lightness Races in Orbit
              7 hours ago






            • 2





              @DanielLangr That was a great discussion. Thanks for adding here...

              – Const
              5 hours ago
















            35














            Arrays do not have any indirection, but just store their data "directly". That is, a std::array<int, 5> literally contains five ints in a row, flat. And, like vectors, they do not put padding between their elements, so they're "internally contiguous".



            However, the std::array object itself may be larger than the set of its elements! It is permitted to have trailing "stuff" like padding. So, although likely, it is not necessarily true that your data will all be contiguous in the first case.



            An int
            +----+
            | |
            +----+

            A vector of 2 x int
            +----+----+----+-----+ +----+----+
            | housekeeping | ptr | | 1 | 2 |
            +----+----+----+-----+ +----+----+
            | ^
            -----------

            An std::array<int, 5>
            +----+----+----+----+----+----------->
            | 1 | 2 | 3 | 4 | 5 | possible cruft/padding....
            +----+----+----+----+----+----------->

            A vector of 2 x std::array<int, 5>
            +----+----+----+-----+ +----+----+----+----+----+----------------------------+----+----+----+----+----+----------->
            | housekeeping | ptr | | 1 | 2 | 3 | 4 | 5 | possible cruft/padding.... | 1 | 2 | 3 | 4 | 5 | possible cruft/padding....
            +----+----+----+-----+ +----+----+----+----+----+----------------------------+----+----+----+----+----+----------->
            | ^
            -----------


            And, even if it were, due to aliasing rules, whether you'd be able to use a single int* to navigate all 10 numbers would potentially be a different matter!



            All in all, a vector of ten ints would be clearer, completely packed, and possibly safer to use.



            In the case of a vector of vectors, a vector is really just a pointer plus some housekeeping, hence the indirection (as you say).






            share|improve this answer





















            • 8





              According to the answers here data don't have to be contiguous: Is the data in nested std::arrays guaranteed to be contiguous?. There are some discussion about this topic. Another discussions: Does std::array of std::array have contiguous memory? and Is the size of std::array defined by standard.

              – Daniel Langr
              9 hours ago








            • 1





              IOW, while the memory allocated has to be contiguous, the array elements need not be.

              – MSalters
              8 hours ago






            • 1





              Ooh this answer just gets posher and posher. Upped to 13.

              – Bathsheba
              7 hours ago








            • 1





              @Bathsheba The diagram is appalling but oh well 😂

              – Lightness Races in Orbit
              7 hours ago






            • 2





              @DanielLangr That was a great discussion. Thanks for adding here...

              – Const
              5 hours ago














            35












            35








            35







            Arrays do not have any indirection, but just store their data "directly". That is, a std::array<int, 5> literally contains five ints in a row, flat. And, like vectors, they do not put padding between their elements, so they're "internally contiguous".



            However, the std::array object itself may be larger than the set of its elements! It is permitted to have trailing "stuff" like padding. So, although likely, it is not necessarily true that your data will all be contiguous in the first case.



            An int
            +----+
            | |
            +----+

            A vector of 2 x int
            +----+----+----+-----+ +----+----+
            | housekeeping | ptr | | 1 | 2 |
            +----+----+----+-----+ +----+----+
            | ^
            -----------

            An std::array<int, 5>
            +----+----+----+----+----+----------->
            | 1 | 2 | 3 | 4 | 5 | possible cruft/padding....
            +----+----+----+----+----+----------->

            A vector of 2 x std::array<int, 5>
            +----+----+----+-----+ +----+----+----+----+----+----------------------------+----+----+----+----+----+----------->
            | housekeeping | ptr | | 1 | 2 | 3 | 4 | 5 | possible cruft/padding.... | 1 | 2 | 3 | 4 | 5 | possible cruft/padding....
            +----+----+----+-----+ +----+----+----+----+----+----------------------------+----+----+----+----+----+----------->
            | ^
            -----------


            And, even if it were, due to aliasing rules, whether you'd be able to use a single int* to navigate all 10 numbers would potentially be a different matter!



            All in all, a vector of ten ints would be clearer, completely packed, and possibly safer to use.



            In the case of a vector of vectors, a vector is really just a pointer plus some housekeeping, hence the indirection (as you say).






            share|improve this answer















            Arrays do not have any indirection, but just store their data "directly". That is, a std::array<int, 5> literally contains five ints in a row, flat. And, like vectors, they do not put padding between their elements, so they're "internally contiguous".



            However, the std::array object itself may be larger than the set of its elements! It is permitted to have trailing "stuff" like padding. So, although likely, it is not necessarily true that your data will all be contiguous in the first case.



            An int
            +----+
            | |
            +----+

            A vector of 2 x int
            +----+----+----+-----+ +----+----+
            | housekeeping | ptr | | 1 | 2 |
            +----+----+----+-----+ +----+----+
            | ^
            -----------

            An std::array<int, 5>
            +----+----+----+----+----+----------->
            | 1 | 2 | 3 | 4 | 5 | possible cruft/padding....
            +----+----+----+----+----+----------->

            A vector of 2 x std::array<int, 5>
            +----+----+----+-----+ +----+----+----+----+----+----------------------------+----+----+----+----+----+----------->
            | housekeeping | ptr | | 1 | 2 | 3 | 4 | 5 | possible cruft/padding.... | 1 | 2 | 3 | 4 | 5 | possible cruft/padding....
            +----+----+----+-----+ +----+----+----+----+----+----------------------------+----+----+----+----+----+----------->
            | ^
            -----------


            And, even if it were, due to aliasing rules, whether you'd be able to use a single int* to navigate all 10 numbers would potentially be a different matter!



            All in all, a vector of ten ints would be clearer, completely packed, and possibly safer to use.



            In the case of a vector of vectors, a vector is really just a pointer plus some housekeeping, hence the indirection (as you say).







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited 8 hours ago

























            answered 10 hours ago









            Lightness Races in OrbitLightness Races in Orbit

            286k51465788




            286k51465788








            • 8





              According to the answers here data don't have to be contiguous: Is the data in nested std::arrays guaranteed to be contiguous?. There are some discussion about this topic. Another discussions: Does std::array of std::array have contiguous memory? and Is the size of std::array defined by standard.

              – Daniel Langr
              9 hours ago








            • 1





              IOW, while the memory allocated has to be contiguous, the array elements need not be.

              – MSalters
              8 hours ago






            • 1





              Ooh this answer just gets posher and posher. Upped to 13.

              – Bathsheba
              7 hours ago








            • 1





              @Bathsheba The diagram is appalling but oh well 😂

              – Lightness Races in Orbit
              7 hours ago






            • 2





              @DanielLangr That was a great discussion. Thanks for adding here...

              – Const
              5 hours ago














            • 8





              According to the answers here data don't have to be contiguous: Is the data in nested std::arrays guaranteed to be contiguous?. There are some discussion about this topic. Another discussions: Does std::array of std::array have contiguous memory? and Is the size of std::array defined by standard.

              – Daniel Langr
              9 hours ago








            • 1





              IOW, while the memory allocated has to be contiguous, the array elements need not be.

              – MSalters
              8 hours ago






            • 1





              Ooh this answer just gets posher and posher. Upped to 13.

              – Bathsheba
              7 hours ago








            • 1





              @Bathsheba The diagram is appalling but oh well 😂

              – Lightness Races in Orbit
              7 hours ago






            • 2





              @DanielLangr That was a great discussion. Thanks for adding here...

              – Const
              5 hours ago








            8




            8





            According to the answers here data don't have to be contiguous: Is the data in nested std::arrays guaranteed to be contiguous?. There are some discussion about this topic. Another discussions: Does std::array of std::array have contiguous memory? and Is the size of std::array defined by standard.

            – Daniel Langr
            9 hours ago







            According to the answers here data don't have to be contiguous: Is the data in nested std::arrays guaranteed to be contiguous?. There are some discussion about this topic. Another discussions: Does std::array of std::array have contiguous memory? and Is the size of std::array defined by standard.

            – Daniel Langr
            9 hours ago






            1




            1





            IOW, while the memory allocated has to be contiguous, the array elements need not be.

            – MSalters
            8 hours ago





            IOW, while the memory allocated has to be contiguous, the array elements need not be.

            – MSalters
            8 hours ago




            1




            1





            Ooh this answer just gets posher and posher. Upped to 13.

            – Bathsheba
            7 hours ago







            Ooh this answer just gets posher and posher. Upped to 13.

            – Bathsheba
            7 hours ago






            1




            1





            @Bathsheba The diagram is appalling but oh well 😂

            – Lightness Races in Orbit
            7 hours ago





            @Bathsheba The diagram is appalling but oh well 😂

            – Lightness Races in Orbit
            7 hours ago




            2




            2





            @DanielLangr That was a great discussion. Thanks for adding here...

            – Const
            5 hours ago





            @DanielLangr That was a great discussion. Thanks for adding here...

            – Const
            5 hours ago













            12














            The big difference between std::vector and std::array is that std::vector contains a pointer to the memory it wraps, while std::array contains the actual array in itself.



            That means a vector of vectors is like a jagged array.



            For a vector of arrays, the std::array objects will be placed contiguously but separate from the vector object. Note that the std::array object themselves may be larger than the array they contain, and if so then the data will not be contiguous.



            The last bit also means that an array (plain C-style or std::array) of std::array may also not keep the data contiguously. The std::array objects in the array will be contiguous, but not the data.



            The only way to guarantee contiguous data for a "multi-dimensional" array is nested plain C-style arrays.






            share|improve this answer





















            • 3





              It also means that a vector of arrays is similar to an array of arrays, in that the data is all contiguous... I dare to disagree. Please, see my comment under LightnessRacesinOrbit's answer.

              – Daniel Langr
              9 hours ago






            • 2





              @DanielLangr Thanks for reminding me. Rephrased that part.

              – Some programmer dude
              9 hours ago
















            12














            The big difference between std::vector and std::array is that std::vector contains a pointer to the memory it wraps, while std::array contains the actual array in itself.



            That means a vector of vectors is like a jagged array.



            For a vector of arrays, the std::array objects will be placed contiguously but separate from the vector object. Note that the std::array object themselves may be larger than the array they contain, and if so then the data will not be contiguous.



            The last bit also means that an array (plain C-style or std::array) of std::array may also not keep the data contiguously. The std::array objects in the array will be contiguous, but not the data.



            The only way to guarantee contiguous data for a "multi-dimensional" array is nested plain C-style arrays.






            share|improve this answer





















            • 3





              It also means that a vector of arrays is similar to an array of arrays, in that the data is all contiguous... I dare to disagree. Please, see my comment under LightnessRacesinOrbit's answer.

              – Daniel Langr
              9 hours ago






            • 2





              @DanielLangr Thanks for reminding me. Rephrased that part.

              – Some programmer dude
              9 hours ago














            12












            12








            12







            The big difference between std::vector and std::array is that std::vector contains a pointer to the memory it wraps, while std::array contains the actual array in itself.



            That means a vector of vectors is like a jagged array.



            For a vector of arrays, the std::array objects will be placed contiguously but separate from the vector object. Note that the std::array object themselves may be larger than the array they contain, and if so then the data will not be contiguous.



            The last bit also means that an array (plain C-style or std::array) of std::array may also not keep the data contiguously. The std::array objects in the array will be contiguous, but not the data.



            The only way to guarantee contiguous data for a "multi-dimensional" array is nested plain C-style arrays.






            share|improve this answer















            The big difference between std::vector and std::array is that std::vector contains a pointer to the memory it wraps, while std::array contains the actual array in itself.



            That means a vector of vectors is like a jagged array.



            For a vector of arrays, the std::array objects will be placed contiguously but separate from the vector object. Note that the std::array object themselves may be larger than the array they contain, and if so then the data will not be contiguous.



            The last bit also means that an array (plain C-style or std::array) of std::array may also not keep the data contiguously. The std::array objects in the array will be contiguous, but not the data.



            The only way to guarantee contiguous data for a "multi-dimensional" array is nested plain C-style arrays.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited 9 hours ago

























            answered 10 hours ago









            Some programmer dudeSome programmer dude

            296k24250412




            296k24250412








            • 3





              It also means that a vector of arrays is similar to an array of arrays, in that the data is all contiguous... I dare to disagree. Please, see my comment under LightnessRacesinOrbit's answer.

              – Daniel Langr
              9 hours ago






            • 2





              @DanielLangr Thanks for reminding me. Rephrased that part.

              – Some programmer dude
              9 hours ago














            • 3





              It also means that a vector of arrays is similar to an array of arrays, in that the data is all contiguous... I dare to disagree. Please, see my comment under LightnessRacesinOrbit's answer.

              – Daniel Langr
              9 hours ago






            • 2





              @DanielLangr Thanks for reminding me. Rephrased that part.

              – Some programmer dude
              9 hours ago








            3




            3





            It also means that a vector of arrays is similar to an array of arrays, in that the data is all contiguous... I dare to disagree. Please, see my comment under LightnessRacesinOrbit's answer.

            – Daniel Langr
            9 hours ago





            It also means that a vector of arrays is similar to an array of arrays, in that the data is all contiguous... I dare to disagree. Please, see my comment under LightnessRacesinOrbit's answer.

            – Daniel Langr
            9 hours ago




            2




            2





            @DanielLangr Thanks for reminding me. Rephrased that part.

            – Some programmer dude
            9 hours ago





            @DanielLangr Thanks for reminding me. Rephrased that part.

            – Some programmer dude
            9 hours ago











            8














            The C++ standard does not guarantee that std::array doesn't contain any payload at the end of the array, so alas you cannot assume that the first element of a subsequent array is just after the last element of a prior array.



            Even if that were the case, the behaviour on attempting to reach any element in a array by pointer arithmetic on a pointer to an element in a different array is undefined. This is because pointer arithmetic is only valid within arrays.



            The above also applies to a std::array<std::array>.






            share|improve this answer




























              8














              The C++ standard does not guarantee that std::array doesn't contain any payload at the end of the array, so alas you cannot assume that the first element of a subsequent array is just after the last element of a prior array.



              Even if that were the case, the behaviour on attempting to reach any element in a array by pointer arithmetic on a pointer to an element in a different array is undefined. This is because pointer arithmetic is only valid within arrays.



              The above also applies to a std::array<std::array>.






              share|improve this answer


























                8












                8








                8







                The C++ standard does not guarantee that std::array doesn't contain any payload at the end of the array, so alas you cannot assume that the first element of a subsequent array is just after the last element of a prior array.



                Even if that were the case, the behaviour on attempting to reach any element in a array by pointer arithmetic on a pointer to an element in a different array is undefined. This is because pointer arithmetic is only valid within arrays.



                The above also applies to a std::array<std::array>.






                share|improve this answer













                The C++ standard does not guarantee that std::array doesn't contain any payload at the end of the array, so alas you cannot assume that the first element of a subsequent array is just after the last element of a prior array.



                Even if that were the case, the behaviour on attempting to reach any element in a array by pointer arithmetic on a pointer to an element in a different array is undefined. This is because pointer arithmetic is only valid within arrays.



                The above also applies to a std::array<std::array>.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered 9 hours ago









                BathshebaBathsheba

                176k27251373




                176k27251373






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54197195%2fwhat-is-the-memory-layout-of-vector-of-arrays%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







                    Popular posts from this blog

                    GameSpot

                    connect to host localhost port 22: Connection refused

                    Getting a Wifi WPA2 wifi connection