Saturday, August 11, 2012

Creating a Dynamic Matrix in ANSI C

The memory of a dynamic matrix can be allocated at run time as opposite to the static matrices (who are allocated at compile-time).

The allocation algorithm is based on the function void* malloc(size_t size). The first thing that must be done is to allocate the space for the entire matrix. After that, each row from the matrix must be allocated separately.
/*
 * Description:
 *  Allocates dinamically memory for a rows x columns matrix
 * Parameters:
 *  matrix  - a pointer to the matrix which will be allocated
 *  rows    - the number of rows
 *  columns - the number of columns
 * Returns:
 *  matrix - a pointer to the allocated matrix
 */
int** Matrix_Alloc(int** matrix, int rows, int columns)
{
    int i;
    matrix=malloc(sizeof(int)*rows*columns);
    if(matrix!=NULL)
    {
        /*Allocating space for each row*/
        for(i=0; i<rows;i++)
        {
            matrix[i] = malloc(sizeof(int)*columns);
            if(matrix[i]==NULL)
            {
                return NULL;
            }
        }
    }
    return matrix;
}
The deallocation algorithm is based on the function void free(void* address) and is pretty much the opposite of the allocation algorithm, First the rows of the matrix will be deallocated and then the matrix itself.
/*
 * Description:
 *  Frees a dynamically allocated matrix
 * Parameters:
 *  matrix  - a pointer to an allocated matrix
 *  rows    - the number of rows
 *  columns - the number of columns
 * Returns:
 *  Nothing
 */
void Matrix_Free(int** matrix, int rows)
{
    int i;
    for(i = 0; i < rows; i++)
    {
        free(matrix[i]);
    }
    free(matrix);
}
Here's an example on how to use the functions:
#include<stdio.h>
#include<stdbool.h>
#include<stdlib.h>

int** Matrix_Alloc(int** matrix, int rows, int columns);
void Matrix_Free(int** matrix, int rows);

void Matrix_Print(int** matrix, int rows, int columns)
{
    int i,j;
    for(i=0; i<rows;i++)
    {
        for(j=0; j<columns; j++)
        {
            printf("%d ",matrix[i][j]);
        }
        putchar('\n');
    }
}

int main(void)
{
    int **matrix = NULL;
    int i, j;

    /*Allocating the matrix*/
    matrix = Matrix_Alloc(matrix, 3, 3);

    /*Assigning values to the matrix*/
    for (i = 0; i < 3; i++)
    {
        for (j = 0; j < 3; j++)
        {
            matrix[i][j] = (i+j);
        }
    }

    /*Printing the results*/
    Matrix_Print(matrix,3,3);

    /*Freeing the memory*/
    Matrix_Free(matrix,3);
    puts("Memory freed");

    return 0;
}
/*Output
0 1 2 
1 2 3 
2 3 4 
Memory freed
*/
In the example above we used a primitive data type. If we would had use a structure, each element of the matrix would had to be allocated/freed as well.

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