C++序列化导致段错误
C++ serialization caused segment fault
我正在构建一个基于 Huffman 的压缩器,我使用Decoder
的序列化来存储将文件解压缩为名为 decoder.bin
的二进制文件所需的信息
在main.cpp
,它进展顺利:
#include "Huffman.hpp"
size_t hash(CodeType cd){
return cd;
}
int main() {
/* ------- BUILD -------- */
.....
/* ------- ENCODE -------- */
.....
/* ------- DECODE -------- */
Decoder dec(huffmanTree, root);
std::ofstream ofs("decoder.bin", std::ios::binary);
ofs.write ((char*)&dec, sizeof(Decoder));
ofs.close();
Decoder newDec;
std::ifstream ifs("decoder.bin", std::ios::binary);
ifs.read ((char*)& newDec, sizeof(Decoder));
ifs.close();
std::cout << newDec.tree.getCap();
newDec.restore();
newDec.decode("encoded.huf", "decoded.txt");
}
但是,如果我使用单独的decode.cpp
独立于编码工作的main.cpp
来实现解码过程,那么我的机器上会发生奇怪的事情。
> make -f makeDecoder
clang++ -c --std=c++11 decode.cpp -o bin/decode.o
clang++ bin/decode.o bin/HTree.o bin/Huffman.o bin/bitsMap.o -o decode
> ./decode
1000Restoring ...
Segmentation fault: 11
decode.cpp
的所有代码:
#include "Huffman.hpp"
int main() {
Decoder newDec;
std::ifstream ifs("decoder.bin", std::ios::binary);
ifs.read ((char*)& newDec, sizeof(Decoder));
ifs.close();
std::cout << newDec.tree.getCap();
newDec.restore();
newDec.decode("encoded.huf", "decoded.txt");
}
(1000是打印容量,忽略它(
所以这很奇怪,相同的代码行为不同,我不知道为什么会发生segment fault
。
如果您太忙而无法从github下载我的库并构建此程序,则可以忽略此问题,也谢谢。但如果你能:),对我有很大帮助
我的机器信息:
Apple LLVM version 6.0 (clang-600.0.54) (based on LLVM 3.5svn)
Target: x86_64-apple-darwin14.0.0
Thread model: posix
看起来你有两个主要功能。你在解码器.cpp和main.cpp中有一个,看起来你正在解码器制作文件中构建main。
相关文章:
- 为什么PyImport_ImportModule python 3.7.2 中出现段错误?
- 为什么在访问 vtkRenderWindow 的"交互器"变量时会发生段错误?
- 全局向量导致 C++ 程序结束时出现段错误
- 为什么重载运算符<<打印特征类成员会导致段错误?
- 更改条件段错误
- 使用 TTF_RenderText() 加载字体时获取段错误 TTF_OpenFontRW()
- 注册对对象工厂的调用会导致段错误
- pthread_create在构造函数段错误中
- Nanoflann发现邻居提出段错误
- C++ 中的构造函数、继承、堆栈、堆、this-pointer 和段错误
- 具有unique_ptr的 CRTP 会导致段错误
- 增强纤维work_stealing屏障会导致段错误
- 当我返回指向结构的指针向量时出现段错误
- C++为什么我的代码没有爆炸/段错误?
- Pthread段错误,使用指向main中变量的指针
- C++ 模板中的段错误
- 相当于Windows/MSVC上的段错误?
- 为什么我的 LLVM JIT 实现出现段错误?
- 为什么访问我的引用捕获变量会导致我的 lambda 函数出现段错误?
- 为什么自删除的全局 Vulkan 实例仅在添加层时才导致段错误?