Skip to content

Serialization shouldn't depend on output buffer's initial capacity #128

@0Xellos

Description

@0Xellos

I'm trying to use this library and I've encountered odd behavior:

#include <iostream>
#include <cstddef>
#include <vector>
#include <bitsery/bitsery.h>
#include <bitsery/adapter/buffer.h>
#include <bitsery/traits/vector.h>

std::vector<std::byte> serialize(const std::vector<float> &input) {
    std::vector<std::byte> output;
    // output.reserve(input.size() * 4);

    bitsery::Serializer<bitsery::OutputBufferAdapter<std::vector<std::byte>>> serializer{output};

    serializer.container4b(input, input.size());
    serializer.adapter().flush();
    return output;
}

int main() {
    int N;
    std::cin >> N;
    std::vector<float> input(N, 0);
    std::cout << serialize(input).size() << std::endl;
}

When the reserve() is uncommented, the printed size changes. For example with N=1000 it goes from 4002 (no reserve) to 6080 (with reserve).

It shouldn't happen since the contents of the object output, taken as an abstract data container, are unchanged; only internal state of the specific implementation (i.e. its capacity) changes. Serialization may modify this internal state but mustn't serialize the input container to different bytes depending on it or write garbage bytes. The contents of a vector aren't the memory it has allocated i.e. .capacity(). It contains only .size() elements.

I don't know if I'm doing something invalid here or if this is intended for bitsery. Help?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions