1

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.

TriposG
  • 103
  • 1
  • 2
  • 11
  • 2
    https://stackoverflow.com/questions/11463455/c-programming-initialize-2d-array-dynamically – user3386109 Aug 22 '19 at 17:15
  • Possible duplicate of [C programming initialize 2D array dynamically](https://stackoverflow.com/questions/11463455/c-programming-initialize-2d-array-dynamically) – cmwt Aug 22 '19 at 18:03

2 Answers2

2

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.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • Thanks for your detailed answer. Will it be a problem if I replace `size_t` with `int` `for ( size_t i = 0; i < n; i++ ) { for ( size_t j = 0; j < n; j++ ) a[i][j] = i * n + j; }` – TriposG Aug 23 '19 at 11:13
  • The reason is I need the iterators as integers in order to satisfy the (some condition) that I specified above. – TriposG Aug 23 '19 at 11:14
  • 1
    @TriposG No problem. Use objects of the type int instead of the type size_t. – Vlad from Moscow Aug 23 '19 at 11:24
  • When I run the above code it is giving me `warning: Array access results in a null pointer dereference` Kindly note that I have changed the a[i][j] = i * n + j; to a[i][j] = 1; – TriposG Aug 23 '19 at 13:17
  • 1
    @TriposG Ask a new question showing your current code. – Vlad from Moscow Aug 23 '19 at 13:22
1
numberOfConstPiArray[countKernel][col]

is getting an int* from numberOfConstPiArray[countKernel], then trying to dereference col'th element of this int*, and fails, as numberOfConstPiArray[countKernel] was not initialized with an reference to int array memory.

You may use instead:

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

...
        numberOfConstPiArray[countKernel * kernelColumnCount + col] = 1;
Renat
  • 7,718
  • 2
  • 20
  • 34