-2

または、Facing a error — glibcが空きの無効な次のサイズ(高速)を検出しました。

私は次のように定義されているbondと呼ばれる構造体を持っています:

typedef struct{
    int type1;
    int type2;
    int id_1;
    int id_2;
    float dist;
} bond;

これらの構造体の配列をネストされたループに割り当て、定期的に解放しています。ただし、何らかの理由で、free()のInvalidNextsizeエラーが発生します。コードは以下のとおりです。

while(i<(C.num_type_A+C.num_type_B)){//1a
    bond_arr=(bond*)malloc(100*sizeof(bond));

    a=-N1;
    while(a<=N1){//2a

        b=-N2;
        while(b<=N2){//3a

            c=-N3;
            while(c<=N3){//4a

                j=0;
                while(j<(C.num_type_A+C.num_type_B)){//5a

                    //if the same atom do nothing
                    if((i==j)&&(a==0)&&(b==0)&&(c==0)){//6a
                        j=j+1;
                    }//6b
                    else{//7a

                        //calculate bond length
                        bondlength=calc_dist(a,b,c,i,j,C);

                        if(bondlength<=cutoff){//8a
                            //store bond
                            temp_bond.type1=((i+1)<=C.num_type_A);
                            temp_bond.type2=((j+1)<=C.num_type_B);
                            temp_bond.id_1=i;
                            temp_bond.id_2=j;
                            temp_bond.dist=bondlength;
                            bond_arr[n]=temp_bond;
                            n=n+1;

                            //if out of memory allocate twice as much
                            if(n==(nmax-1)){//9a
                                printf("begin\n");
                                temp_ptr=realloc(bond_arr,sizeof(bond)*2*nmax);
                                printf("end\n");

                                if(temp_ptr==NULL){
                                    printf("Memory allocation failed\n");
                                }
                                else{
                                    bond_arr=temp_ptr;
                                }
                                nmax=2*nmax;
                            }//9b
                        }//8b
                    }//7b
                    j=j+1;
                }//5b
                c=c+1;
            }//4b
            b=b+1;
        }//3b
        a=a+1;
    }//2b

    //sort bonds and update LI index
    sort_bonds(bond_arr,n);
    f=update_LI(bond_arr,LIcurr,n);
    LIcurr=f;
    printf("%d\n",n);
    free(bond_arr);
    n=0;
    i=i+1;
}//1b
4

1 に答える 1

0

reallocロジックが原因のようです。nサイズ100の初期割り当て。その後、に達すると拡大されますnmax-1。の初期値nmaxは表示されていません(私が見るように)。100から開始しても、ループの先頭でリセットされません。したがって、n100を超えて再割り当てが発生した場合、nmaxは2倍になり、元のサイズの100とは一致しなくなります。

于 2013-03-26T22:04:24.703 に答える