dimanche 3 janvier 2016

Assumptions on memory layout of objects

For which categories of types (standard layout, POD, trivial type, alignment constraints...) are the following assumptions valid?

B is a subclass of A, and B* b a pointer to a B object.

  1. A* a = b has the same address as b, i.e. static_cast<A*>(b) == reinterpret_cast<A*>(b).

  2. B bs[n] is an array of B objects. &bs[i] == static_cast<B*>(static_cast<void*>(bs) + i * sizeof(B)).

  3. offsetof can be used to access data members of A and of B, from b: int i = *static_cast<int*>(static_cast<void*>(b) + offsetof(B, m_i)).

Aucun commentaire:

Enregistrer un commentaire