Home arrow Studies arrow Coding arrow Dynamic Memory Allocation
Multi-Dimensional Dynamic Memory Allocation in C PDF Print

 
In somehow almost everyone has befallen with memory problems when trying to use multi-dimensional arrays, like matrices. I came this solution after the extensive search at internet. Pseudo code of the two-dimensional array is as follows:
 

      1. Take width and height values.
2  Allocate number of "height" double-pointers.

      2. For each line.

      2.1. Allocate one line by the size of width.

      3. Repeat second statement by "height" times.

You can use above technique to allocate three or more dimensional arrays. Let's say we want to allocate three dimensional array, (x, y, z). In this case you should first allocate number of z triple-pointers. Secondly in a FOR loop allocate number of y double-pointers. Finally in another FOR loop allocate lines in size of x.


Allocate A Signed Long Integer (4-bytes) Matrix and Free

/*
    Memory Allocation and Free functions.
                                Written by Chasan Chouse, 2004

    Matrices must be used first height, second width, like  MAT[height][width]
      Sample Code:
            for(i=0;i<height;i++)
                for(j=0;j<width;j++)
                    MAT[i][j] = 5;
 

   Usage of the functions below is really simple:

           //  Allocate
           double **SampleMatrix;
           SampleMatrix = Alloc_2D_Double( 480, 640);

           // Freeing
           Free_2D_Double(SampleMatrix, 480);


*/

long **Alloc_2D_Long(long height, long width)
{
    long **Mat;
    long i;

    if ((Mat = (long **)malloc(height * sizeof(long *))) == NULL)
    {
        DispError("\nERROR! Unable to allocate memory\n");
        exit(0);
    }
    for (i=0;i<height;i++)
    {
        if ((Mat[i] = (long *)malloc( width * sizeof(long) )) == NULL)

        {
            DispError("\nERROR! Unable to allocate memory\n");
            exit(0);
        }
    }

    return Mat;
}

void Free_2D_Long(long **a, long height)
{
    long i;
    /* this is reverse order method*/
    for(i=0;i<height;i++)
    {
        free(a[i]);
    }
    free(a);
}

Allocate A Double Matrix and Free

double **Alloc_2D_Double(long height, long width)
{
    double **Mat;
    long i;

    if ((Mat = (double **)malloc(height * sizeof(double *))) == NULL)
    {
        DispError("\nERROR! Unable to allocate memory\n");
        exit(0);
    }
    for (i=0;i<height;i++)
    {
        if ((Mat[i] = (double *)malloc( width * sizeof(double) )) == NULL)

        {
            DispError("\nERROR! Unable to allocate memory\n");
            exit(0);
        }
    }

    return Mat;
}

void
 Free_2D_Double(double **a, long height)
{
    long i;
    /* this is reverse order method*/
    for(i=0;i<height;i++)
    {
        free(a[i]);
    }
    free(a);
}
 

Allocate A UCHAR (1-byte) Matrix and Free


#define UCHAR unsigned char

UCHAR **Alloc_2D_UCHAR(long height, long width)
{
    UCHAR **Mat;
    long i;

    if ((Mat = (UCHAR **)malloc(height * sizeof(UCHAR *))) == NULL)
    {
        DispError("\nERROR! Unable to allocate memory\n");
        exit(0);
    }
    for (i=0;i<height;i++)
    {
        if ((Mat[i] = (UCHAR *)malloc( width * sizeof(UCHAR) )) == NULL)
        {
            DispError("\nERROR! Unable to allocate memory\n");
            exit(0);
        }
    }

    return Mat;
}

void
 Free_2D_UCHAR(UCHAR **a, long height)
{
    long i;
    /* this is reverse order method*/
    for(i=0;i<height;i++)
    {
        free(a[i]);
    }
    free(a);
}
 

Allocate A Structure Matrix and Free

typedef struct {
    long x;
    long y;
} BlockInfoStrc;

BlockInfoStrc **Alloc_2D_BlockInfoStrc(long height, long width)
{
    BlockInfoStrc **Mat;
    long i;

    if ((Mat = (BlockInfoStrc **)malloc(height * sizeof(BlockInfoStrc *))) == NULL)
    {
        DispError("\nERROR! Unable to allocate memory\n");
        exit(0);
    }
    for (i=0;i<height;i++)
    {
        if ((Mat[i] = (BlockInfoStrc *)malloc( width * sizeof(BlockInfoStrc) )) == NULL)
        {
            DispError("\nERROR! Unable to allocate memory\n");
            exit(0);
        }
    }

    return Mat;
}

void Free_2D_BlockInfoStrc(BlockInfoStrc **a, long height)
{
    long i;
    /* this is reverse order method*/
    for(i=0;i<height;i++)
    {
        free(a[i]);
    }
    free(a);
}
 

 

Copyright 2003-2007 by Chasan Chouse.

Locations of visitors to this page