Thursday, September 1, 2011

The Dynamic Multidimensional Arrays Library

The Dynamic Multidimensional Arrays Library provides functions for the allocation and deallocation of arrays, matrices, cubes and hypercubes.

In order to pass any kind of data type to the functions, we shall pass the array to the functions using a void pointer. Also, for each allocation function, the size of an array's element must be passed.

The prototypes for the functions are:
/*
 * Abstract:
 *  Allocates a 1D array
 * Parameters:
 *  vector - a pointer to the 1D array
 *  xsize - the size of the array
 *  elementSize - the size of one element
 * Returns:
 *  A pointer to the allocated array
 */
void* NAlloc1D(void* vector, size_t xsize, size_t elementSize);
/*
 * Abstract:
 *  Allocates a 2D array
 * Parameters:
 *  vector - a pointer to the 2D array
 *  xsize - the x size of the array (real of rows)
 *  ysize - the y size of the array (real of columns)
 *  elementSize - the size of one element
 * Returns:
 *  A pointer to the allocated array
 */
void** NAlloc2D(void **vector, size_t xsize, size_t ysize, size_t elementSize);
/*
 * Abstract:
 *  Allocates a 3D array
 * Parameters:
 *  vector - a pointer to the 3D array
 *  xsize - the x size of the array (real of rows)
 *  ysize - the y size of the array (real of columns)
 *  zsize - the z size of the array (real of tables)
 *  elementSize - the size of an array element
 * Returns:
 *  A pointer to the allocated array
 */
void*** NAlloc3D(void ***vector, size_t xsize, size_t ysize, size_t zsize,
        size_t elementSize);
/*
 * Abstract:
 *  Allocates a 4D array
 * Parameters:
 *  vector - a pointer to the 4D array
 *  xsize - the x size of the array (real of rows)
 *  ysize - the y size of the array (real of columns)
 *  zsize - the z size of the array (real of tables)
 *  tsize - the t size of the array
 *  elementSize - the size of an array element
 * Returns:
 *  NULL
 */
void**** NAlloc4D(void ****vector, size_t xsize, size_t ysize, size_t zsize,
        size_t tsize, size_t elementSize);

/*Deallocation functions*/
/*
 * Abstract:
 *  Deallocates a 1D array
 * Parameters:
 *  vector - a pointer to the 1D array
 * Return:
 *  NULL
 */
void* NFree1D(void *vector);
/*
 * Abstract:
 *  Deallocates a 2D array
 * Parameters:
 *  vector - a pointer to the 2D array
 *  xsize - the x size of the array (real of rows)
 * Returns:
 *  NULL
 */
void** NFree2D(void **vector, size_t xSize);
/*
 * Abstract:
 *  Deallocates a 3D array
 * Parameters:
 *  vector - a pointer to the 3D array
 *  xsize - the x size of the array (real of rows)
 *  ysize - the y size of the array (real of columns)
 * Returns:
 *  NULL
 */
void*** NFree3D(void ***vector, size_t xsize, size_t ysize);
/*
 * Abstract:
 *  Deallocates a 4D array
 * Parameters:
 *  vector - a pointer to the 3D array
 *  xsize - the x size of the array (real of rows)
 *  ysize - the y size of the array (real of columns)
 *  zsize - the z size of the array (real of tables
 * Returns:
 *  NULL
 */
void**** NFree4D(void ****vector, size_t xsize, size_t ysize, size_t zsize);

5.Example
#include<stdio.h>
#include"dmda.h"

int main()
{
    int** array = NULL;
    int i,j;
    /*Creating a 10x10 2-dimensional numeric array*/
    array = (int**)NAlloc2D((int**)array,10,10,sizeof(int));
    /*Initializing the array with values*/
    for(i=0; i<10; i++)
    {
        for(j=0; j<10; j++)
        {
            array[i][j] = i-j;
        }
    }
    /*Displaying the 2-dimensional array*/
    for(i=0; i< 10; i++)
    {
        for(j=0; j<10; j++)
        {
            printf("%+d ",array[i][j]);
        }
        putchar('\n');
    }
    /*Freeing the 2-dimensional array*/
    array = (int**)NFree2D((int**)array,10);
    puts("Memory freed");
    return 0;
}
/*Output
+0 -1 -2 -3 -4 -5 -6 -7 -8 -9
+1 +0 -1 -2 -3 -4 -5 -6 -7 -8
+2 +1 +0 -1 -2 -3 -4 -5 -6 -7
+3 +2 +1 +0 -1 -2 -3 -4 -5 -6
+4 +3 +2 +1 +0 -1 -2 -3 -4 -5
+5 +4 +3 +2 +1 +0 -1 -2 -3 -4
+6 +5 +4 +3 +2 +1 +0 -1 -2 -3
+7 +6 +5 +4 +3 +2 +1 +0 -1 -2
+8 +7 +6 +5 +4 +3 +2 +1 +0 -1
+9 +8 +7 +6 +5 +4 +3 +2 +1 +0
Memory freed
 */

No comments:

Post a Comment

Got a question regarding something in the article? Leave me a comment and I will get back at you as soon as I can!

Related Posts Plugin for WordPress, Blogger...
Recommended Post Slide Out For Blogger