У меня есть ранее выделенный кусок памяти, который я хочу интерпретировать на месте как struct
.Как я могу определить адрес памяти в блоке, который имеет самое дружелюбное выравнивание для struct
?
Просто нужно знать механизм для определения того, какая граница байта для данного struct
будет работать лучше всего, в основном.
// psuedo-code
struct Object{
int theseMembersCould;
double beAnything;
char itsJustData[69];
}
// a chunk of previously allocated memory that I want to use
std::vector<uint8> block;
block.resize(1024);
uint32 byteBoundary = ????; // <-- this is what I want to discover
// math to get the nearest addr on the boundary (assumes byteBoundary will be POW2)
uint32 alignmentOffset= (byteBoundary - (block.data() & byteBoundary-1u)) & byteBoundary-1u;
Object * obj = new (block.data() + alignmentOffset) Object;
obj->itsJustData = "used as if it were a normal object beyond this point";