C++ zlib内存不足问题

C++ zlib内存不足问题,c++,zlib,C++,Zlib,我想用zlib压缩字符串。如果我在大约一小时后将这个函数放入一个循环中,“compress”返回-4,这意味着Z_MEM_错误。有人知道问题出在哪里吗 std::string compressData(std::string const& line) { char *src=(char*)line.c_str(); int srcLen=strlen(src); int destLen=compressBound(srcLen); char *dest

我想用zlib压缩字符串。如果我在大约一小时后将这个函数放入一个循环中,“compress”返回-4,这意味着Z_MEM_错误。有人知道问题出在哪里吗

std::string compressData(std::string const& line)
{

    char *src=(char*)line.c_str();
    int srcLen=strlen(src);

    int destLen=compressBound(srcLen);
    char *dest=new char[destLen];

    int result=compress((unsigned char *)dest ,(uLongf*)&destLen ,(const unsigned char *)src ,srcLen );

    QByteArray sd = QByteArray::fromRawData(dest, destLen);
    QString hexZipData (sd.toHex());
    std::string hexZipDataStr = hexZipData.toStdString();

    if( result != Z_OK)
    {
       hexZipDataStr = "";
       std::cout << "error !"; 
    }

    delete []dest;
    dest = NULL;

    return hexZipDataStr;
}
std::string compressData(std::string const&line)
{
char*src=(char*)line.c_str();
int srcLen=strlen(src);
int destLen=压缩绑定(srcLen);
char*dest=新字符[destLen];
int result=compress((unsigned char*)dest,(uLongf*)和destLen,(const unsigned char*)src,srcLen);
QByteArray sd=QByteArray::fromRawData(dest,destLen);
QString-hexZipData(sd.toHex());
std::string hexZipDataStr=hexZipData.toStdString();
如果(结果!=Z_正常)
{
hexZipDataStr=“”;

std::cout我能看到的唯一可疑的地方是,您提供了
int destLen
作为类型为
uLongf
的输出参数。如果
uLongf
大于
int
,这可能会炸毁堆栈,并且“长”部分表明在64位平台上可能是这样

我建议您立即声明
destLen
类型为
uLongf
,并避免强制转换


除此之外,我看不出您的代码有任何问题。

char*src=(char*)line.c_str();
->->不要这样做。c_str()返回指向const char的指针,因此您正在丢弃constness。如果您使用c++-cast(在本例中为static_cast),编译器将能够警告您。(虽然这不是你问题的原因)你能发布一个最少的完整的例子吗?我猜Qt在这里是多余的。再次:使用C++的转换。可能,你转换不兼容的指针,例如在<代码>(ululf*)和DestLeN< /C>