简体   繁体   English

c 为循环中的二维指针数组分配空间

[英]c allocating space for a 2d array of pointers in a loop

I have a rather large program that requires me to use pointers to 2 dimensional arrays.我有一个相当大的程序,需要我使用指向二维数组的指针。 I'm having a difficult time allocating space for the arrays.我很难为数组分配空间。

I've already tried allocating the space at the time of declaration but I ran into a million road blocks.我在声明时已经尝试分配空间,但我遇到了一百万个路障。

I found code here: Create a pointer to two-dimensional array我在这里找到了代码: 创建一个指向二维数组的指针

I have a 2D array of pointers to an array other_arrays我有一个指向数组other_arrays的二维指针数组

I have this:我有这个:

    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));
      }
    }

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

I've tried some things I've found online such as SumH[j] = (double *)malloc(sizeof(double));我尝试了一些我在网上找到的东西,比如SumH[j] = (double *)malloc(sizeof(double));

But then I get: error: array type 'double [51][1]' is not assignable但后来我得到: error: array type 'double [51][1]' is not assignable

Or something like this yields the same error:或者这样的事情会产生同样的错误:

  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));
  }

SOLUTION解决方案

I didn't cast malloc我没有施放malloc

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

If you want to allocate 2d arrays:如果要分配二维数组:

    /* 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);

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

I doubt this is want you wanted.我怀疑这是你想要的。 To allocate pointers to the first row of 2d arrays:分配指向二维数组第一行的指针:

    /* 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));

Usage would be ... SumH[j][i] ...用法是 ... SumH[j][i] ...

To allocate array of pointers to rows分配指向行的指针数组

    /* 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));

Usage would be ... SumH[j][i] ...用法是 ... SumH[j][i] ...

If you want pointers to arrays and want to initialize them, then you should be doing things much more simply:如果你想要指向数组的指针并想要初始化它们,那么你应该做的更简单:

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

Now you can use:现在您可以使用:

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++;
    }
}

Note that an array dimension of [1] is close to pointless.请注意, [1]的数组维度几乎毫无意义。

FWIW, valgrind gives the following code a clean bill of health: 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;
}

The output is not very exciting:输出不是很令人兴奋:

[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]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM