Can I use FlatBuffers to serialize/deserialize C/C++ pointers?

270 views
Skip to first unread message

Lucky Boy

unread,
May 31, 2019, 11:22:15 PM5/31/19
to FlatBuffers
Hi all,
I have a simple question, which is about serializing pointers. In C/C++, since pointers don't carry bounds information, how can FlatBuffers know the size of a buffer (e.g. void*) and generate the IDL file automatically? 
The worse thing is for more complicated pointer types such as struct sshkey** pp, do I need to use long buffer size to marshal? Thanks!

mikkelfj

unread,
Jun 1, 2019, 2:04:49 PM6/1/19
to FlatBuffers
The IDL is not generated automatically.

You manually write a FlatBuffers schema:

When data is serialised you start the buffer by calling a generated function suitable for the type you want to serialise, then you set one field at a time, or call constructor function that takes multiple arguments.

If you need to write data from a string or an array (a vector in FlatBuffer terms), there are dedicated functions to deal with that.

See the monster example:

When you go the opposite direction an deserialise the buffer, there are generated functions to read the expected type. If the buffer is valid these functions will not know how large the buffer is, but they will know how to read the data present and avoid reading outside the buffer. If the buffer is invalid you might get a crash, but you can call a verifier which take a buffer size as input and checks that it is safe to read the buffer.

When navigating the buffer you do not need the size, and you actually also do not need the start of the buffer, except when starting out. This is because all information is stored as relative offsets to the parent data.
Reply all
Reply to author
Forward
0 new messages