GRASS GIS 8 Programmer's Manual  8.2.2dev(2023)-3d2c704037
dalloc.c
Go to the documentation of this file.
1 
2 /**
3  * \file dalloc.c
4  *
5  * \brief Matrix memory management functions.
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or (at
10  * your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  *
21  * \author GRASS GIS Development Team
22  *
23  * \date 2004-2006
24  */
25 
26 #include <stdlib.h>
27 #include <grass/gis.h>
28 
29 
30 /**
31  * \fn double *G_alloc_vector (size_t n)
32  *
33  * \brief Vector matrix memory allocation.
34  *
35  * Allocate a vector (array) of <b>n</b> doubles initialized to zero.
36  *
37  * \param[in] n size of vector to allocate
38  * \return double *
39  */
40 
41 double *G_alloc_vector(size_t n)
42 {
43  return (double *)G_calloc(n, sizeof(double));
44 }
45 
46 
47 /**
48  * \fn double **G_alloc_matrix (int rows,int cols)
49  *
50  * \brief Matrix memory allocation.
51  *
52  * Allocate a matrix of <b>rows</b> by <b>cols</b> doubles initialized
53  * to zero.
54  *
55  * \param[in] rows number of rows in matrix
56  * \param[in] cols number of columns in matrix
57  * \return double **
58  */
59 
60 double **G_alloc_matrix(int rows, int cols)
61 {
62  double **m;
63  int i;
64 
65  m = (double **)G_calloc(rows, sizeof(double *));
66  m[0] = (double *)G_calloc((size_t) rows * cols, sizeof(double));
67  for (i = 1; i < rows; i++)
68  m[i] = m[i - 1] + cols;
69 
70  return m;
71 }
72 
73 
74 /**
75  * \fn float *G_alloc_fvector (size_t n)
76  *
77  * \brief Floating point vector memory allocation.
78  *
79  * Allocate a vector (array) of <b>n</b> floats initialized to zero.
80  *
81  * \param[in] n size of vector to allocate
82  * \return float *
83  */
84 
85 float *G_alloc_fvector(size_t n)
86 {
87  return (float *)G_calloc(n, sizeof(float));
88 }
89 
90 
91 /**
92  * \fn float **G_alloc_fmatrix (int rows, int cols)
93  *
94  * \brief Floating point matrix memory allocation.
95  *
96  * Allocate a matrix of <b>rows</b> by <b>cols</b> floats initialized
97  * to zero.
98  *
99  * \param[in] rows number of rows in matrix
100  * \param[in] cols number of columns in matrix
101  * \return float **
102  */
103 
104 float **G_alloc_fmatrix(int rows, int cols)
105 {
106  float **m;
107  int i;
108 
109  m = (float **)G_calloc(rows, sizeof(float *));
110  m[0] = (float *)G_calloc((size_t) rows * cols, sizeof(float));
111  for (i = 1; i < rows; i++)
112  m[i] = m[i - 1] + cols;
113 
114  return m;
115 }
116 
117 
118 /**
119  * \fn void G_free_vector (double *v)
120  *
121  * \brief Vector memory deallocation.
122  *
123  * Deallocate a vector (array) of doubles.
124  *
125  * \param[in,out] v vector to free
126  * \return void
127  */
128 
129 void G_free_vector(double *v)
130 {
131  G_free(v);
132  v = NULL;
133 
134  return;
135 }
136 
137 
138 /**
139  * \fn void G_free_fvector (float *v)
140  *
141  * \brief Vector memory deallocation.
142  *
143  * Deallocate a vector (array) of floats.
144  *
145  * \param[in,out] v vector to free
146  * \return void
147  */
148 
149 void G_free_fvector(float *v)
150 {
151  G_free(v);
152  v = NULL;
153 
154  return;
155 }
156 
157 
158 /**
159  * \fn void G_free_matrix (double **m)
160  *
161  * \brief Matrix memory deallocation.
162  *
163  * Deallocate a matrix of doubles.
164  *
165  * \param[in,out] m matrix to free
166  * \return void
167  */
168 
169 void G_free_matrix(double **m)
170 {
171  G_free(m[0]);
172  G_free(m);
173  m = NULL;
174 
175  return;
176 }
177 
178 
179 /**
180  * \fn void G_free_fmatrix (float **m)
181  *
182  * \brief Floating point matrix memory deallocation.
183  *
184  * Deallocate a matrix of floats.
185  *
186  * \param[in,out] m matrix to free
187  * \return void
188  */
189 
190 void G_free_fmatrix(float **m)
191 {
192  G_free(m[0]);
193  G_free(m);
194  m = NULL;
195 
196  return;
197 }
float ** G_alloc_fmatrix(int rows, int cols)
Floating point matrix memory allocation.
Definition: dalloc.c:104
void G_free(void *)
Free allocated memory.
Definition: gis/alloc.c:149
#define NULL
Definition: ccmath.h:32
#define G_calloc(m, n)
Definition: defs/gis.h:113
double * G_alloc_vector(size_t n)
Vector matrix memory allocation.
Definition: dalloc.c:41
void G_free_fmatrix(float **m)
Floating point matrix memory deallocation.
Definition: dalloc.c:190
void G_free_vector(double *v)
Vector memory deallocation.
Definition: dalloc.c:129
void G_free_matrix(double **m)
Matrix memory deallocation.
Definition: dalloc.c:169
void G_free_fvector(float *v)
Vector memory deallocation.
Definition: dalloc.c:149
double ** G_alloc_matrix(int rows, int cols)
Matrix memory allocation.
Definition: dalloc.c:60
float * G_alloc_fvector(size_t n)
Floating point vector memory allocation.
Definition: dalloc.c:85