c:1维未知时动态分配3d数组

时间:2012-01-23 18:32:07

标签: c multidimensional-array malloc

我正在尝试使用这些参数动态分配3d数组: 1)我知道的前2个维度,可以定义为常量(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
}

2 个答案:

答案 0 :(得分:0)

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

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

只需更改循环的界限,或将访问权限更改为brr[m][j][k]

答案 1 :(得分:0)

道歉,在我发布了你对过量malloc(或calloc)语句的厌恶之后我意识到了。代码使用过多的此语句,这将需要过多的清理(自由语句)。我会留下这个帖子,以防你可以使用它的某些部分。 RYK

以下是一些代码,用于动态创建和分配矩阵空间。它们还显示了连续更大尺寸矩阵的模式。注意,参数列表仅包括每个索引的所需大小(顺序);即行(r),然后是c,r(列,行),然后是p,c,r(页面,列,行),依此类推。注意这些函数也用calloc替换malloc,只是一个偏好。 问候, 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;
}