*** Error in `./a.out': double free or corruption (!prev): 0x096fb008 *** Aborted (core dumped)

5k views Asked by At

I am getting the error same as the title. I think it is happening because of matrix A but can't understand why and how to fix it.

Kindly help me out. Here is the complete code. I am using g++ to compile the code.

#include <stdio.h>
#include <fstream>
#include <math.h>
#include <string.h>
using namespace std;
void count()
{
    FILE *file, *output, *list;
    file = fopen("coordinate", "r");
    output = fopen("number", "w");
    list = fopen("list", "w");
    int min;
    printf("Enter the minimum number of atoms for critical clusters :");
    scanf("%d",&min);
    int i, j, count=0, cnt=0, fin_count=0, id;
    int nmonomer= 165;
    double x, y, z, sumx, sumy, sumz, sum=0;
    char dump[1000];
    while (fgets(dump, 1000, file)!=NULL)
    {
        if (strlen(dump)==1)
            count++;
    }
    fclose(file);
    printf("%d\n",count);
    file = fopen("coordinate", "r");
    int clus_cnt[count];
    for(i=0; i<count; i++)
        clus_cnt[i] = 0;
    while (fgets(dump, 1000, file)!=NULL)
    {
        if (strlen(dump)==1)
        {
            fprintf(list, "%5d%5d\n", (cnt+1), clus_cnt[cnt]);
            cnt++;
        }
        else
        {
            fprintf(list, "%s", dump);
            clus_cnt[cnt]++;
        }
    }
    fclose(file);
    fclose(list);
    file = fopen("list", "r");
    for (i=0; i<count; i++)
    {
        if (clus_cnt[i] >= min)
        {
            sumx=0;
            sumy=0;
            sumz=0;
            for (j=0; j<clus_cnt[i]; j++)
            {
                fscanf(file, "%lf%lf%lf\n", &x, &y, &z);
                sumx += x;
                sumy += y;
                sumz += z;
            }
            //fprintf (output, "%8d%8d%12.3f%12.3f%12.3f\n", i+1, clus_cnt[i], sumx, sumy, sumz);
            fprintf (output, "%5d%5d%8.3f%8.3f%8.3f\n", i+1, clus_cnt[i], sumx/(double)clus_cnt[i], sumy/(double)clus_cnt[i], sumz/(double)clus_cnt[i]);
            fgets(dump, 1000, file);
            fin_count++;
        }
        else
        {
            for (j=0; j<clus_cnt[i]+1; j++)
                fgets(dump, 1000, file);
        }       
    }
    printf("%d\n", fin_count);
    fclose(file);
    fclose(output);
    file = fopen("number", "r");    
    for (i=0; i<fin_count; i++)
    {
        fscanf(file, "%d%d%lf%lf%lf\n", &id, &cnt, &x, &y, &z);
        sum += cnt;
    }
    sum /= (double)fin_count;
    printf ("The Average Number of Clusters :%8.3f\n", sum);
    fclose(file);
}
int main()
{
    FILE *file, *output, *coords, *selection;
    file = fopen("dump.sti", "r");
    output = fopen("cluster_list", "w");
    coords = fopen("coordinate", "w");
    double cutoff;
    printf("Enter the cutoff distance :");
    scanf("%lf",&cutoff);
    int i, natom=8122, nmonomer=165, nstep=9, id, type, index=0, j, lk, mol, k, sel, s;
    double xs, ys, zs, xi, yi, zi, x[nmonomer], y[nmonomer], z[nmonomer], xj, yj, zj, vx, vy, vz, dist, rxk, ryk, rzk, q;
    int l[nmonomer], ind[nmonomer], flag[nmonomer];
    char dump[1000];
    int A[8122];
    for (i=0; i<8122; i++)
    { fscanf(file, "%d%d%lf%lf%lf%lf%lf%lf\n", &id, &type, &xs, &ys, &zs, &xi, &yi, &zi);
      A[i] = type;
    }
    fclose(file);
    for (s=0; s<natom; s++)
    {
        selection = fopen("selection_fxfg", "r");
    fscanf(file, "%d%d%lf%lf%lf%lf%lf%lf\n", &id, &type, &xs, &ys, &zs, &xi, &yi, &zi);
        for (i=0; i<nmonomer; i++)
        {
              fscanf(selection, "%d", &sel);    
              if (id == sel)
              {
                x[index] = xs + (46.1206 + 40.1206)*xi;
                y[index] = ys + (86.8457 + 56.8457)*yi;
                z[index] = zs + (35.9228 + 35.9228)*zi;
                ind[index] = id;   
                index++;
              }
        }
       fclose(selection);
     }

    printf("%d\n", index);
    //-------------
    for (i=0; i<nmonomer; i++)
        l[i] = i;
    for (i=0; i<nmonomer-1; i++)
    {
        if (i == l[i])
        {
            j=i;
            xj = x[j];
            yj = y[j];
            zj = z[j];
            for (k=i+1; k<nmonomer; k++)
            {
                lk = l[k];
                if (lk == k)
                {
                    rxk = xj-x[k];
                    ryk = yj-y[k];
                    ryk = yj-y[k];
                    rzk = zj-z[k];
                    dist = sqrt(pow(rxk,2)+pow(ryk,2)+pow(rzk,2));
                    if (dist <= cutoff)
                    {
                        l[k] = l[j];
                        l[j] = lk;
                    }
                }
            }
            j = l[j];
            xj = x[j];
            yj = y[j];
            zj = z[j];
            while (j != i)
            {
                for (k=i+1; k<nmonomer; k++)
                {
                    lk = l[k];
                    if (lk == k)
                    {
                        rxk = xj-x[k];
                        ryk = yj-y[k];
                        rzk = zj-z[k];
                        dist = sqrt(pow(rxk,2)+pow(ryk,2)+pow(rzk,2));
                        if (dist <= cutoff)
                        {
                            l[k] = l[j];
                            l[j] = lk;
                        }
                    }
                }
                j = l[j];
                xj = x[j];
                yj = y[j];
                zj = z[j];
            }
        }
    }
    //-------------
    for (i=0; i<nmonomer; i++)
    {
        flag[i] = 0;
        fprintf(output, "%8d%8d\n", i+1, l[i]+1);
    }

    int lit;
    for (i=0; i<nmonomer; i++)
    {
        if (flag[i] == 0)
        {
            lit = l[i];
            if (lit != i)
            {
                fprintf(coords, "%10.3f %10.3f %10.3f%d\n", x[i], y[i], z[i],A[i]);
                while (lit != i)
                {
                    flag[lit] = 1;
                    fprintf(coords, "%10.3f %10.3f %10.3f%d\n", x[lit], y[lit], z[lit], A[lit]);
                    lit = l[lit];
                }
                fprintf(coords, "\n");
            }
            else
                fprintf(coords, "%10.3f %10.3f %10.3f%d\n\n", x[i], y[i], z[i], A[i]);          
        }
    }
    fclose(file);
    fclose(output);
    fclose(coords);
    count();
}

Thanks a lot in advance.

2

There are 2 answers

0
The Dark On

You have a call to fclose(file) in the middle of main, but continue to read from it and close it again at the end of main.

Two calls to fclose on the same file handle may cause a double-free error (or may do something else entirely in a different run).

1
Will On

just remove line 102

fclose(file);