std::vector and contiguous memory of multidimensional arrays

后端 未结 6 1821
青春惊慌失措
青春惊慌失措 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 08:17

    For reference the way I currently create a 2D array in a contiguous memory block is by first making a (dynamic) array of float* of length N, allocating all N*5 floats in one array and then copying the address of every 5th element into the first array of float*.

    That's not a 2D array, that's an array of pointers. If you want a real 2D array, this is how it's done:

    float (*p)[5] = new float[N][5];
    
    p [0] [0] = 42;   // access first element
    p[N-1][4] = 42;   // access last element
    
    delete[] p;
    

    Note there is only a single allocation. May I suggest reading more about using arrays in C++?

提交回复
热议问题