| 1 | #include "stdio.h" |
|---|
| 2 | #include <stdlib.h> |
|---|
| 3 | |
|---|
| 4 | /************************************************************************** |
|---|
| 5 | * alloc_float_2d - dynamically allocates a two dimensional array |
|---|
| 6 | * |
|---|
| 7 | * Input: |
|---|
| 8 | * firsdim - the number of elements in the first dimension of the array |
|---|
| 9 | * seconddim - the number of elements in the second dimension of the array |
|---|
| 10 | * Return: |
|---|
| 11 | * On success, a newly allocated two-dimensional array |
|---|
| 12 | * On failure, NULL |
|---|
| 13 | * |
|---|
| 14 | ***************************************************************************/ |
|---|
| 15 | float **alloc_float_2d(int firstdim,int seconddim) |
|---|
| 16 | { |
|---|
| 17 | float **outvar; |
|---|
| 18 | int row, row2; |
|---|
| 19 | |
|---|
| 20 | outvar = (float **)calloc(firstdim,sizeof(float *)); |
|---|
| 21 | if (outvar == NULL) return NULL; |
|---|
| 22 | for (row=0; row < firstdim; row++) { |
|---|
| 23 | outvar[row] = (float *)calloc(seconddim,sizeof(float)); |
|---|
| 24 | if (outvar[row] == NULL) { |
|---|
| 25 | for (row2 = 0; row2 < row; row2++) { |
|---|
| 26 | free(outvar[row]); |
|---|
| 27 | } |
|---|
| 28 | free(outvar); |
|---|
| 29 | return NULL; |
|---|
| 30 | } |
|---|
| 31 | } |
|---|
| 32 | return outvar; |
|---|
| 33 | } |
|---|
| 34 | |
|---|
| 35 | /************************************************************************** |
|---|
| 36 | * free_float_2d - frees memory held by a two dimensional array |
|---|
| 37 | * |
|---|
| 38 | * Input: |
|---|
| 39 | * var - the two-dimensional array to be freed |
|---|
| 40 | * firsdim - the number of elements in the first dimension of the array |
|---|
| 41 | * seconddim - the number of elements in the second dimension of the array |
|---|
| 42 | * Return: |
|---|
| 43 | * None |
|---|
| 44 | * |
|---|
| 45 | ***************************************************************************/ |
|---|
| 46 | |
|---|
| 47 | void free_float_2d(float **var, int firstdim, int seconddim) |
|---|
| 48 | { |
|---|
| 49 | int row; |
|---|
| 50 | for (row=0; row < firstdim; row++) { |
|---|
| 51 | free(var[row]); |
|---|
| 52 | } |
|---|
| 53 | free(var); |
|---|
| 54 | } |
|---|