这个十分古怪的Segmentation fault产生原因?

请问这个函数究竟是怎么会产生Segmentation fault的,

void update_list(double4* r, int N, Neighbor* nei, double3 Box, Potential *pot, int maxtype){
    int i, j, k, L, o, m[3], ri[3], rj[3];
    double **Rrange, **Rrangesq;
    int pti, ptj, maxpairs;
    double dx, dy, dz, Rsqij, maxRrange;
    double box[3];
    box[0] = Box.x;box[1] = Box.y; box[2] = Box.z;
    Rrange = (double **)malloc(maxtype * sizeof(double *));
    maxpairs = nei->Maxpairsperatom;
    printf("%d",maxpairs);
    for(i = 0; i < maxtype; i++) 
    {
        Rrange[i] = (double *)malloc(maxtype * sizeof(double));
    }   
    Rrangesq = (double **)malloc(maxtype * sizeof(double *));
    for(i = 0; i < maxtype; i++) 
    {
        Rrangesq[i] = (double *)malloc(maxtype * sizeof(double));
    }   


    maxRrange = 0;
    for(i = 0;i<maxtype;i++){
        for(j = 0; j < maxtype; j++){
            Rrange[i][j]=sqrt(pot->Rcutoffsq[i][j] + nei->Skin);
            Rrangesq[i][j]=Rrange[i][j]*Rrange[i][j];
            if(maxRrange<Rrange[i][j]){
                maxRrange = Rrange[i][j];
            }
        }
    }

    //Call new list
    new_list(r, N, nei, Box, maxRrange);
    nei->mMarker = (int *)malloc(N * sizeof(int));
    nei->pMarker = (int **)malloc(N * sizeof(int *));
    for(i = 0; i < N; i++)
    {
        nei->pMarker[i] = (int *)malloc(nei->Maxpairsperatom * sizeof(int));
    }

    for(i = 0; i<N; i++){
        ri[0]=r[i].x;ri[1]=r[i].y;ri[2]=r[i].z;pti = (int)r[i].w;
        for(j = 0; j<3; j++)
        {
            nei->iCell[j] = 1 + (int)((ri[j]/box[j]+0.5)*nei->nPart[j]);
            if(nei->iCell[j]>nei->nPart[j]) nei->iCell[j] = nei->nPart[j];
        }
        for(k = 0;k<14; k++)
        {
            if(k == 0){
                for(o = 0;o<3;o++) 
                {
                    m[o] = nei->iCell[o];
                }
            }
            if(k == 1)
            {
                m[0] = nei->iCell[0]+1;
                m[1] = nei->iCell[1];
                m[2] = nei->iCell[2];
            }
            if(k<=4 && k>=2)
            {
                m[0] = nei->iCell[0] + k - 3;
                m[1] = nei->iCell[1] + 1;
                m[2] = nei->iCell[2];
            }
            if(k<=7 && k>=5)
            {
                m[0] = nei->iCell[0] + k-6;
                m[1] = nei->iCell[1] - 1;
                m[2] = nei->iCell[2] + 1;
            }
            if(k<=10 && k>=8)
            {
                m[0] = nei->iCell[0] + k-9;
                m[1] = nei->iCell[1];
                m[2] = nei->iCell[2] + 1;
            }
            if(k<=13 && k>=11)
            {
                m[0] = nei->iCell[0] + k-12;
                m[1] = nei->iCell[1] + 1;
                m[2] = nei->iCell[2] + 1;
            }
        }
        if (m[0]==nei->iCell[0] && m[1] == nei->iCell[1] && m[2] == nei->iCell[2])
        {
            j = nei->CellList[i];
        }
        else{
            for (k = 0;k<3;k++)
            {
                if(m[k] >nei->nPart[k]) m[k] = nei->nPart[k]; 
                if(m[k] <= 0) m[k] = m[k] + nei->nPart[k];
            }
            j = nei->Head[m[0]][m[1]][m[2]];
        }
        L = 0;
        while(j != 0)
        {
            
            j = j - 1;
            rj[0] = r[j].x;
            rj[1] = r[j].y;
            rj[2] = r[j].z;
            dx = pbc(ri[0]-rj[0], box[0]);
            dy = pbc(ri[1]-rj[1], box[1]);
            dz = pbc(ri[2]-rj[2], box[2]);
            Rsqij = dx*dx + dy*dy + dz*dz;
            //L = L + 1;
            if(Rsqij<Rrangesq[pti][ptj])
            {
                if( L > maxpairs)
                {
                    printf("Update: List too small\n");
                    printf("%d\n", L); 
                    break;
                }  
                //nei->pMarker[i][L] = j;
                L = L + 1;
            }
            j = nei->CellList[j];
        }
        nei->mMarker[i] = L;
    }
}

只要在这个if语句中使用或访问L就会出现,做过如下尝试。

            L = L + 1;
            if(Rsqij<Rrangesq[pti][ptj])
            {
               /* if( L > nei->Maxpairsperatom)
                {
                    printf("Update: List too small\n");
                    printf("%d\n", L); 
                    system("pause");
                }  
                nei->pMarker[i][L] = j;
                L = L + 1;*/
            }

这样可以运行

            //L = L + 1;
            if(Rsqij<Rrangesq[pti][ptj])
            {
               /* if( L > nei->Maxpairsperatom)
                {
                    printf("Update: List too small\n");
                    printf("%d\n", L); 
                    system("pause");
                }  
                nei->pMarker[i][L] = j;*/
                L = L + 1;
            }

这样不可以运行,甚至做了如下极端的尝试

            if(Rsqij<Rrangesq[pti][ptj])
            {
                if( L > maxpairs)
                { 
                    break;
                }  
                L = L + 1;
            }

即便改成这样,还是不行
只要在if(Rsqij<Rrangesq[pti][ptj])内尝试访问L就不行

阅读 811
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
宣传栏