How to create vector in memory and others can read it

74 views
Skip to first unread message

Li yonggang

unread,
Sep 28, 2017, 3:26:52 PM9/28/17
to FlatBuffers
We are solving the problem by providing some default value with flatbuffer format, so we need to generate flatbuffers:String, flatbuffers:vector<>. The question is what is the best way to create those flatbuffer object in memory and others can read it.
I tried:
int invs[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
  auto vs = builder.CreateVector(invs, 10);
  builder.Finish(vs);

  // we will return p to users, so they can read like flatbuffer.
  auto p =
      flatbuffers::GetRoot<std::vector<int>>(builder.GetBufferPointer());

but looks like it does not work. any suggestion?

Wouter van Oortmerssen

unread,
Sep 28, 2017, 4:10:07 PM9/28/17
to Li yonggang, FlatBuffers
The in-memory representation of a FlatBuffer vector is not compatible with a std::vector (std::vector implies memory allocation of a separate buffer, FlatBuffers is, well, flat).

flatbuffers::GetRoot<FlatBuffers::Vector<int>> may work better.

Do note that even this is very non-standard, as normally the root of a FlatBuffer must always be a table, so you should really use a schema like:

table MyVec { vec:[int]; }

If you don't want to deal with schemas, and just want to stick arbitrary data types into a flat buffer, FlexBuffers may be more appropriate for you: https://google.github.io/flatbuffers/flexbuffers.html


--
You received this message because you are subscribed to the Google Groups "FlatBuffers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to flatbuffers+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Li yonggang

unread,
Sep 28, 2017, 4:53:31 PM9/28/17
to FlatBuffers
Oh, yeah, Thanks Wouter! I made a mistake, that should be flatbuffer::Vector! Thanks for pointing out.
Reply all
Reply to author
Forward
0 new messages