双重自由或腐败(fasttop) [英] double free or corruption (fasttop)

查看:210
本文介绍了双重自由或腐败(fasttop)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码的以下部分在执行 *检测到 的glibc时给了我这个信息./p>

我已经看过很多遍代码了,但是我无法让牧师看到我是如何滥用free (temp2)

bool found= false;
int x=0;
for ( x=0; x<=312500; x++)
{
    while (count <=32)
    {
        fscanf (file, "%d", &temp->num);  

        temp->ptr=NULL;

        newNode = (NODE *)malloc(sizeof(NODE));
        newNode->num=temp->num;
        newNode->ptr=NULL;

        if (first != NULL)
        {
            temp2=(NODE *)malloc(sizeof(NODE));

            temp2=first;
            while (temp2 != NULL && !found)
            {
                if (temp2->num == newNode->num) 
                {found=true;}

                temp2= temp2->ptr;
            }

            free(temp2);

            if (!found)
            { 
                last->ptr=newNode;
                last=newNode;
                count=count+1;
            }   
        }   
        else  
        {
            first = newNode;
            last = newNode;
            count=count+1;
        }

        fflush(stdin);
    }

解决方案

问题在这里:

        temp2=first;

基本上,当您释放temp2时,您首先释放的是内存,而不是这里分配的内存:

        temp2=(NODE *)malloc(sizeof(NODE));

,这仍然是内存泄漏,因为在分配之后,它无法再释放.

此外,您的代码可能还有更多问题(一个问题是您不应该在输入流上使用fflush),但是如果没有更多细节,就无法确定.

The following section of my code gives me this messege when executing * glibc detected ./a.out: double free or corruption (fasttop): 0x08e065d0 **

i have gone through the code many times but i cant clealry see how i am misusing the free (temp2)

bool found= false;
int x=0;
for ( x=0; x<=312500; x++)
{
    while (count <=32)
    {
        fscanf (file, "%d", &temp->num);  

        temp->ptr=NULL;

        newNode = (NODE *)malloc(sizeof(NODE));
        newNode->num=temp->num;
        newNode->ptr=NULL;

        if (first != NULL)
        {
            temp2=(NODE *)malloc(sizeof(NODE));

            temp2=first;
            while (temp2 != NULL && !found)
            {
                if (temp2->num == newNode->num) 
                {found=true;}

                temp2= temp2->ptr;
            }

            free(temp2);

            if (!found)
            { 
                last->ptr=newNode;
                last=newNode;
                count=count+1;
            }   
        }   
        else  
        {
            first = newNode;
            last = newNode;
            count=count+1;
        }

        fflush(stdin);
    }

解决方案

The problem is here:

        temp2=first;

Basically, when you free temp2, you free first, not the memory allocated here:

        temp2=(NODE *)malloc(sizeof(NODE));

, which remains a memory leak, because after the assignment it can't be freed anymore.

Also, your code has probably some more problems (one is that you shouldn't use fflush on an input stream), but without some more details, it's impossible to tell.

这篇关于双重自由或腐败(fasttop)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆