"./a.out"中的错误:双重释放或损坏(!prev):0x096fb008***中止(核心转储)



我得到的错误与标题相同。我认为这是由于矩阵 A 而发生的,但无法理解为什么以及如何修复它。

请帮帮我。这是完整的代码。我正在使用g ++来编译代码。

#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("%dn",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%5dn", (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%lfn", &x, &y, &z);
                sumx += x;
                sumy += y;
                sumz += z;
            }
            //fprintf (output, "%8d%8d%12.3f%12.3f%12.3fn", i+1, clus_cnt[i], sumx, sumy, sumz);
            fprintf (output, "%5d%5d%8.3f%8.3f%8.3fn", 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("%dn", fin_count);
    fclose(file);
    fclose(output);
    file = fopen("number", "r");    
    for (i=0; i<fin_count; i++)
    {
        fscanf(file, "%d%d%lf%lf%lfn", &id, &cnt, &x, &y, &z);
        sum += cnt;
    }
    sum /= (double)fin_count;
    printf ("The Average Number of Clusters :%8.3fn", 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%lfn", &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%lfn", &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("%dn", 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%8dn", 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%dn", x[i], y[i], z[i],A[i]);
                while (lit != i)
                {
                    flag[lit] = 1;
                    fprintf(coords, "%10.3f %10.3f %10.3f%dn", x[lit], y[lit], z[lit], A[lit]);
                    lit = l[lit];
                }
                fprintf(coords, "n");
            }
            else
                fprintf(coords, "%10.3f %10.3f %10.3f%dnn", x[i], y[i], z[i], A[i]);          
        }
    }
    fclose(file);
    fclose(output);
    fclose(coords);
    count();
}

提前非常感谢。

你在 main

的中间有一个调用fclose(file),但继续从中读取并在 main 结束时再次关闭它。

对同一文件句柄的两次调用可能会导致双重释放错误(或者可能在不同的运行中完全执行其他操作)。

只需删除第 102 行

fclose(file);

相关内容

  • 没有找到相关文章

最新更新