c为循环中的2d指针数组分配空间

时间:2016-01-14 00:16:53

标签: c arrays pointers malloc allocation

我有一个相当大的程序,需要我使用指向二维数组的指针。我很难为阵列分配空间。

我已经尝试在申报时分配空间,但我遇到了一百万个路障。

我在这里找到了代码:Create a pointer to two-dimensional array

我有一个指向数组other_arrays

的2D数组

我有这个:

    static double other_arrays[51][1];
    double (*SumH)[51][1] = &other_arrays;
    double (*WeightIH)[51][1] = &other_arrays;
    double (*Hidden)[51][1] = &other_arrays;
    double (*SumO)[51][1] = &other_arrays;
    double (*WeightHO)[51][1] = &other_arrays;
    double (*Output)[51][1] = &other_arrays;
    double (*DeltaWeightIH)[51][1] = &other_arrays;
    double (*DeltaWeightHO)[51][1] = &other_arrays;
    for (i = 0; i < 2; i++){
      for (j = 0; j < 51; j++){
        SumH[j][i] = (double *)malloc(sizeof(double));
        WeightIH[j][i] = (double *)malloc(sizeof(double));
        Hidden[j][i] = (double *)malloc(sizeof(double));
        SumO[j][i] = (double *)malloc(sizeof(double));
        WeightHO[j][i] = (double *)malloc(sizeof(double));
        Output[j][i] = (double *)malloc(sizeof(double));
        DeltaWeightIH[j][i] = (double *)malloc(sizeof(double));
        DeltaWeightHO[j][i] = (double *)malloc(sizeof(double));
      }
    }

当我编译时,我得到:error: array type 'double [1]' is not assignable

我尝试了一些我在网上找到的东西,例如SumH[j] = (double *)malloc(sizeof(double));

然后我得到:error: array type 'double [51][1]' is not assignable

或类似的东西会产生同样的错误:

  for (j = 0; j < 51; j++){
    SumH[j] = (double *)malloc(sizeof(double));
    WeightIH[j] = (double *)malloc(sizeof(double));
    Hidden[j] = (double *)malloc(sizeof(double));
    SumO[j] = (double *)malloc(sizeof(double));
    WeightHO[j] = (double *)malloc(sizeof(double));
    Output[j] = (double *)malloc(sizeof(double));
    DeltaWeightIH[j] = (double *)malloc(sizeof(double));
    DeltaWeightHO[j] = (double *)malloc(sizeof(double));
  }

解决方案

我没有投出malloc

    *SumH[j][i] = *(double *)malloc(sizeof(double));

2 个答案:

答案 0 :(得分:1)

如果要分配2d数组:

    /* allocate 2d arrays */
    double (*SumH)[51][1] = malloc(51*1*sizeof(double));
    double (*WeightIH)[51][1] = malloc(51*1*sizeof(double));
    double (*Hidden)[51][1] = malloc(51*1*sizeof(double));
    double (*SumO)[51][1] = malloc(51*1*sizeof(double));
    double (*WeightHO)[51][1] = malloc(51*1*sizeof(double));
    double (*Output)[51][1] = malloc(51*1*sizeof(double));
    double (*DeltaWeightIH)[51][1] = malloc(51*1*sizeof(double));
    double (*DeltaWeightHO)[51][1] = malloc(51*1*sizeof(double));
    /* ... */
    free(DeltaWeightHO);
    free(DeltaWeightIH);
    free(Output);
    free(WeightHO);
    free(SumO);
    free(Hidden);
    free(WeightIH);
    free(SumH);

用法是...(* SumH)[j] [i] ...

我怀疑这是否是你想要的。 要分配指向第一行2d数组的指针:

    /* allocate pointers to first row of 2d arrays */
    double (*SumH)[1] = malloc(51*1*sizeof(double));
    double (*WeightIH)[1] = malloc(51*1*sizeof(double));
    double (*Hidden)[1] = malloc(51*1*sizeof(double));
    double (*SumO)[1] = malloc(51*1*sizeof(double));
    double (*WeightHO)[1] = malloc(51*1*sizeof(double));
    double (*Output)[1] = malloc(51*1*sizeof(double));
    double (*DeltaWeightIH)[1] = malloc(51*1*sizeof(double));
    double (*DeltaWeightHO)[1] = malloc(51*1*sizeof(double));

用法是...... SumH [j] [i] ...

分配行指针数组

    /* allocate array of pointers to rows */
    double (*SumH[51])[1];
    for(i = 0; i < sizeof(SumH)/sizeof(SumH[0]); i++)
        SumH[i] = malloc(1*sizeof(double));

用法是...... SumH [j] [i] ...

答案 1 :(得分:1)

如果你想要指向数组并想要初始化它们,那么你应该更简单地做一些事情:

double (*SumH)[5][2] = malloc(sizeof(*SumH));
double (*WeightIH)[5][2] = malloc(sizeof(*WeightIH));

现在你可以使用:

int k = 0;
for (int i = 0; i < 5; i++)
{
    for (int j = 0; j < 2; j++)
    {
        (*SumH)[i][j] = k;
        (*WeightIH)[i][j] = k++;
    }
}

请注意,[1]的数组维度接近无意义。

FWIW,valgrind为以下代码提供了清晰的健康状况:

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
    double (*SumH)[5][2] = malloc(sizeof(*SumH));
    double (*WeightIH)[5][2] = malloc(sizeof(*WeightIH));


    int k = 0;
    for (int i = 0; i < 5; i++)
    {
    for (int j = 0; j < 2; j++)
    {
        (*SumH)[i][j] = k;
        (*WeightIH)[i][j] = k++;
    }
    }

    for (int i = 0; i < 5; i++)
    {
    for (int j = 0; j < 2; j++)
        printf("[%f, %f]", (*SumH)[i][j], (*WeightIH)[i][j]);
    putchar('\n');
    }

    free(SumH);
    free(WeightIH);

    return 0;
}

输出不是很令人兴奋:

[0.000000, 0.000000][1.000000, 1.000000]
[2.000000, 2.000000][3.000000, 3.000000]
[4.000000, 4.000000][5.000000, 5.000000]
[6.000000, 6.000000][7.000000, 7.000000]
[8.000000, 8.000000][9.000000, 9.000000]