C   24
nrerror
Guest on 14th March 2023 12:03:34 AM


  1. #include <stdio.h>
  2. #include <stddef.h>
  3. #include <stdlib.h>
  4. #define NR_END 1
  5. #define FREE_ARG char*
  6.  
  7. void nrerror(char error_text[])
  8. /* Numerical Recipes standard error handler */
  9. {
  10.         fprintf(stderr,"Numerical Recipes run-time error...\n");
  11.         fprintf(stderr,"%s\n",error_text);
  12.         fprintf(stderr,"...now exiting to system...\n");
  13.         exit(1);
  14. }
  15.  
  16.  
  17. int *ivector(long nl, long nh)
  18. /* allocate an int vector with subscript range v[nl..nh] */
  19. {
  20.         int *v;
  21.  
  22.         v=(int *)malloc((size_t) ((nh-nl+1+NR_END)*sizeof(int)));
  23.         if (!v) nrerror("allocation failure in ivector()");
  24.         return v-nl+NR_END;
  25. }
  26.  
  27.  
  28. double *dvector(long nl, long nh)
  29. /* allocate a double vector with subscript range v[nl..nh] */
  30. {
  31.         double *v;
  32.  
  33.         v=(double *)malloc((size_t) ((nh-nl+1+NR_END)*sizeof(double)));
  34.         if (!v) nrerror("allocation failure in dvector()");
  35.         return v-nl+NR_END;
  36. }
  37.  
  38.  
  39. void free_dvector(double *v, long nl, long nh)
  40. /* free a double vector allocated with dvector() */
  41. {
  42.         free((FREE_ARG) (v+nl-NR_END));
  43. }
  44.  
  45.  
  46. void free_ivector(int *v, long nl, long nh)
  47. /* free an int vector allocated with ivector() */
  48. {
  49.         free((FREE_ARG) (v+nl-NR_END));
  50. }

Raw Paste

Login or Register to edit or fork this paste. It's free.