c++ - Memory layout of vector of POD objects -
suppose have simple c++ class,
class data { public: float data[3]; void clear() { data[0] = 0.0f; data[1] = 0.0f; data[2] = 0.0f } } and vector of data's,
std::vector<data> v(10); is safe assume &v[0].data[0] points array of 30 floats?
from standard
23.3.6.1 class template vector overview
the elements of vector stored contiguously, meaning if v vector t type other bool, obeys identity &v[n] == &v[0] + n 0 <= n < v.size()
so &v[0] indeed points beginning of 10 continuous data objects.
but layout of data have
9.2.13 class members
nonstatic data members of (non-union) class same access control (clause 11) allocated later members have higher addresses within class object. order of allocation of non-static data members different access control unspecified (11). implementation alignment requirements might cause 2 adjacent members not allocated after each other; so might requirements space managing virtual functions (10.3) , virtual base classes (10.1).
so cannot sure sizeof(data) == 3*sizeof(float), therefore general answer should be: it's not save assume 30 continuous floats.
Comments
Post a Comment