std::vector and c-style arrays

前端 未结 3 2002
没有蜡笔的小新
没有蜡笔的小新 2021-02-15 09:49

I am experimenting with OpenCL to increase the speed of our software. We work with maps a lot and, to simplify, represent a map as a std::vector< std::vector >. The OpenCL AP

3条回答
  •  孤城傲影
    2021-02-15 10:24

    Are there implementation guarantees in the stl that vector is, internally, consecutive in memory?

    Although I cannot quote the standards here, I have seen code in high-quality libraries assuming this layout (namely, POCO).

    Can I safely cast a std::vector to int* and expect that to work?

    Specifically, you cannot recast the vector itself. But, I have seen the following code:

    std::vector vec;
    int* ptr = &vec[0];
    

    In the case of a vector of vectors, can I still assume this holds true? I would expect the vector to hold other state data, or alignment issues, or maybe something else...

    You probably cannot cast a vector of vectors to a linear array. Each vector will reserve its own memory range and you cannot expect all of these ranges to be sequencial.

提交回复
热议问题