I have a Linux code.
I would like to pre-allocate 10000 items of different types as circular array. I always know which object type it is.
Since biggest object takes 54 bytes - I want to allocate 10000 x 54 chunk of memory.
Whats the correct pointer arithmetic to retrieve reference to an object with index i
?
x64 architecture
uint8_t cache[10000 * 54];
MyType* o = static_cast<MyType*>(cache + i * 54);
o.Prop1 = 10;
is this right?
EDIT: I need most efficient solution
EDIT2: these are instances of classes not structs (if that makes difference for aligning)
EDIT3: 54 byte is red herring, consider any "appropriate" aligned size, also I compile it with g++
as C++20
on CentOS9