Pages

Wednesday, November 16, 2011

Dynamic Two-dimensioned Arrays in C

Extending the concept discussed in dynamic one-dimensioned arrays to two-dimensioned arrays.Let us now take the concept discussed in Static and Dynamic One-dimensioned Arrays in C forward. Here is the outline of what we intend to do:

Define 'p', a pointer to pointer to float, as the name of the 2D array.

To pointer 'p', let us allocate an array of 'm' elements of type 'pointer to float', Here, 'm' is the number of rows we wish the 2D array to have.

To each element p[i] allocated above, let us allocate an array of 'n' elements of type 'float'. Each such block will store one full row of the 2D array. Here, 'n' is the number of columns in each row of the array.

This is illustrated in the diagram below:

Note the following points:

Memory allocated to the rows of the array may not be contiguous. This deviates from the way memory is allocated to a static array in C. In a future iteration of this post, I intend to discuss how to remedy this shortcoming.

This dynamic 2D array requires more memory as compared to a static 2D array. The memory allocated to the rows (sizeof(float *) * m) and the memory allocated to the pointer 'p' itself are extra, as compared to a static 2D array in C.

Row and column indexing still starts with zero. I will discuss how to remedy this in a future post, but it follows the same concept that is discussed in the previous post C/C++ Arrays with Arbitrary Start Index.

The sequence of memory allocation is, allocate memory to pointer to row pointers first and then to the row pointers. While deallocating memory, deallocate memory allocated to the row pointers first, and then deallocate memory allocated to pointer to row pointers.

It would be possible to allocate a different number of columns to different rows, but it would deviate from the traditional definition of an array. However, this would be a good way to define sparse matrices (which contains only a few elements that are non-zero and remain non-zero during all subsequent operations on them).

Let us illustrate this with a C program:

#include <stdio.h>

#include <stdlib.h>

int main() {

float **p;

int m, n, i, j;

printf("Enter number of rows and columns: ");

scanf("%d%d", &m, &n);

/* Allocate memory */

p = (float *) malloc(sizeof(float *) * m); /* Row pointers */

for(i = 0; i < m; i++) {

p[i] = (float) malloc(sizeof(float) * n); /* Rows */

}

/* Assign values to array elements and print them */

for(i = 0; i < m; i++) {

for(j = 0; j < n; j++) {

p[i][j] = (i * 10) + (j + 1);

printf("%6.2f ", p[i][j]);

}

printf("\n");

}

/* Deallocate memory */

for(i = 0; i < m; i++) {

free(p[i]); /* Rows */

}

free(p); /* Row pointers */

}

I have dispensed with error checking only for the purpose of brevity, however, it is a bad practice.

Subscribe Now

FeedBurner FeedCount

About this Blog

This blog is a collection of random thoughts about teaching, numerical and scientific computation and programming.

It will be of interest mainly to students of structural engineering and novice and veteran structural engineers. It may also interest other engineers and anyone beginning to learn programming languages for numeric and scientific computations.