I have a previously allocated chunk of memory that I want to interpret in-place as a struct
. How could I determine the memory address within the chunk that has the friendliest alignment for the struct
?
Just need to know the mechanism for determining what byte boundary a given struct
would work best within, basically.
// 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";