gpt4 book ai didi

c++ - boost 原始 C 数组的序列化、反序列化

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:07:36 25 4
gpt4 key购买 nike

我正在尝试使用下面的示例序列化和反序列化原始 C 指针及其数据。它似乎序列化得很好,但我不确定如何使其反序列化——当我反序列化它时,它只是因内存访问冲突异常而崩溃。我想这是因为它不知道如何反序列化它,但我应该在哪里指定呢?

使用 vector 不是一种选择,在非常大的原始数据量中它非常慢

#include <stdint.h>
#include <string>
#include <iostream>
#include <fstream>
#pragma warning (push)
#pragma warning( disable : 4244 )
#include <boost/serialization/serialization.hpp>
#include <boost/serialization/vector.hpp>
#include <boost/serialization/string.hpp>
#include <boost/serialization/array.hpp>
#include <boost/archive/binary_oarchive.hpp>
#include <boost/archive/binary_iarchive.hpp>
#pragma warning (pop)

struct Monkey
{
uint32_t num;
float* arr;

};


namespace boost
{
namespace serialization
{
template<class Archive>
void serialize(Archive & ar, Monkey& m, const unsigned int version)
{
ar & m.num;
ar & make_array<float>(m.arr, m.num);
}
}
}


int _tmain(int argc, _TCHAR* argv[])
{
const char* name = "monkey.txt";

{
Monkey m;
m.num = 10;
m.arr = new float[m.num];
for (uint32_t index = 0; index < m.num; index++)
m.arr[index] = (float)index;

std::ofstream outStream(name, std::ios::out | std::ios::binary | std::ios::trunc);
boost::archive::binary_oarchive oar(outStream);
oar << (m);
}

Monkey m;
std::ifstream inStream(name, std::ios::in | std::ios::binary);
boost::archive::binary_iarchive iar(inStream);
iar >> (m);

return 0;
}

最佳答案

我衷心建议您在这里使用 std::arraystd::vector,因为...您搞砸了:)

对于初学者来说,Monkey 不会初始化它的成员。因此,加载最终会对 m.arr 恰好具有的任何指针值执行 load_binary。您如何期望反序列化“知道”您需要为此分配内存?你需要告诉它:

    template<class Archive>
void serialize(Archive & ar, Monkey& m, const unsigned int version)
{
ar & m.num;
if (Archive::is_loading::value)
{
assert(m.arr == nullptr);
m.arr = new float[m.num];
}
ar & make_array<float>(m.arr, m.num);
}

现在,让 Monkey 不那么不安全(通过添加初始化和销毁​​,也许最重要的是,禁止复制语义):

struct Monkey
{
uint32_t num;
float* arr;

Monkey() : num(0u), arr(nullptr) {}

Monkey(Monkey const&) = delete;
Monkey& operator=(Monkey const&) = delete;
~Monkey() { delete[] arr; }
};

现在,您可以看到它起作用了:

#include <iostream>
#include <fstream>
#pragma warning(disable: 4244)
#include <boost/serialization/serialization.hpp>
#include <boost/archive/binary_oarchive.hpp>
#include <boost/archive/binary_iarchive.hpp>

struct Monkey
{
uint32_t num;
float* arr;

Monkey() : num(0u), arr(nullptr) {}

Monkey(Monkey const&) = delete;
Monkey& operator=(Monkey const&) = delete;
~Monkey() { delete[] arr; }
};

namespace boost
{
namespace serialization
{
template<class Archive>
void serialize(Archive & ar, Monkey& m, const unsigned int version)
{
ar & m.num;
if (Archive::is_loading::value)
{
assert(m.arr == nullptr);
m.arr = new float[m.num];
}
ar & make_array<float>(m.arr, m.num);
}
}
}

int main(int argc, char* argv[])
{
const char* name = "monkey.txt";
{
Monkey m;
m.num = 10;
m.arr = new float[m.num];
for (uint32_t index = 0; index < m.num; index++)
m.arr[index] = (float)index;

std::ofstream outStream(name, std::ios::out | std::ios::binary | std::ios::trunc);
boost::archive::binary_oarchive oar(outStream);
oar << (m);
}

Monkey m;
std::ifstream inStream(name, std::ios::in | std::ios::binary);
boost::archive::binary_iarchive iar(inStream);
iar >> (m);

std::copy(m.arr, m.arr + m.num, std::ostream_iterator<float>(std::cout, ";"));
}

打印

0;1;2;3;4;5;6;7;8;9;

Live on Coliru

关于c++ - boost 原始 C 数组的序列化、反序列化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20597045/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com