jeudi 23 mai 2019

How do I create an instance of a class, variably sized, at a specific memory location?

So I'm working on a project involving writing packets to a memory mapped file. Our current strategy is to create a packet class containing the following members

uint32_t packetHeader;
uint8_t packetPayload[];
uint32_t packetChecksum;

When we create a packet, first we'd like to have it's address in memory be a specified offset within the memory mapped file, which I think can be done with placement new(). However, we'd also like for the packetPayload not to be a pointer to some memory from the heap, but contiguous with the rest of the class (so we can avoid memcpying from heap to our eventual output file)

I.E.

Memory

Beginning of class  | BOC + 4 | (length of Payload) |
Header                Payload   Checksum


Would this be achievable using a length argument for the Packet class constructor? Or would we have to template this class for variably sized payloads?

Aucun commentaire:

Enregistrer un commentaire