我有一个问题,我尝试压缩文件的数据。一切都适用于压缩调用,但它不是压缩调用本身,因为在它之前抛出了segfault。显示我的代码会更清楚:
std::cout << "FILENAME: ";
std::cin >> filename;
if(!fileExists(filename))
{
std::cout << "ERR: FILE NOT FOUND." << std::endl;
continue;
}
std::cout << "Compressing file data...";
writeFile(filename, zlib_compress(readFile(filename)));
std::cout << " Done." << std::endl;
在函数zlib_compress ...
std::string zlib_compress(const std::string& str)
{
std::cout << "DEBUG" << std::endl;
z_stream zs; // z_stream is zlib's control structure
memset(&zs, 0, sizeof(zs));
if (deflateInit(&zs, 9) != Z_OK)
std::cout << "deflateInit failed while compressing." << std::endl;
zs.next_in = (Bytef*)str.data();
zs.avail_in = str.size(); // set the z_stream's input
int ret;
char outbuffer[1073741824];
std::string outstring;
// retrieve the compressed bytes blockwise
do
{
zs.next_out = reinterpret_cast<Bytef*>(outbuffer);
zs.avail_out = sizeof(outbuffer);
ret = deflate(&zs, Z_FINISH);
if (outstring.size() < zs.total_out)
{
// append the block to the output string
outstring.append(outbuffer, zs.total_out - outstring.size());
}
} while(ret == Z_OK);
deflateEnd(&zs);
if(ret != Z_STREAM_END) // an error occurred that was not EOF
{
std::ostringstream oss;
oss << "Exception during zlib compression: (" << ret << ") " << zs.msg;
std::cout << oss.str();
}
return outstring;
}
我知道,我知道,这个功能需要工作,我只是从某个地方进行C&amp; P'd尝试。
但问题是:
std::cout << "DEBUG" << std::endl;
从不被调用。编译器说seg故障来自这里:
std::string zlib_compress(const std::string& str)
> {
但为什么......?它早先工作了。我只是不知道出了什么问题!
编辑:调试器输出。
#0 00000000 0x00402cbb in __chkstk_ms() (??:??)
#1 004013BE zlib_compress(str=...) (C:\Users\***\Documents\Work\Programming\Compressor\z.cpp:5)
#2 00401DDA _fu15___ZSt4cout() (C:\Users\***\Documents\Work\Programming\Compressor\main.cpp:80)
答案 0 :(得分:6)
char outbuffer [1073741824];
太大而无法叠加
答案 1 :(得分:0)
您将zlib_compress中的字符串作为参数持续引用 - 您需要确保zlib_compress中的内存可用(从readfile返回的内容)。如果您也可以共享readFile函数的原型,那将是件好事。