The outage for Sunday 24th November has been cancelled.
Bioplib
Protein Structure C Library
 All Data Structures Files Functions Variables Typedefs Macros Pages
array3.c
Go to the documentation of this file.
1 /************************************************************************/
2 /**
3 
4  \file array3.c
5 
6  \version V1.1
7  \date 07.07.14
8  \brief Allocate and free 3D arrays
9 
10  \copyright (c) Dr. Andrew C. R. Martin, University of Reading, 2002-14
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 3D 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][10];
44 
45  which, when passed to a fuunction, 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 ***)Array3D(sizeof(TYPE), nrows, ncolumns, nplanes);
57 
58 \code
59  matrix = (float **)Array3D(sizeof(float), 10, 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 30.05.02 Original
69 - V1.1 07.07.14 Include array.h. Remove FreeArray3D() prototype.
70  Use bl prefix for functions By: CTP
71 
72 *************************************************************************/
73 /* Doxygen
74  -------
75  #GROUP General Programming
76  #SUBGROUP Array handling
77 
78  #FUNCTION blArray3D()
79  Create a 3D array of elements of size `size' with dimensions `dim1'
80  rows by `dim2' columns by `dim3' planes
81 
82  #FUNCTION blFreeArray3D()
83  Frees a 3D array with dimensions `dim1' rows by `dim2' columns by
84  `dim3' planes.
85 */
86 /************************************************************************/
87 /* Includes
88 */
89 #include <stdlib.h>
90 #include "array.h"
91 
92 /************************************************************************/
93 /* Defines and macros
94 */
95 #ifndef NULL
96 #define NULL ((void *)0)
97 #endif
98 
99 
100 /************************************************************************/
101 /* Globals
102 */
103 
104 /************************************************************************/
105 /* Prototypes
106 */
107 
108 
109 /************************************************************************/
110 /*>char ***blArray3D(int size, int dim1, int dim2, int dim3)
111  ---------------------------------------------------------
112 *//**
113 
114  \param[in] size Size of an array element
115  \param[in] dim1 First dimension (number of rows)
116  \param[in] dim2 Second dimension (number of columns)
117  \param[in] dim3 Third dimension (number of planes)
118  \return Array of pointers. Must be cast to required
119  type
120 
121  Create a 3D array of elements of size `size' with dimensions `dim1'
122  rows by `dim2' columns by `dim3' planes
123 
124 - 30.05.02 Original
125 - 07.07.14 Use bl prefix for functions By: CTP
126 */
127 char ***blArray3D(int size, int dim1, int dim2, int dim3)
128 {
129  char ***array = NULL;
130  int i, j;
131 
132  /* Allocate memory for the outer dimension array */
133  if((array = (char ***)malloc(dim1 * sizeof(char **))) == NULL)
134  return(NULL);
135 
136  /* Set all positions to NULL */
137  for(i=0; i<dim1; i++) array[i] = NULL;
138 
139  /* Allocate memory for each array in the second dimension */
140  for(i=0; i<dim1; i++)
141  {
142  /* If allocation fails, jump to badexit */
143  if((array[i] = (char **)malloc(dim2 * sizeof(char *))) == NULL)
144  goto badexit;
145 
146  /* Set all positions to NULL */
147  for(j=0; j<dim2; j++) array[i][j] = NULL;
148 
149  /* Allocate memory for each array in the third dimension */
150  for(j=0; j<dim2; j++)
151  {
152  /* If allocation fails, jump to badexit */
153  if((array[i][j] = (char *)malloc(dim3 * size)) == NULL)
154  goto badexit;
155  }
156  }
157 
158  return(array);
159 
160 badexit:
161  blFreeArray3D(array, dim1, dim2, dim3);
162 
163  return(NULL);
164 }
165 
166 /************************************************************************/
167 /*>void blFreeArray3D(char ***array, int dim1, int dim2, int dim3)
168  ---------------------------------------------------------------
169 *//**
170 
171  \param[in] array Array of pointers to be freed
172  \param[in] dim1 First dimension (number of rows)
173  \param[in] dim2 Second dimension (number of columns)
174  \param[in] dim3 Third dimension (number of planes)
175 
176  Frees a 3D array with dimensions `dim1' rows by `dim2' columns by
177  `dim3' planes.
178 
179 - 30.05.02 Original
180 - 07.07.14 Use bl prefix for functions By: CTP
181 */
182 void blFreeArray3D(char ***array, int dim1, int dim2, int dim3)
183 {
184  int i, j;
185 
186  if(array!=NULL)
187  {
188  for(i=0; i<dim1; i++)
189  {
190  if(array[i] != NULL)
191  {
192  for(j=0; j<dim2; j++)
193  {
194  if(array[i][j] != NULL)
195  free(array[i][j]);
196  }
197  free(array[i]);
198  }
199  }
200 
201  free(array);
202  }
203 }
#define NULL
Definition: array3.c:96
void blFreeArray3D(char ***array, int dim1, int dim2, int dim3)
Definition: array3.c:182
char *** blArray3D(int size, int dim1, int dim2, int dim3)
Definition: array3.c:127
Include file for 2D/3D array functions.