繁体   English   中英

glibc检测到free()无效的大小(快速)错误?

[英]glibc detected free() invalid size (fast) error?

执行以下函数时出现此错误,我不知道这意味着什么。 这是函数:

void readf2()
{
    std::ifstream inFile("f2",std::ios_base::binary);
    std::string str(2, '\0');
    int i = 0;
    while(inFile.read(&str[i],2)){
    cout<<"Success: ["<< i << "] = "<< (int)str[i];                        ;
    cout<<"\n";
    i++;
    }
}

该功能可以工作一段时间,然后将各种数字写入控制台,然后由于出现此错误,回溯和内存映射而崩溃。 发生这种情况是因为我要释放一个不存在的内存地址吗?

您很可能会给read调用一个指向不属于您的内存的指针。

str[i]返回字符串中的偏移量,但不能保证您有足够的内存读取该位置(+2)。

您可能的意思是拥有一个int数组,并使用i作为该数组的索引:

void readf2()
{
    std::ifstream inFile("f2",std::ios_base::binary);
    std::vector< int >  str; // if you're reading int's - use int, not string
    int i = 0;
    int j;
    while(inFile.read(&j,sizeof(int))){ // check what the content is here
    str.push_back(j);
    cout<<"Success: ["<< i << "] = "<< str[i];
    cout<<"\n";
    i++;
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM