My schema is:
table Object {
data: [ubyte]
}
I have the next code:
flatbuffers::FlatBufferBuilder fb;
std::uint8_t* data = nullptr;
const size_t size = 100;
auto vector = fb.CreateUninitializedVector(size, sizeof (std::uint8_t), &data);
memset(data, 1, size);
auto object = CreateObject(fb, vector);
fb.Finish(object);
But, I want to create uninitilized vector larger than required, and cut the vector size before send, like there:
flatbuffers::FlatBufferBuilder fb;
std::uint8_t* data = nullptr;
const size_t reserved_size = 100;
auto vector = fb.CreateUninitializedVector(reserved_size, sizeof (std::uint8_t), &data);
const size_t real_size = 60;
memset(data, 1, real_size);
// How to cut vector size to real size?
auto object = CreateObject(fb, vector);
fb.Finish(object);
There's currently no function in the API for this, no, and while it would be possible to add a function for this, it is a bit tricky, and like you already indicate, could only be done right after creation of the vector (front of the buffer == start of vector). You could open a PR/issue on the FlatBuffers repo to implement it.
One further issue is that vectors are "pre-aligned", meaning that if you specify an original size of 101 bytes it will likely write 3 padding bytes first (depending on whatever data came before it). If you then specify the new size as 100, the start of the vector would be misaligned which is not possible. So any such function would have to assert that the difference between old and new size is a multiple of 4 (4 being the size of the size field that precedes the vector).