GRASS 8 Programmer's Manual  8.5.0dev(2025)-c070206eb1
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 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 double *G_alloc_vector(size_t n)
39 {
40  return (double *)G_calloc(n, sizeof(double));
41 }
42 
43 /**
44  * \fn double **G_alloc_matrix (int rows,int cols)
45  *
46  * \brief Matrix memory allocation.
47  *
48  * Allocate a matrix of <b>rows</b> by <b>cols</b> doubles initialized
49  * to zero.
50  *
51  * \param[in] rows number of rows in matrix
52  * \param[in] cols number of columns in matrix
53  * \return double **
54  */
55 double **G_alloc_matrix(int rows, int cols)
56 {
57  double **m;
58  int i;
59 
60  m = (double **)G_calloc(rows, sizeof(double *));
61  m[0] = (double *)G_calloc((size_t)rows * cols, sizeof(double));
62  for (i = 1; i < rows; i++)
63  m[i] = m[i - 1] + cols;
64 
65  return m;
66 }
67 
68 /**
69  * \fn float *G_alloc_fvector (size_t n)
70  *
71  * \brief Floating point vector memory allocation.
72  *
73  * Allocate a vector (array) of <b>n</b> floats initialized to zero.
74  *
75  * \param[in] n size of vector to allocate
76  * \return float *
77  */
78 float *G_alloc_fvector(size_t n)
79 {
80  return (float *)G_calloc(n, sizeof(float));
81 }
82 
83 /**
84  * \fn float **G_alloc_fmatrix (int rows, int cols)
85  *
86  * \brief Floating point matrix memory allocation.
87  *
88  * Allocate a matrix of <b>rows</b> by <b>cols</b> floats initialized
89  * to zero.
90  *
91  * \param[in] rows number of rows in matrix
92  * \param[in] cols number of columns in matrix
93  * \return float **
94  */
95 float **G_alloc_fmatrix(int rows, int cols)
96 {
97  float **m;
98  int i;
99 
100  m = (float **)G_calloc(rows, sizeof(float *));
101  m[0] = (float *)G_calloc((size_t)rows * cols, sizeof(float));
102  for (i = 1; i < rows; i++)
103  m[i] = m[i - 1] + cols;
104 
105  return m;
106 }
107 
108 /**
109  * \fn void G_free_vector (double *v)
110  *
111  * \brief Vector memory deallocation.
112  *
113  * Deallocate a vector (array) of doubles.
114  *
115  * \param[in,out] v vector to free
116  * \return void
117  */
118 void G_free_vector(double *v)
119 {
120  G_free(v);
121  v = NULL;
122 
123  return;
124 }
125 
126 /**
127  * \fn void G_free_fvector (float *v)
128  *
129  * \brief Vector memory deallocation.
130  *
131  * Deallocate a vector (array) of floats.
132  *
133  * \param[in,out] v vector to free
134  * \return void
135  */
136 void G_free_fvector(float *v)
137 {
138  G_free(v);
139  v = NULL;
140 
141  return;
142 }
143 
144 /**
145  * \fn void G_free_matrix (double **m)
146  *
147  * \brief Matrix memory deallocation.
148  *
149  * Deallocate a matrix of doubles.
150  *
151  * \param[in,out] m matrix to free
152  * \return void
153  */
154 void G_free_matrix(double **m)
155 {
156  G_free(m[0]);
157  G_free(m);
158  m = NULL;
159 
160  return;
161 }
162 
163 /**
164  * \fn void G_free_fmatrix (float **m)
165  *
166  * \brief Floating point matrix memory deallocation.
167  *
168  * Deallocate a matrix of floats.
169  *
170  * \param[in,out] m matrix to free
171  * \return void
172  */
173 void G_free_fmatrix(float **m)
174 {
175  G_free(m[0]);
176  G_free(m);
177  m = NULL;
178 
179  return;
180 }
#define NULL
Definition: ccmath.h:32
void G_free_fmatrix(float **m)
Floating point matrix memory deallocation.
Definition: dalloc.c:173
double ** G_alloc_matrix(int rows, int cols)
Matrix memory allocation.
Definition: dalloc.c:55
void G_free_vector(double *v)
Vector memory deallocation.
Definition: dalloc.c:118
double * G_alloc_vector(size_t n)
Vector matrix memory allocation.
Definition: dalloc.c:38
float ** G_alloc_fmatrix(int rows, int cols)
Floating point matrix memory allocation.
Definition: dalloc.c:95
void G_free_fvector(float *v)
Vector memory deallocation.
Definition: dalloc.c:136
void G_free_matrix(double **m)
Matrix memory deallocation.
Definition: dalloc.c:154
float * G_alloc_fvector(size_t n)
Floating point vector memory allocation.
Definition: dalloc.c:78
void G_free(void *)
Free allocated memory.
Definition: gis/alloc.c:147
#define G_calloc(m, n)
Definition: defs/gis.h:95