c: 一维未知时三维数组的动态分配



我正在尝试动态分配一个3d数组,参数如下:1) 我知道的前两个维度可以定义为常量(ACOUNT和BCOUNT)。第三个需要在运行时决定。2) 我希望能够使用arr[I][j][k]=n对数组进行寻址;3) 我想通过一百万个以上的mallocs 来避免死亡

因此,在下面的代码中,使用"*arr"的第一部分效果很好,而使用"*brr"的第二部分则出现了令人遗憾的结局。有没有办法用一些星号的魔法药剂来实现这些参数?

(我正在使用VS2010 C++进行编译,因此出现了令人讨厌的铸件。)

#define ACOUNT 9000
#define BCOUNT 195
#define CCOUNT 8
short (*arr)[BCOUNT][CCOUNT];
short (*brr)[ACOUNT][BCOUNT];
void main(void)
{
    arr = (short (*)[BCOUNT][CCOUNT] )  malloc( (unsigned long) ACOUNT * BCOUNT *CCOUNT * sizeof(short));
        for (int j = 0; j < ACOUNT; ++j)
            for (int k = 0; k < BCOUNT; ++k)
                for (int m = 0; m < CCOUNT; ++m)
                    arr[j][k][m] = j + k + m;
    // still alive here
    brr = (short (*)[ACOUNT][BCOUNT] )  malloc( (unsigned long) ACOUNT * BCOUNT *CCOUNT * sizeof(short));
        for (int j = 0; j < ACOUNT; ++j)
            for (int k = 0; k < BCOUNT; ++k)
                for (int m = 0; m < CCOUNT; ++m)
                    brr[j][k][m] = j + k + m;
         // error: unhandled exception ...access violation... etc
}

抱歉,在我发布这篇文章后,我意识到你讨厌过多的malloc(或calloc)语句。代码使用了过多的这个语句,这将需要过多的清理(free语句)。如果你能使用其中的一部分,我会离开这个职位。Ryk

以下是一些用于动态创建和分配矩阵空间的代码。它们还显示了连续较大尺寸矩阵的模式。注意,参数列表仅包括每个索引所需的大小(顺序);即row(r),然后c,r(column,row),然后p,c,r,(page,column,rowe)等等。请注意,这些函数也将malloc替换为calloc,只是首选项。当做Ryk

double * Create1D(int r)
{   
    double *space; 
    double *arr;
    space = calloc(r*sizeof(double), sizeof(double));
    arr   = calloc(sizeof(double), sizeof(double));
    arr = (double *)space;
    return arr;
}
double ** Create2D(int c, int r)
{   
    double *space; 
    double **arr;
    int    y;
    space   = calloc(c*r*sizeof(double), sizeof(double));
    arr   = calloc(c * sizeof(double *), sizeof(double));
    for(y=0;y<c;y++)
    {
        arr[y] = (double *)space + (y*r);   
    }
    return arr;
}
double *** Create3D(int p, int c, int r) 
{
    double *space;
    double ***arr;
    int    x,y;
    space = calloc (p*c*r*sizeof(double),sizeof(double));
    arr = calloc(p * sizeof(double **), sizeof(double));
    for(x = 0; x < p; x++)
    {
        arr[x] = calloc(c * sizeof(double *),sizeof(double));
        for(y = 0; y < c; y++)
        {
            arr[x][y] = ((double *)space + (x*(c*r) + y*r));
        }
    }
    return arr;
}
double **** Create4D(int hR, int p, int c, int r)     
{
    double *space;
    double ****arr;
    int    w,x,y;
    space = calloc(hR*p*c*r*sizeof(double), sizeof(double));
    arr = calloc(hR * sizeof(double ***), sizeof(double));
    for(w=0;w<hR;w++)
    {
        arr[w] = calloc(p * sizeof(double **), sizeof(double));
        for(x=0;x<p;x++)
        {
            arr[w][x] = calloc(c * sizeof(double *), sizeof(double));
            for(y=0;y<c;y++)
            {                                        
                arr[w][x][y] = ((double *)space + (w*(p*c*r) + x*(c*r) + y*r)); 
            }
        }
    }
    return arr;
}
double ***** Create5D(int hC, int hR, int p, int c, int r) 
{
    double *space; 
    double *****arr;
    int    v,w,x,y;
    space = calloc(hC*hR*p*c*r*sizeof(double),sizeof(double));
    arr = calloc(hC * sizeof(double ****),sizeof(double));
    for(v=0;v<hC;v++)
    {
        arr[v] = calloc(hR * sizeof(double ***),sizeof(double));
        for(w=0;w<hR;w++)
        {
            arr[v][w] = calloc(p * sizeof(double **),sizeof(double));
            for(x=0;x<p;x++)
            {
                arr[v][w][x] = calloc(c * sizeof(double *),sizeof(double));
                for(y=0;y<c;y++)
                {
                    arr[v][w][x][y] = ((double *)space + (v*(hR*p*c*r) + w*(p*c*r) + x*(c*r) + y*r));   
                }
            }
        }
    }
    return arr;
}
double ****** Create6D(int hP, int hC, int hR, int p, int c, int r)
{
    double *space;
    double ******arr;
    int    u,v,w,x,y;
    space = calloc(hP*hC*hR*p*c*r*sizeof(double),sizeof(double));
    arr = calloc(hP * sizeof(double *****),sizeof(double));
    for(u=0;u<hP;u++)
    {
        arr[u] = calloc(hC * sizeof(double ****),sizeof(double));
        for(v=0;v<hC;v++)
        {
            arr[u][v] = calloc(hR * sizeof(double ***),sizeof(double));
            for(w=0;w<hR;w++)
            {
                arr[u][v][w] = calloc(p * sizeof(double **),sizeof(double));
                for(x=0;x<p;x++)
                {
                    arr[u][v][w][x] = calloc(c * sizeof(double *),sizeof(double));
                    for(y=0;y<c;y++)
                    {
                        arr[u][v][w][x][y] = ((double *)space + (u*(hC*hR*p*c*r) + v*(hR*p*c*r) + w*(p*c*r) + x*(c*r) + y*r));  
                    }
                }
            }
        }
    }
    return arr;
}

您对brr[j][k][m]的访问是非法的。注意,brr是指向short[ACOUNT][BCOUNT]CCOUNT数组的指针,所以当您访问它时,第一个索引应该小于CCOUNT

(编译器将第一个索引乘以ACOUNT*BCOUNT,将第二个索引乘以BCOUNT

只需更改循环的边界或更改对brr[m][j][k]的访问。

相关内容

  • 没有找到相关文章