GRASS GIS 8 Programmer's Manual  8.5.0dev(2024)-602118adcc
ialloc.c
Go to the documentation of this file.
1 /**
2  * \file ialloc.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 int *G_alloc_ivector (size_t n)
30  *
31  * \brief Vector matrix memory allocation.
32  *
33  * Allocate a vector (array) of <b>n</b> integers initialized to zero.
34  *
35  * \param[in] n size of vector to allocate
36  * \return integer *
37  */
38 int *G_alloc_ivector(size_t n)
39 {
40  return (int *)G_calloc(n, sizeof(int));
41 }
42 
43 /**
44  * \fn int **G_alloc_imatrix (int rows, int cols)
45  *
46  * \brief Matrix memory allocation.
47  *
48  * Allocate a matrix of <b>rows</b> by <b>cols</b> integers 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 int **
54  */
55 int **G_alloc_imatrix(int rows, int cols)
56 {
57  int **m;
58  int i;
59 
60  m = (int **)G_calloc(rows, sizeof(int *));
61  m[0] = (int *)G_calloc((size_t)rows * cols, sizeof(int));
62  for (i = 1; i < rows; i++)
63  m[i] = m[i - 1] + cols;
64 
65  return m;
66 }
67 
68 /**
69  * \fn void G_free_ivector(int *v)
70  *
71  * \brief Vector memory deallocation.
72  *
73  * Deallocate a vector (array) of integers.
74  *
75  * \param[in,out] v vector to free
76  * \return void
77  */
78 void G_free_ivector(int *v)
79 {
80  G_free(v);
81  v = NULL;
82 
83  return;
84 }
85 
86 /**
87  * \fn int G_free_imatrix (int **m)
88  *
89  * \brief Matrix memory deallocation.
90  *
91  * Deallocate a matrix of integers.
92  *
93  * \param[in,out] m matrix to free
94  * \return void
95  */
96 void G_free_imatrix(int **m)
97 {
98  G_free(m[0]);
99  G_free(m);
100  m = NULL;
101 
102  return;
103 }
#define NULL
Definition: ccmath.h:32
void G_free(void *)
Free allocated memory.
Definition: gis/alloc.c:150
#define G_calloc(m, n)
Definition: defs/gis.h:95
int ** G_alloc_imatrix(int rows, int cols)
Matrix memory allocation.
Definition: ialloc.c:55
void G_free_imatrix(int **m)
Matrix memory deallocation.
Definition: ialloc.c:96
void G_free_ivector(int *v)
Vector memory deallocation.
Definition: ialloc.c:78
int * G_alloc_ivector(size_t n)
Vector matrix memory allocation.
Definition: ialloc.c:38