std::vector and contiguous memory of multidimensional arrays

后端 未结 6 1815
青春惊慌失措
青春惊慌失措 2020-11-27 07:42

I know that the standard does not force std::vector to allocate contiguous memory blocks, but all implementations obey this nevertheless.

Suppo

6条回答
  •  一向
    一向 (楼主)
    2020-11-27 07:56

    Under the hood, a vector may look approximately like (p-code):

    class vector {
        T      *data;
        size_t  s;
    };
    

    Now if you make a vector >, there will be a layout like this

    vector> --> data {
        vector,
        vector,
        vector
    };
    

    or in "inlined" form

    vector> --> data {
        {data0, s0},
        {data1, s1},
        {data2, s2}
    };
    

    Yes, the vector-vector therefore uses contiguous memory, but no, not as you'd like it. It most probably stores an array of pointers (and some other variables) to external places.

    The standard only requires that the data of a vector is contiguous, but not the vector as a whole.

提交回复
热议问题