My question is about a typical process to parse a byte array like the following code:
struct Header { /* ... */ };
struct Entry1 { /* ... */ };
struct Entry2 { /* ... */ };
void Parse(char* p) {
const auto header = reinterpret_cast<Header*>(p);
p += sizeof(Header);
const auto ent1 = reinterpret_cast<Entry1*>(p);
p += sizeof(Entry1);
const auto ent2 = reinterpret_cast<Entry2*>(p);
}
First of all, the spec says that char*
can alias any other pointer type, so reinterpret_cast<Header*>
is safe.
However what about the other reinterpret_cast
statemetns,
are they violating the strict aliasing rule because p
, whose type is char*
, has already been aliased with Header*
? or safe because p
is incremented by sizeof(Header)
?
Thank you.