0

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);
1
  • The shared_ptr should be created at the same time when a is created. Commented Dec 13, 2017 at 10:26

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.