Click here to Skip to main content
15,867,568 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My knowledge in C is poor, most of my programming experience is in managed code, I know that my question may be simple, but I am struggle in solving it.

right now I am using a code that is already written in C, the code snippet that I am trying to modify is for allocating memory for a 2D matrix, I want to make it for 3D matrix.

int** A;
long L;

A = imatrix(0, L - 1, 0, L - 1);

int** imatrix(long nrl, long nrh, long ncl, long nch)
/* allocate a int matrix with subscript range m[nrl..nrh][ncl..nch] */
{
long i, nrow = nrh - nrl + 1, ncol = nch - ncl + 1;
int** m;

/* allocate pointers to rows */
m = (int**)malloc((size_t)((nrow + NR_END) * sizeof(int*)));
if (!m) nrerror("allocation failure 1 in matrix()");
m += NR_END;
m -= nrl;


/* allocate rows and set pointers to them */
m[nrl] = (int*)malloc((size_t)((nrow * ncol + NR_END) * sizeof(int)));
if (!m[nrl]) nrerror("allocation failure 2 in matrix()");
m[nrl] += NR_END;
m[nrl] -= ncl;

for (i = nrl + 1; i <= nrh; i++) m[i] = m[i - 1] + ncol;

/* return pointer to array of pointers to rows */
return m;
}


what I have tried reflect the fact that I don't understand the code that I am using, but I am trying.

I need some direction and explanation

What I have tried:

int*** A;
long L;



int*** imatrix(long nrl, long nrh, long ncl, long nch, long ndl, long ndh)
/* allocate a int matrix with subscript range m[nrl..nrh][ncl..nch] */
{
long i, nrow = nrh - nrl + 1, ncol = nch - ncl + 1, nd = ndh - ndl + 1;
int*** m;

/* allocate pointers to rows */
m = (int***)malloc((size_t)((nrow + NR_END) * sizeof(int**)));
if (!m) nrerror("allocation failure 1 in matrix()");
m += NR_END;
m -= nrl;


/* allocate rows and set pointers to them */
m[nrl] = (int*)malloc((size_t)((nrow * ncol + NR_END) * sizeof(int)));
if (!m[nrl]) nrerror("allocation failure 2 in matrix()");
m[nrl] += NR_END;
m[nrl] -= ncl;

for (i = nrl + 1; i <= nrh; i++) m[i] = m[i - 1] + ncol;

/* return pointer to array of pointers to rows */
return m;
}
Posted
Updated 25-Nov-22 16:23pm

1 solution

Here is one way to do it. I use a macro for the memory allocation to improve readability. I also defined a couple of pointer types for the same reason. Here's the code :
C
/* allocate memory of a given type - must use free to release it */

#define AllocateMemory( count, type )   (type*)calloc( count, sizeof( type ) )

typedef int *   PINT;
typedef PINT *  PPINT;

/* allocate a matrix of integers */

int*** AllocateIntMatrix( int nrows, int ncols, int ndeep )
{
    int count = 0;
    int i, j, k;
    int *** m;

    m = AllocateMemory( nrows, PPINT );

    for( i = 0; i < nrows; ++i )
    {
        m[ i ] = AllocateMemory( ncols, PINT );

        for( j = 0; j < ncols; ++j )
        {
            m[ i ][ j ] = AllocateMemory( ndeep, int );
        }
    }

    // it is already initialized to zero by calloc but set it as a test

    for( i = 0; i < nrows; ++i )
    {
        for( j = 0; j < ncols; ++j )
        {
            for( k = 0; k < ndeep; ++k )
            {
                m[ i ][ j ][ k ] = count;
                ++count;
            }
        }
    }

    return m;
}
Here's code to release the matrix since all well-behaved programs clean up after themselves.
C
void ReleaseIntMatrix( int *** im, int nrows, int ncols )
{
    int i, j;
    for( i = 0; i < nrows; ++i )
    {
        for( j = 0; j < ncols; ++j )
        {
            free( m[ i ][ j ] );
        }

        free( m[ i ] );
    }

    free( m );
};
Note that the functions should allocate the memory they are told to and nothing else. All of those calculations you have should be outside because otherwise the release function would need to calculate them also and it is very bad practice to duplicate code like that. It would be better to have a function to calculate those things. In addition, if you have weird rules for those subscripts, and not starting from zero qualifies as weird, then I recommend that you implement an accessing function analogous to a Get method.
 
Share this answer
 
v3
Comments
Sabry1905 29-Nov-22 11:41am    
Thank you so much

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900