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