0
votes

I am using Flatbuffers with C++. I would like to create an array of bytes in a struct that is the size of the generated table (I am sending the contents as a payload for a NanoMSG message).

How does one do a sizeof(table)?

#include "pnt_generated.h"

struct packetStruct {
    Topics topic;
    int payloadSize;
    uint8_t payload[sizeof(pnt)];
};

does not work directly.

2
In C++ you cannot declare variable size arrays (a FlatBuffer is not a fixed size known ahead of time). Note that you can create size prefixed buffers in FlatBuffers. So rather than trying to wrap a FlatBuffer in your own struct, you can use FlatBuffers for all of it. - Aardappel
@Aardappel I need to wrap it in a packet, since I am using a variant of ZeroMQ to publish it on the net, and the first bytes of the packet are the channel/topic. The rest is the payload which is Flatbuffers. - Dr.YSG
can reserve a std::vector instead of array.. struct packet { Topics topic; int pSize; std::vector<uint8> payload(sizeof(pnt)); } - Shivendra Agarwal
That does not help one get a contiguous memory block with the channel topic as the first bytes - Dr.YSG

2 Answers

0
votes

Since it seems that Flatbuffers dynamically sets the size (in my case of the payload). And no one has a better idea, I am creating a fix sized payload in the struct, and then checking to see if I exceed that:

#define PayloadMax 256
#include "pnt_generated.h"

struct packetStruct {
    Topics topic;
    int payloadSize;
    uint8_t payload[PayloadMax];
};
0
votes

You can get the size of your payload from the bufferbuilder: flatbuffers::FlatBufferBuilder builder(1024);
auto l = CreateLogEvent(builder, builder.CreateString("INFO"), builder.CreateString("main.c"), builder.CreateString("Test Log Entry")); FinishLogEventBuffer(builder, l); auto ptr = builder.GetBufferPointer(); auto size = builder.GetSize();