【问题标题】:Serializing struct containing vector of pointers with each pointer containing other pointers序列化包含指针向量的结构,每个指针包含其他指针
【发布时间】:2018-05-30 13:20:33
【问题描述】:

我正在尝试使用 Boost 序列化一个包含向量和整数的 minHeap 对象。

struct minHeap {
    std::vector<keyNode> heapStructure; // A vector of many keyNodes
    int size; // Current size of vector
    minHeap() {
        size = 0;
    }
private:
    friend class boost::serialization::access;
    template<class Archive>
    void serialize(Archive & ar, const unsigned version)
    {
        ar & heapStructure;
        ar & size;
    }
}; 

现在heapStructurekeyNode 的向量,每个keyNode 包含一个字符、整数和对象keyNode 本身的两个指针。

struct keyNode {
    char data; // The character itself
    int frequency; // Occurances of the character in the data stream.
    keyNode * leftNode, *rightNode;  // Left and right children of the root node (the keyNode itself)
    keyNode() {
        data = NULL;
        frequency = 0;
        leftNode = NULL;
        rightNode = NULL;
    }
private:
    friend class boost::serialization::access;
    template<class Archive>
    void serialize(Archive & ar, const unsigned version)
    {
        ar &data;
        ar &frequency;
        ar &leftNode;
        ar &rightNode;

    }
};

下面的(示例)代码显示了我如何序列化和反序列化文件。

// Write Encoded Stream to File
ofstream outFile;
string outPath = filePath + ".enc";
outFile.open(outPath, ios::out | ios::binary); // TODO: Fix the output path
bitsetR bitStorage(outputStream);
boost::archive::binary_oarchive output(outFile);
output << bitStorage;
output << hTree;
outFile.close();
ifstream inFile;
inFile.open("demo.txt.enc"); // TODO: Fix the output path
bitsetR bitStr("");
boost::archive::binary_iarchive input(inFile);
minHeap temp;
input >> bitStr;
input >> temp;

序列化时我没有收到任何错误,但反序列化失败并出现以下错误(VS 2017):

Exception Thrown: Input Stream Error (boost::archive::archive_exception)

我应该在这里注意到bitsetR 对象成功反序列化。反序列化minHeap对象时抛出异常。

【问题讨论】:

  • 什么是bitsetR?什么是输出流?
  • @sehe bitsetR 是 boost::dynamic_bitset (我在您的答案之一中使用了代码来序列化动态位集)。 outputStream 只是一个字符串,我用它来初始化 bitsetR 对象。

标签: c++ pointers serialization boost deserialization


【解决方案1】:

我认为所需要做的就是以正确的顺序正确刷新和关闭存档和流。此代码工作正常:

Live On Coliru

#include <boost/serialization/serialization.hpp>
#include <boost/serialization/access.hpp>
#include <boost/serialization/vector.hpp>

struct keyNode {
    char data          = 0;       // The character itself
    int frequency      = 0;       // Occurances of the character in the data stream.
    keyNode *leftNode  = nullptr;
    keyNode *rightNode = nullptr; // Left and right children of the root node (the keyNode itself)

  private:
    friend class boost::serialization::access;
    template <class Archive> void serialize(Archive &ar, unsigned) {
        ar &data;
        ar &frequency;
        ar &leftNode;
        ar &rightNode;
    }
};

struct minHeap {
    std::vector<keyNode> heapStructure; // A vector of many keyNodes
    size_t size() const { return heapStructure.size(); }

  private:
    friend class boost::serialization::access;
    template <class Archive> void serialize(Archive &ar, unsigned) {
        ar &heapStructure;
    }
};

#include <boost/archive/binary_oarchive.hpp>
#include <boost/archive/binary_iarchive.hpp>
#include <fstream>

int main() {
    minHeap hTree;

    {
        std::ofstream outFile("demo.txt.enc", std::ios::binary);
        {
            boost::archive::binary_oarchive output(outFile);
            output << hTree;
        }
    }

    {
        std::ifstream inFile("demo.txt.enc");
        boost::archive::binary_iarchive input(inFile);
        minHeap temp;
        input >> temp;
    }
}

请注意,我可以通过删除范围使其失败并显示类似的消息:

int main() {
    minHeap hTree;

    std::ofstream outFile("demo.txt.enc", std::ios::binary);
    boost::archive::binary_oarchive output(outFile);
    output << hTree;

    std::ifstream inFile("demo.txt.enc");
    boost::archive::binary_iarchive input(inFile);
    minHeap temp;
    input >> temp;
}

打印Live On Coliru

terminate called after throwing an instance of 'boost::archive::archive_exception'
  what():  input stream error
bash: line 7: 16838 Aborted                 (core dumped) ./a.out

【讨论】:

  • 它正在为一个空的 minheap 工作。当我在其中插入数据(形成树)时,它会显示相同的错误。
  • 你能发布一个最小的、独立的例子吗?如果需要,可以从mine开始。
  • 感谢您的回复。我已经修复了大部分代码。序列化和反序列化适用于小数据。但是对于大数据(例如 500 K),它会失败。
  • 哦,没有反序列化的大对象是 boost::dynamic_bitset
  • 全部修复。我学到了一件事。永远不要使用 mvcc(和 Visual Studio)。从现在开始,我将信任 GCC。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-08-28
  • 2021-07-28
  • 1970-01-01
  • 1970-01-01
  • 2013-07-12
相关资源
最近更新 更多