ric
ric

Reputation: 139

How to serialize multidimensional array on cereal, C++ serialize library

Does anyone know how to serialize multidimensional array on cereal, C++ library?

I tested by the source code shown below. but, it complains

"error C2338: Cereal does not support serializing raw pointers - please use a smart pointer"

As shown in the code, a smart pointer "shared_ptr" was already used.

What is the wrong point?

const int square_size = 3;  

int** a = new int*[square_size];
for (int i = 0; i < square_size; i++) {
    a[i] = new int[square_size];
}

std::shared_ptr<int*> sp(a, [](int** a) {for (int i = 0;i < square_size;i++) { delete a[i]; }});

std::ofstream ofs("output.cereal", std::ios::binary);
cereal::BinaryOutputArchive archive(ofs);
archive(sp);

Upvotes: 0

Views: 637

Answers (1)

Azoth
Azoth

Reputation: 1672

You are still serializing a raw pointer - your shared_ptr is holding an int *, so when cereal goes to dereference the smart pointer it finds itself trying to serialize a raw pointer, which is not something it is supports.

One of the easiest solutions for your particular example would be to consider using std::vector in place of the raw pointer with new, which would also save you the effort of writing that custom destructor in your shared_ptr.

If this is just a reduced example, you'll have to restructure your code not to have raw pointers owning data if you want cereal to serialize it.

Upvotes: 1

Related Questions