Search code examples
cmemory-leaksmallocinfinite-loopfree

Why does my function go into infinite loop sometimes?


I have the following code. It is running fine. But sometimes the del and ins functions are going into infinite loop but sometimes working fine. The readt function is working fine, still I have included it for your reference. What is the problem with my del and ins? Is there any memory leak?

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<string.h>
#include<math.h>
#include<unistd.h>

struct node
{   int info;
    struct node *link;
};
typedef struct node m;

m *search(int,m*);
m *del(int,m*);
m *ins(int,int,m*);
int posof(int,m*);


int readt(m *t_c,char path[])
{   int t,szt=0;
    FILE *tfile;
    char ch;
    char filename[]="/temp.txt";
    strcat(path,filename);
    tfile=fopen(path,"r");
    if(tfile==NULL)
        exit(0);
    fseek(tfile, 0, SEEK_END);
    szt = ftell(tfile);
    fseek(tfile,0,SEEK_SET);
    if(szt!=0)
    {   while(1)
        {   fscanf(tfile,"%d%c",&t,&ch);
            t_c->info=t;
            t_c->link=(m*)malloc(sizeof(m));
            t_c=t_c->link;
            if(ch==';')
                break;
        }       
    }
    t_c->link=NULL;
    //free(t_c);
    fclose(tfile);
    return 0;
}

m *search(int Noftarget,m *t_c)
{   int i,p1,p2;
    srand(time(NULL));
    for(i=0;i<100;i++)
    {   p1=(1+rand()%(Noftarget));
        p2=(1+rand()%(Noftarget));
        t_c=del(p1,t_c);
        t_c=ins(p1,p2,t_c);
        break;
    }
    return t_c;
}

m *del(int target,m *t_h)
{   m *t_c;
    int j=1,i;
    t_c=t_h;
    i=posof(target,t_h);
    if(i==1)
    {   t_c=t_c->link;
        t_h=t_c;
    }
    else
    {   while(j<i-1)
        {   t_c=t_c->link;
            j++;
        }
        t_c->link=t_c->link->link;
    }
    return t_h;
}

m *ins(int target,int position,m *t_h)
{   int j=0;
    m *swaptarget,*t_c;
    t_c=t_h;
    swaptarget=(m*)malloc(sizeof(m));
    swaptarget->info=target;
    if(position==1)
    {   swaptarget->link=t_c;
        t_h=swaptarget;
    }
    else
    {   while(j<position-2)
        {   t_c=t_c->link;
            j++;
        }
        swaptarget->link=t_c->link;
        t_c->link=swaptarget;
    }
    free(swaptarget);
    return t_h;
}

int posof(int p1,m *t_c)
{   int i=1,a=0;
    while(t_c->link!=NULL)
    {   if(p1==t_c->info)
        {   a=i;
            break;
        }
        t_c=t_c->link;
        i++;
    }
    return a;
}

int main()
{   int Noftarget=8,j,r=1,count=0,noi,szd_n=0,i=0,sz;
    char cwd[200];
    m *t_h;
    getcwd(cwd, sizeof(cwd));
    t_h=(m*)malloc(sizeof(m));
    readt(t_h,cwd);
    t_h=search(Noftarget,t_h);
    free(t_h);
    return 0;
}

and the content of temp file is: 1,2,3,4,5,6,7,8;


Solution

  • The program contains memory leaks. Memory is allocating iteratively inside the while loop but only one pointer is removing at the end. Need to remove all allocations. And there is no need to free any pointer at the ins function rather del function needs the free operation of deleted pointer. The modified code is here:

    #include<stdio.h>
    #include<stdlib.h>
    #include<time.h>
    #include<string.h>
    #include<math.h>
    #include<unistd.h>
    
    struct node
    {   int info;
        struct node *link;
    };
    typedef struct node m;
    
    m *search(int,m*);
    m *del(int,m*);
    m *ins(int,int,m*);
    int posof(int,m*);
    
    
    int readt(m *t_c,char path[])
    {   int t,szt=0;
        FILE *tfile;
        char ch;
        char filename[]="/temp.txt";
        strcat(path,filename);
        tfile=fopen(path,"r");
        if(tfile==NULL)
            exit(0);
        fseek(tfile, 0, SEEK_END);
        szt = ftell(tfile);
        fseek(tfile,0,SEEK_SET);
        if(szt!=0)
        {   while(1)
            {   fscanf(tfile,"%d%c",&t,&ch);
                t_c->info=t;
                t_c->link=(m*)malloc(sizeof(m));
                //printf("%d ",t_c->info);
                t_c=t_c->link;
                if(ch==';')
                    break;
            }       
        }
        t_c->link=NULL;
        //free(t_c);
        fclose(tfile);
        return 0;
    }
    m *search(int Noftarget,m *t_c)
    {   int i,p1,p2;
        srand(time(NULL));
        for(i=0;i<100;i++)
        {   p1=(1+rand()%(Noftarget));
            p2=(1+rand()%(Noftarget));
            t_c=del(p1,t_c);
            t_c=ins(p1,p2,t_c);
            break;
        }
        return t_c;
    }
    m *del(int target,m *t_h)
    {   m *t_c;
        int j=1,i;
        t_c=t_h;
        i=posof(target,t_h);
        if(i==1)
        {   free(t_c);
            t_c=t_c->link;
            t_h=t_c;
        }
        else
        {   while(j<i-1)
            {   t_c=t_c->link;
                j++;
            }
            free(t_c->link);
            t_c->link=t_c->link->link;
        }
        return t_h;
    }
    m *ins(int target,int position,m *t_h)
    {   int j=0;
        m *swaptarget,*t_c;
        t_c=t_h;
        swaptarget=(m*)malloc(sizeof(m));
        swaptarget->info=target;
        if(position==1)
        {   swaptarget->link=t_c;
            t_h=swaptarget;
        }
        else
        {   while(j<position-2)
            {   t_c=t_c->link;
                j++;
            }
            swaptarget->link=t_c->link;
            t_c->link=swaptarget;
        }
        return t_h;
    }
    int posof(int p1,m *t_c)
    {   int i=1,a=0;
        while(t_c->link!=NULL)
        {   if(p1==t_c->info)
            {   a=i;
                break;
            }
            t_c=t_c->link;
            i++;
        }
        return a;
    }
    
    int main()
    {   int Noftarget=7,j,r=1,count=0,noi,szd_n=0,i=0,sz;
        char cwd[200];
        m *t_h;
        getcwd(cwd, sizeof(cwd));
        t_h=(m*)malloc(sizeof(m));
        readt(t_h,cwd);
        print_tsp(t_h);
        t_h=search(Noftarget,t_h);
        print_tsp(t_h);
        while(t_h!=NULL)
        {   free(t_h);
            t_h=t_h->link;
        }
        return 0;
    }
    

    It is checked by valgrind and does not have any memory leak.