Bioplib
Protein Structure C Library
 All Data Structures Files Functions Variables Typedefs Macros Pages
array2.c
Go to the documentation of this file.
1 /************************************************************************/
2 /**
3 
4  \file array2.c
5 
6  \version V1.5
7  \date 07/07.14
8  \brief Allocate and free 2D arrays
9 
10  \copyright (c) UCL / Dr. Andrew C. R. Martin 1993-2014
11  \author Dr. Andrew C. R. Martin
12  \par
13  Institute of Structural & Molecular Biology,
14  University College London,
15  Gower Street,
16  London.
17  WC1E 6BT.
18  \par
19  andrew@bioinf.org.uk
20  andrew.martin@ucl.ac.uk
21 
22 **************************************************************************
23 
24  This code is NOT IN THE PUBLIC DOMAIN, but it may be copied
25  according to the conditions laid out in the accompanying file
26  COPYING.DOC.
27 
28  The code may be modified as required, but any modifications must be
29  documented so that the person responsible can be identified.
30 
31  The code may not be sold commercially or included as part of a
32  commercial product except as described in the file COPYING.DOC.
33 
34 **************************************************************************
35 
36  Description:
37  ============
38 
39  Creates a 2D array where the first dimension is a set of pointers. This
40  is better for passing into subroutines than the conventional C method
41  of simply declaring:
42 
43  TYPE matrix[10][10];
44 
45  which, when passed to a function, loses the concept of dimensions
46  unless the matrix is explicitly defined with these dimension in the
47  function.
48 
49  This routine creates an array of pointers to 1-D arrays and can thus be
50  passed to functions successfully.
51 
52 **************************************************************************
53 
54  Usage:
55  ======
56  matrix = (TYPE **)Array2D(sizeof(TYPE), nrows, ncolumns);
57 
58 \code
59  matrix = (float **)Array2D(sizeof(float), 10, 10);
60 \endcode
61 
62  Returns NULL (having freed any allocated memory) if there is a problem.
63 
64 **************************************************************************
65 
66  Revision History:
67  =================
68 - V1.0 07.10.92 Original
69 - V1.1 29.01.93 Added includes of sysdefs.h & malloc.h for MS-DOS
70 - V1.2 16.06.93 Includes stdlib.h rather than malloc.h
71 - V1.3 01.03.94 Corrected other include file usage
72 - V1.4 18.03.94 Added NULL definition for systems which don't define
73  it in stdlib.h
74 - V1.5 07.07.14 Include array.h Use bl prefix for functions By: CTP
75 
76 
77 *************************************************************************/
78 /* Doxygen
79  -------
80  #GROUP General Programming
81  #SUBGROUP Array handling
82 
83  #FUNCTION blArray2D()
84  Create a 2D array of elements of size `size' with dimensions `dim1'
85  rows by `dim2' columns.
86 
87  #FUNCTION blFreeArray2D()
88  Frees a 2D array with dimensions `dim1' rows by `dim2' columns.
89 */
90 /************************************************************************/
91 /* Includes
92 */
93 #include <stdlib.h>
94 
95 /************************************************************************/
96 /* Defines and macros
97 */
98 #ifndef NULL
99 #define NULL ((void *)0)
100 #endif
101 
102 
103 /************************************************************************/
104 /* Globals
105 */
106 
107 /************************************************************************/
108 /* Prototypes
109 */
110 
111 /************************************************************************/
112 /*>char **blArray2D(int size, int dim1, int dim2)
113  --------------------------------------------
114 *//**
115 
116  \param[in] size Size of an array element
117  \param[in] dim1 First dimension (number of rows)
118  \param[in] dim2 Second dimension (number of columns)
119  \return Array of pointers. Must be cast to required
120  type
121 
122  Create a 2D array of elements of size `size' with dimensions `dim1'
123  rows by `dim2' columns.
124 
125 - 07.10.92 Original
126 - 12.07.93 Tidied and commented
127 - 07.07.14 Use bl prefix for functions By: CTP
128 
129 */
130 char **blArray2D(int size,
131  int dim1,
132  int dim2)
133 {
134  char **array = NULL;
135  int i;
136 
137  /* Allocate memory for the outer dimension array */
138  if((array = (char **)malloc(dim1 * sizeof(char *))) == NULL)
139  return(NULL);
140 
141  /* Set all positions to NULL */
142  for(i=0; i<dim1; i++) array[i] = NULL;
143 
144  /* Allocate memory for each array in the second dimension */
145  for(i=0; i<dim1; i++)
146  {
147  /* If allocation fails, jump to badexit */
148  if((array[i] = (char *)malloc(dim2 * size)) == NULL)
149  goto badexit;
150  }
151 
152  return(array);
153 
154 badexit:
155  for(i=0; i<dim1; i++) if(array[i]) free(array[i]);
156  free(array);
157  return(NULL);
158 }
159 
160 /************************************************************************/
161 /*>void blFreeArray2D(char **array, int dim1, int dim2)
162  ----------------------------------------------------
163 *//**
164 
165  \param[in] array Array of pointers to be freed
166  \param[in] dim1 First dimension (number of rows)
167  \param[in] dim2 Second dimension (number of columns)
168 
169  Frees a 2D array with dimensions `dim1' rows by `dim2' columns.
170 
171 - 07.10.92 Original
172 - 07.07.14 Use bl prefix for functions By: CTP
173 */
174 void blFreeArray2D(char **array,
175  int dim1,
176  int dim2)
177 {
178  int i;
179 
180  if(array)
181  {
182  for(i=0; i<dim1; i++) if(array[i]) free(array[i]);
183  free(array);
184  }
185 }
#define NULL
Definition: array2.c:99
void blFreeArray2D(char **array, int dim1, int dim2)
Definition: array2.c:174
char ** blArray2D(int size, int dim1, int dim2)
Definition: array2.c:130