Search code examples
carrayspointersdynamic-memory-allocation

Allocating a 2D array using calloc


I am trying to dynamically allocate a 2D array using calloc.

Here is the code that I have tried.

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

I have initialised the following variables as shown

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

The following is the main code which loops through and tries to alter the 2D array.

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


}

This doesn't seem to work. I understand that doesn't seem to work is vague but as this code is a part of a large compiler, there is no way for me to print out the specific output. Apologies for that.

My question is: Have I initialised the arrays in the correct way and have is the way I modified the values of the elements in the array correct.

Thank you.


Solution

  • This

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

    is not an allocation of a two-dimensional array because at least the type of numberOfConstPiArray is int ** instead of for example int ( * )[kernelColumnCount].

    If your compiler supports variable length arrays then you could use the following approach as it is shown in the demonstrative program

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

    The program output is

     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 
    

    Or you can allocate an array of arrays the following way.

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

    The program output is the same as shown above.