【问题标题】:Allocating a 2D array using calloc使用 calloc 分配二维数组
【发布时间】:2019-08-22 17:06:01
【问题描述】:

我正在尝试使用 calloc 动态分配二维数组。

这是我尝试过的代码。

int **      numberOfConstPiArray = calloc(invariannumberOfUniqueKernels * kernelColumnCount, sizeof(int *));

我已经初始化了以下变量,如图所示

    int numberOfUniqueKernels = 100;
    int kernelColumnCount = 10;
    int dimensionalMatrixColumnCount = 10;

以下是循环并尝试更改二维数组的主要代码。

for (int countKernel = 0; countKernel < numberOfUniqueKernels; countKernel++)
{
    int countNumberOfConst = 0;
    int numberOfTerms = 0;
    int numberOfConstPi = 0;

    for (int col = 0; col < kernelColumnCount; col++)
    {
        for (int row = 0; row < dimensionalMatrixColumnCount; row++)
        {
            if (some condition is satisfied)
            {
                countNumberOfConst += 1;
            }

            if (another condition satisfied)
            {
                numberOfTerms += 1;
            }

        }

        if(countNumberOfConst == numberOfTerms)
        {
            numberOfConstPi += 1;
            numberOfConstPiArray[countKernel][col] = 1;
        }

        countNumberOfConst=0;
        numberOfTerms=0;
    }


}

这似乎不起作用。我知道这似乎不起作用,但由于此代码是大型编译器的一部分,因此我无法打印出特定的输出。对此表示歉意。

我的问题是:我是否以正确的方式初始化了数组,并且是否以正确的方式修改了数组中元素的值。

谢谢。

【问题讨论】:

标签: c arrays pointers dynamic-memory-allocation


【解决方案1】:

这个

int **      numberOfConstPiArray = calloc(invariannumberOfUniqueKernels * kernelColumnCount, sizeof(int *));

不是二维数组的分配,因为至少numberOfConstPiArray 的类型是int ** 而不是例如int ( * )[kernelColumnCount]

如果您的编译器支持可变长度数组,那么您可以使用以下方法,如演示程序中所示

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

int main(void) 
{
    size_t n = 5;

    int ( *a )[n] = calloc( n * n, sizeof( int ) );

    for ( size_t i = 0; i < n; i++ )
    {
        for ( size_t j = 0; j < n; j++ ) a[i][j] = i * n + j;           
    }

    for ( size_t i = 0; i < n; i++ )
    {
        for ( size_t j = 0; j < n; j++ ) printf( "%2d ", a[i][j] );
        putchar( '\n' );
    }

    free( a );

    return 0;
}

程序输出是

 0  1  2  3  4 
 5  6  7  8  9 
10 11 12 13 14 
15 16 17 18 19 
20 21 22 23 24 

或者您可以通过以下方式分配数组数组。

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

int main(void) 
{
    size_t n = 5;

    int **a = calloc( n, sizeof( int * ) );

    for ( size_t i = 0; i < n; i++ )
    {
        a[i] = calloc( n, sizeof( int ) );
    }

    for ( size_t i = 0; i < n; i++ )
    {
        for ( size_t j = 0; j < n; j++ ) a[i][j] = i * n + j;           
    }

    for ( size_t i = 0; i < n; i++ )
    {
        for ( size_t j = 0; j < n; j++ ) printf( "%2d ", a[i][j] );
        putchar( '\n' );
    }

    for ( size_t i = 0; i < n; i++ )
    {
        free( a[i] );
    }

    free( a );

    return 0;
}

程序输出同上图。

【讨论】:

  • 感谢您的详细回答。如果我用int 替换size_t 会不会有问题for ( size_t i = 0; i &lt; n; i++ ) { for ( size_t j = 0; j &lt; n; j++ ) a[i][j] = i * n + j; }
  • 原因是我需要将迭代器作为整数来满足我上面指定的(某些条件)。
  • @TriposG 没问题。使用 int 类型的对象而不是 size_t 类型。
  • 当我运行上面的代码时,它给了我warning: Array access results in a null pointer dereference 请注意我已经改变了 a[i][j] = i * n + j;到 a[i][j] = 1;
  • @TriposG 提出一个显示您当前代码的新问题。
【解决方案2】:
numberOfConstPiArray[countKernel][col]

正在从numberOfConstPiArray[countKernel] 获取int*,然后尝试取消引用此int*col'th 元素,但失败了,因为numberOfConstPiArray[countKernel] 未使用对int 数组内存的引用进行初始化。

你可以改用:

int *      numberOfConstPiArray = calloc(invariannumberOfUniqueKernels * kernelColumnCount, sizeof(int));
memset(numberOfConstPiArray, 0, invariannumberOfUniqueKernels * kernelColumnCount, sizeof(int));

...
        numberOfConstPiArray[countKernel * kernelColumnCount + col] = 1;

【讨论】:

    猜你喜欢
    • 2011-10-01
    • 2012-08-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-18
    • 2019-03-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多