/*From art@Playfair.Stanford.EDU Fri Aug 19 11:30:56 1994 Received: from Playfair.Stanford.EDU by grover.stat.washington.edu (5.65/UW-NDC Revision: 2.23 ) id AA10241; Fri, 19 Aug 94 11:30:55 -0700Received: from tukey by playfair.Stanford.EDU with SMTP (5.67b8/25-eef) id AA21340; Fri, 19 Aug 1994 11:30:54 -0700 Received: by tukey (5.67b8/) id AA13850; Fri, 19 Aug 1994 11:30:53 -0700 Date: Fri, 19 Aug 1994 11:30:53 -0700 From: Art Owen Message-Id: <199408191830.AA13850@tukey> To: jaw@stat.washington.edu Subject: memory.c Status: RO */ /* Get and free vectors, matrices, arrays. This is done in a way similar to that used by Numerical Recipes in C, except that: errors do not cause an exit, the lower subscript is always zero, the vector getter is called by the matrix getter, and there are some 3d structures. lint will complain about possible pointer alignment problems. Conventions: - double is the default - it is an error to ask for a structure with a negative dimension - vectors of length zero are not errors; it is assumed that the calling routine knows not to try anything with them. Glossary: get_vector get_ivector get_matrix get_imatrix get_3array get_i3array get_PFDvector get_PFDmatrix free_ the above */ #include /*#include */ #include double *get_vector( n) int n; { double *v; if( n<0) fprintf(stderr,"Request for %d doubles.\n",n); if( n<=0)return NULL; v = (double *)malloc((unsigned) n*sizeof(double)); if( !v)fprintf(stderr,"Can't obtain %d doubles.\n",n); return v; } int *get_ivector( n) int n; { int *v; if( n<0) fprintf(stderr,"Request for %d ints detected.\n",n); if( n<=0)return NULL; v = (int *)malloc((unsigned) n*sizeof(int)); if( !v)fprintf(stderr,"Can't obtain %d ints.\n",n); return v; } double **get_matrix(n,p) int n,p; { double **m; int i,j; if( n<0 || p<0){ fprintf(stderr,"Request for %d by %d matrix detected.\n",n,p); return NULL; } if( n==0)return NULL; m = (double **)malloc((unsigned) n*sizeof(double *)); if( !m) fprintf(stderr,"Can't obtain %d *doubles.\n",n); else for( i=0; i=0;i--) free_vector( x[i],p); if(n)free( (char*) x ); } free_3array( a,n,p,q) double ***a; int n,p,q; { int i; /*printf("free a %d %d %d\n",n,p,q);*/ for( i=n-1; i>=0;i--) free_matrix( a[i],p,q); if(n>0)free( (char*) a); if(n<0)fprintf(stderr,"Asked to free %d matrices.",n); } free_ivector( v,n) int *v; int n; { if(n)free( (char*) v ); } free_imatrix( x,n,p) int **x; int n,p; { int i; for( i=n-1; i>=0;i--) free_ivector( x[i],p); if(n)free( (char*) x ); } free_i3array( a,n,p,q) int ***a; int n,p,q; { int i; for( i=n-1; i>=0;i--) free_imatrix( a[i],p,q); if(n)free( (char*) a); } /* PFD = Pointer to Function returning Double */ typedef double (*PFD)(); PFD *get_PFDvector( n) int n; { PFD *v; if( n<0) fprintf(stderr,"Request for %d PFDs.\n",n); if( n<=0)return NULL; v = (PFD *)malloc((unsigned) n*sizeof(PFD)); if( !v)fprintf(stderr,"Can't obtain %d PFDs.\n",n); return v; } PFD **get_PFDmatrix(n,p) int n,p; { PFD **m; int i; if( n<0 || p<0){ fprintf(stderr,"Request for %d by %d PFD matrix detected.\n",n,p); return NULL; } if( n==0)return NULL; m = (PFD **)malloc((unsigned) n*sizeof(PFD *)); if( !m) fprintf(stderr,"Can't obtain %d *PFDs.\n",n); else for( i=0; i