Bug Summary

File:programs/Simulation/genpi/nrutil.c
Location:line 182, column 2
Description:Memory is never released; potential leak of memory pointed to by 'm'

Annotated Source Code

1/* CAUTION: This is the ANSI C (only) version of the Numerical Recipes
2 utility file nrutil.c. Do not confuse this file with the same-named
3 file nrutil.c that is supplied in the 'misc' subdirectory.
4 *That* file is the one from the book, and contains both ANSI and
5 traditional K&R versions, along with #ifdef macros to select the
6 correct version. *This* file contains only ANSI C. */
7
8#include <stdio.h>
9#include <stddef.h>
10#include <stdlib.h>
11#define NR_END1 1
12#define FREE_ARGchar* char*
13
14void nrerror(char error_text[])
15/* Numerical Recipes standard error handler */
16{
17 fprintf(stderrstderr,"Numerical Recipes run-time error...\n");
18 fprintf(stderrstderr,"%s\n",error_text);
19 fprintf(stderrstderr,"...now exiting to system...\n");
20 exit(1);
21}
22
23float *vector(long nl, long nh)
24/* allocate a float vector with subscript range v[nl..nh] */
25{
26 float *v;
27
28 v=(float *)malloc((size_t) ((nh-nl+1+NR_END1)*sizeof(float)));
29 if (!v) nrerror("allocation failure in vector()");
30 return v-nl+NR_END1;
31}
32
33int *ivector(long nl, long nh)
34/* allocate an int vector with subscript range v[nl..nh] */
35{
36 int *v;
37
38 v=(int *)malloc((size_t) ((nh-nl+1+NR_END1)*sizeof(int)));
39 if (!v) nrerror("allocation failure in ivector()");
40 return v-nl+NR_END1;
41}
42
43unsigned char *cvector(long nl, long nh)
44/* allocate an unsigned char vector with subscript range v[nl..nh] */
45{
46 unsigned char *v;
47
48 v=(unsigned char *)malloc((size_t) ((nh-nl+1+NR_END1)*sizeof(unsigned char)));
49 if (!v) nrerror("allocation failure in cvector()");
50 return v-nl+NR_END1;
51}
52
53unsigned long *lvector(long nl, long nh)
54/* allocate an unsigned long vector with subscript range v[nl..nh] */
55{
56 unsigned long *v;
57
58 v=(unsigned long *)malloc((size_t) ((nh-nl+1+NR_END1)*sizeof(long)));
59 if (!v) nrerror("allocation failure in lvector()");
60 return v-nl+NR_END1;
61}
62
63double *dvector(long nl, long nh)
64/* allocate a double vector with subscript range v[nl..nh] */
65{
66 double *v;
67
68 v=(double *)malloc((size_t) ((nh-nl+1+NR_END1)*sizeof(double)));
69 if (!v) nrerror("allocation failure in dvector()");
70 return v-nl+NR_END1;
71}
72
73float **matrix(long nrl, long nrh, long ncl, long nch)
74/* allocate a float matrix with subscript range m[nrl..nrh][ncl..nch] */
75{
76 long i, nrow=nrh-nrl+1,ncol=nch-ncl+1;
77 float **m;
78
79 /* allocate pointers to rows */
80 m=(float **) malloc((size_t)((nrow+NR_END1)*sizeof(float*)));
81 if (!m) nrerror("allocation failure 1 in matrix()");
82 m += NR_END1;
83 m -= nrl;
84
85 /* allocate rows and set pointers to them */
86 m[nrl]=(float *) malloc((size_t)((nrow*ncol+NR_END1)*sizeof(float)));
87 if (!m[nrl]) nrerror("allocation failure 2 in matrix()");
88 m[nrl] += NR_END1;
89 m[nrl] -= ncl;
90
91 for(i=nrl+1;i<=nrh;i++) m[i]=m[i-1]+ncol;
92
93 /* return pointer to array of pointers to rows */
94 return m;
95}
96
97double **dmatrix(long nrl, long nrh, long ncl, long nch)
98/* allocate a double matrix with subscript range m[nrl..nrh][ncl..nch] */
99{
100 long i, nrow=nrh-nrl+1,ncol=nch-ncl+1;
101 double **m;
102
103 /* allocate pointers to rows */
104 m=(double **) malloc((size_t)((nrow+NR_END1)*sizeof(double*)));
105 if (!m) nrerror("allocation failure 1 in matrix()");
106 m += NR_END1;
107 m -= nrl;
108
109 /* allocate rows and set pointers to them */
110 m[nrl]=(double *) malloc((size_t)((nrow*ncol+NR_END1)*sizeof(double)));
111 if (!m[nrl]) nrerror("allocation failure 2 in matrix()");
112 m[nrl] += NR_END1;
113 m[nrl] -= ncl;
114
115 for(i=nrl+1;i<=nrh;i++) m[i]=m[i-1]+ncol;
116
117 /* return pointer to array of pointers to rows */
118 return m;
119}
120
121int **imatrix(long nrl, long nrh, long ncl, long nch)
122/* allocate a int matrix with subscript range m[nrl..nrh][ncl..nch] */
123{
124 long i, nrow=nrh-nrl+1,ncol=nch-ncl+1;
125 int **m;
126
127 /* allocate pointers to rows */
128 m=(int **) malloc((size_t)((nrow+NR_END1)*sizeof(int*)));
129 if (!m) nrerror("allocation failure 1 in matrix()");
130 m += NR_END1;
131 m -= nrl;
132
133
134 /* allocate rows and set pointers to them */
135 m[nrl]=(int *) malloc((size_t)((nrow*ncol+NR_END1)*sizeof(int)));
136 if (!m[nrl]) nrerror("allocation failure 2 in matrix()");
137 m[nrl] += NR_END1;
138 m[nrl] -= ncl;
139
140 for(i=nrl+1;i<=nrh;i++) m[i]=m[i-1]+ncol;
141
142 /* return pointer to array of pointers to rows */
143 return m;
144}
145
146float **submatrix(float **a, long oldrl, long oldrh, long oldcl, long oldch,
147 long newrl, long newcl)
148/* point a submatrix [newrl..][newcl..] to a[oldrl..oldrh][oldcl..oldch] */
149{
150 long i,j,nrow=oldrh-oldrl+1,ncol=oldcl-newcl;
151 float **m;
152
153 /* allocate array of pointers to rows */
154 m=(float **) malloc((size_t) ((nrow+NR_END1)*sizeof(float*)));
155 if (!m) nrerror("allocation failure in submatrix()");
156 m += NR_END1;
157 m -= newrl;
158
159 /* set pointers to rows */
160 for(i=oldrl,j=newrl;i<=oldrh;i++,j++) m[j]=a[i]+ncol;
161
162 /* return pointer to array of pointers to rows */
163 return m;
164}
165
166float **convert_matrix(float *a, long nrl, long nrh, long ncl, long nch)
167/* allocate a float matrix m[nrl..nrh][ncl..nch] that points to the matrix
168declared in the standard C manner as a[nrow][ncol], where nrow=nrh-nrl+1
169and ncol=nch-ncl+1. The routine should be called with the address
170&a[0][0] as the first argument. */
171{
172 long i,j,nrow=nrh-nrl+1,ncol=nch-ncl+1;
173 float **m;
174
175 /* allocate pointers to rows */
176 m=(float **) malloc((size_t) ((nrow+NR_END1)*sizeof(float*)));
1
Memory is allocated
177 if (!m) nrerror("allocation failure in convert_matrix()");
2
Assuming 'm' is non-null
3
Taking false branch
178 m += NR_END1;
179 m -= nrl;
180
181 /* set pointers to rows */
182 m[nrl]=a-ncl;
4
Memory is never released; potential leak of memory pointed to by 'm'
183 for(i=1,j=nrl+1;i<nrow;i++,j++) m[j]=m[j-1]+ncol;
184 /* return pointer to array of pointers to rows */
185 return m;
186}
187
188float ***f3tensor(long nrl, long nrh, long ncl, long nch, long ndl, long ndh)
189/* allocate a float 3tensor with range t[nrl..nrh][ncl..nch][ndl..ndh] */
190{
191 long i,j,nrow=nrh-nrl+1,ncol=nch-ncl+1,ndep=ndh-ndl+1;
192 float ***t;
193
194 /* allocate pointers to pointers to rows */
195 t=(float ***) malloc((size_t)((nrow+NR_END1)*sizeof(float**)));
196 if (!t) nrerror("allocation failure 1 in f3tensor()");
197 t += NR_END1;
198 t -= nrl;
199
200 /* allocate pointers to rows and set pointers to them */
201 t[nrl]=(float **) malloc((size_t)((nrow*ncol+NR_END1)*sizeof(float*)));
202 if (!t[nrl]) nrerror("allocation failure 2 in f3tensor()");
203 t[nrl] += NR_END1;
204 t[nrl] -= ncl;
205
206 /* allocate rows and set pointers to them */
207 t[nrl][ncl]=(float *) malloc((size_t)((nrow*ncol*ndep+NR_END1)*sizeof(float)));
208 if (!t[nrl][ncl]) nrerror("allocation failure 3 in f3tensor()");
209 t[nrl][ncl] += NR_END1;
210 t[nrl][ncl] -= ndl;
211
212 for(j=ncl+1;j<=nch;j++) t[nrl][j]=t[nrl][j-1]+ndep;
213 for(i=nrl+1;i<=nrh;i++) {
214 t[i]=t[i-1]+ncol;
215 t[i][ncl]=t[i-1][ncl]+ncol*ndep;
216 for(j=ncl+1;j<=nch;j++) t[i][j]=t[i][j-1]+ndep;
217 }
218
219 /* return pointer to array of pointers to rows */
220 return t;
221}
222
223void free_vector(float *v, long nl, long nh)
224/* free a float vector allocated with vector() */
225{
226 free((FREE_ARGchar*) (v+nl-NR_END1));
227}
228
229void free_ivector(int *v, long nl, long nh)
230/* free an int vector allocated with ivector() */
231{
232 free((FREE_ARGchar*) (v+nl-NR_END1));
233}
234
235void free_cvector(unsigned char *v, long nl, long nh)
236/* free an unsigned char vector allocated with cvector() */
237{
238 free((FREE_ARGchar*) (v+nl-NR_END1));
239}
240
241void free_lvector(unsigned long *v, long nl, long nh)
242/* free an unsigned long vector allocated with lvector() */
243{
244 free((FREE_ARGchar*) (v+nl-NR_END1));
245}
246
247void free_dvector(double *v, long nl, long nh)
248/* free a double vector allocated with dvector() */
249{
250 free((FREE_ARGchar*) (v+nl-NR_END1));
251}
252
253void free_matrix(float **m, long nrl, long nrh, long ncl, long nch)
254/* free a float matrix allocated by matrix() */
255{
256 free((FREE_ARGchar*) (m[nrl]+ncl-NR_END1));
257 free((FREE_ARGchar*) (m+nrl-NR_END1));
258}
259
260void free_dmatrix(double **m, long nrl, long nrh, long ncl, long nch)
261/* free a double matrix allocated by dmatrix() */
262{
263 free((FREE_ARGchar*) (m[nrl]+ncl-NR_END1));
264 free((FREE_ARGchar*) (m+nrl-NR_END1));
265}
266
267void free_imatrix(int **m, long nrl, long nrh, long ncl, long nch)
268/* free an int matrix allocated by imatrix() */
269{
270 free((FREE_ARGchar*) (m[nrl]+ncl-NR_END1));
271 free((FREE_ARGchar*) (m+nrl-NR_END1));
272}
273
274void free_submatrix(float **b, long nrl, long nrh, long ncl, long nch)
275/* free a submatrix allocated by submatrix() */
276{
277 free((FREE_ARGchar*) (b+nrl-NR_END1));
278}
279
280void free_convert_matrix(float **b, long nrl, long nrh, long ncl, long nch)
281/* free a matrix allocated by convert_matrix() */
282{
283 free((FREE_ARGchar*) (b+nrl-NR_END1));
284}
285
286void free_f3tensor(float ***t, long nrl, long nrh, long ncl, long nch,
287 long ndl, long ndh)
288/* free a float f3tensor allocated by f3tensor() */
289{
290 free((FREE_ARGchar*) (t[nrl][ncl]+ndl-NR_END1));
291 free((FREE_ARGchar*) (t[nrl]+ncl-NR_END1));
292 free((FREE_ARGchar*) (t+nrl-NR_END1));
293}