Q:

Serialization/Deserialization of a Vector of Integers in C++

Task to be Acomplished
I'm trying to serialize a vector of integers into a string so that it can be stored into a file. The approach used is to copy the integers byte-by-byte into a buffer. For this I used the std::copy_n function. 
To deserialize, I've done the same thing in reverse i.e. copied byte-by-byte into an integer from the buffer and appended those integers to a vector.
I'm not sure if this is the best/fastest way to achieve this.
Code
Serialize function
char *serialize(vector <int> nums)
{
    char *buffer = (char *)malloc(sizeof(int)*nums.size());
    vector <int>::iterator i;
    int j;
    for(i = nums.begin(), j = 0; i != nums.end(); i++, j += 4) {
        copy_n(i, 4, buffer+j);
    }
    return buffer;
}

Deserialize function
vector <int> deserialize(char *str, int len)
{
    int num;
    vector <int> ret;
    for(int j = 0; j < len; j+=4) {
        copy_n(str+j, 4, &num);
        ret.push_back(num);
    }
    return ret;
}

Any inputs on how I can improve this bit of code would be really helpful. I would also love to know other approaches to achieve the same.

A:

Your approach has a number of problems.
char *serialize(vector <int> nums)
{
    char *buffer = (char *)malloc(sizeof(int)*nums.size());
    vector <int>::iterator i;
    int j;
    for(i = nums.begin(), j = 0; i != nums.end(); i++, j += 4) {
        copy_n(i, 4, buffer+j);
    }
    return buffer;
}

1) It allocates memory manually, which is dangerous and rarely necessary.
2) It doesn't do what you think it does. It literally copies each int and tries to stuff it into a char. So the data is getting corrupted if any of the values are above 255 (the maximum number stuffable into a char).
If you are looking for efficiency then I would think the best way would be to write the data directly to the output stream rather than converting it to a string first.
Bear in mind, writing out binary data like this is not portable. I would only use this for serializing/deserializing local data. Ideally from a single session. Beyond that you have to start thinking about making each output data portable and it gets more complicated. Personally I would avoid the binary approach altogether unless absolutely necessary.
If you must do it, I would probably do something more like this:
template<typename POD>
std::ostream& serialize(std::ostream& os, std::vector<POD> const& v)
{
    // this only works on built in data types (PODs)
    static_assert(std::is_trivial<POD>::value && std::is_standard_layout<POD>::value,
        "Can only serialize POD types with this function");

    auto size = v.size();
    os.write(reinterpret_cast<char const*>(&size), sizeof(size));
    os.write(reinterpret_cast<char const*>(v.data()), v.size() * sizeof(POD));
    return os;
}

template<typename POD>
std::istream& deserialize(std::istream& is, std::vector<POD>& v)
{
    static_assert(std::is_trivial<POD>::value && std::is_standard_layout<POD>::value,
        "Can only deserialize POD types with this function");

    decltype(v.size()) size;
    is.read(reinterpret_cast<char*>(&size), sizeof(size));
    v.resize(size);
    is.read(reinterpret_cast<char*>(v.data()), v.size() * sizeof(POD));
    return is;
}

The interface to these functions follows the convention set in the Standard Library and it flexible enough that you can use it to serialize to files (using std::fstream) or strings (using std::stringstream).
std::vector<int> v = {1, 2, 3, 500, 900};

std::stringstream oss; // this could just as well be a `std::fstream` 

if(serialize(oss, v))
{
    std::vector<int> n;
    if(deserialize(oss, n))
    {
        for(auto i: n)
            std::cout << i << '\n';
    }
}

Output:
1
2
3
500
900

