我正在构建a compressor based on 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
答案 0 :(得分:0)
看起来你有两个主要功能。你在decoder.cpp和main.cpp中有一个,看起来你正在解码器生成文件中构建main。