So, I need the memory to be lay out in a specific way:
struct aPacketWithMoreData{
double x;
int16 h;
int16 noNeedToBePassedToTheBuffer;
double y;
}
struct aPacket{
double x;
double y;
int16 h;
}
struct bufferData{
int32 something;
uint64 anotherthing;
aPacket arrayOfData[]; //with unknown size or runtime size
float32 somethingThatEndThis;
int16 withMaybeWeirdSizeAlinement;
}
And then I hace an array of aPacketWithMoreData. I need to make a temp bufferData so that I can call the API (which is very slow) once to copy the buffer. What is the best way to do this? (Note that storing a std::vector doesn't help because a std::vector is actually just a pointer to an array, and in the other side of the API, it will not be able to de-reference that.)
I know that in c++ you can do something like this: (link: https://en.cppreference.com/w/c/language/struct)
struct s{
int a;
float b;
double c[]; //array of unknown size
}
...
void function(uint numberOfC) {
struct s objectName = malloc(sizeof (struct s) + sizeof(double)*numberOfC);
...
}
But I believe that this is for portabilities with c. This would also not work when the array is in the middle (required for the buffer structure). So other than hacking it like below, is there a better way?
void function(uint number) {
char* buffer = new char[sizeof(int32)+sizeof(uint64)+sizeof(float32)+sizeof(int16)+sizeof(aPacket)*number];
*(int32*)(buffer[0]) = returnsInt32();
*(uint64*)(buffer[sizeof(int32)]) = returnsuint64();
...
size_t offset = sizeof(int32)+sizeof(uint64);
for (uint i=0; i<number; i++) {
aPacketWithMoreData& obj = aVector[i]; //inore the possible out of bound for now
*((aPacket*)(buffer[offset])+i) = aPacket(obj.x,obj.y,obj.h);
}
...
AnAPI.passData(buffer, sizeof(buffer));
}
Edit: Fixed the char array thingy.