Merge remote-tracking branch 'origin/release-v4.6.1'
[WRF.git] / external / io_grib1 / grib1_util / alloc_2d.c
blob5f0c6406f4024af0d9e79a38d8b1665b914b7255
1 #include "stdio.h"
2 #include <stdlib.h>
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
14 ***************************************************************************/
15 float **alloc_float_2d(int firstdim,int seconddim)
17 float **outvar;
18 int row, row2;
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]);
28 free(outvar);
29 return NULL;
32 return outvar;
35 /**************************************************************************
36 * free_float_2d - frees memory held by a two dimensional array
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
45 ***************************************************************************/
47 void free_float_2d(float **var, int firstdim, int seconddim)
49 int row;
50 for (row=0; row < firstdim; row++) {
51 free(var[row]);
53 free(var);