【问题标题】:glibc detected free() invalid next size (fast) after convertion type C++ [closed]glibc 在转换类型 C++ 后检测到 free() 无效的下一个大小(快速)[关闭]
【发布时间】:2012-11-14 20:11:43
【问题描述】:

我尝试将 unsigned char* 转换为 string 但问题是我从控制台收到此错误:

检测到glibc ** free():下一个大小无效(快速):0x097a1060

最小化代码:

unsigned char * base64= NULL;
base64 = (unsigned char *)"test";
std::string str((const char *)base64, strlen((const char*)base64)) ;
std::cout<<str; 

PS:我有一个返回无符号字符*的函数

谢谢。

【问题讨论】:

  • 您的代码是否使用返回无符号字符而不是文字“测试”的函数?如果是这样,那么该函数的主体可能就是实际问题所在。
  • 顺便说一句,(unsigned char *) 真的应该是 (const unsigned char *)
  • 我认为您的函数返回的指针不再有效。
  • 当我编译时,我执行时没有错误
  • 那是因为它是一个运行时错误。

标签: c++ c type-conversion


【解决方案1】:

我最好的猜测是你试图释放这个指针。

base64 = (unsigned char*)"test";

是对常量的引用。这是我的最小示例:

#include <string>
#include <cstring>
#include <iostream>
#include <cstdlib>

int main(int argc, char **argv)
{
        unsigned char * base64= NULL;
        base64 = (unsigned char *)"test";
        std::string str((const char *)base64, strlen((const char*)base64)) ;
        std::cout<<str<<std::endl;

        free(base64);

        return 0;
}

这会引发 glibc 错误和核心转储,这是理所当然的!没有 free(),一切正常。

【讨论】:

  • 谢谢。问题是我包含了一个包含错误的库。
猜你喜欢
  • 2012-02-22
  • 1970-01-01
  • 1970-01-01
  • 2017-03-12
  • 2011-04-12
  • 1970-01-01
  • 2017-03-28
  • 2011-06-26
相关资源
最近更新 更多