2

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.

Dr.YSG
  • 7,171
  • 22
  • 81
  • 139
  • Perhaps this has some info on that https://groups.google.com/forum/#!topic/flatbuffers/7wuLsfjI3u8 – jspcal Jul 20 '18 at 17:29
  • 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 Jul 20 '18 at 18:18
  • @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 Jul 20 '18 at 20:05
  • can reserve a std::vector instead of array.. struct packet { Topics topic; int pSize; std::vector payload(sizeof(pnt)); } – Shivendra Agarwal Jul 23 '18 at 00:47
  • That does not help one get a contiguous memory block with the channel topic as the first bytes – Dr.YSG Jul 23 '18 at 01:45
  • Well it looks like no one has a better Idea for me to use a fixed size #define PayloadMax 256, then declary payload as uint_t[PayloadMax] and check in the builder if I exceed the size I don't expect bigger than 256, I don't think this will be a problem. And I am only sending the actuall size (int payloadsize) so what can get hurt? – Dr.YSG Jul 23 '18 at 19:33

2 Answers2

1

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();

0

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];
};
Dr.YSG
  • 7,171
  • 22
  • 81
  • 139