erreur de compilation( cygwin)

erreur de compilation( cygwin) - C - Programmation

Marsh Posté le 04-02-2011 à 11:09:20    

Je suis un doctorant en finance et j’ai appris tout seul à programmer avec le langage C. Certes, je sais quelques notions dans la programmation mais je suis toujours débutant dans ce domaine.  Je compile les programmes avec cygwin. De plus, j’utilise, lors de cette compilation, la librairie GSL (GNU scientific Librairy).
Bref, lors de l’exécution d’un programme (qui permet de calculer une intégration numérique et que j’ai  nommé cdf_mdim), j’ai eu le message suivant (avec le débuggeur gdb) :
 
     9 [main] cdf_Mdim 3816 _cygtls::handle_exceptions: Exception: STATUS_ACCESS_VIOLATION
  1637 [main] cdf_Mdim 3816 open_stackdumpfile: Dumping stack trace to cdf_Mdim.exe.stackdump
521872 [main] cdf_Mdim 3816 _cygtls::handle_exceptions: Exception: STATUS_ACCESS_VIOLATION
578746 [main] cdf_Mdim 3816 _cygtls::handle_exceptions: Error while dumping state (probably corrupted stack)
Program received signal SIGSEGV, Segmentation fault.
0x61016525 in stack_info::walk () from /usr/bin/cygwin1.dll
 
 
Est que quelqu'un peut m’aider à résoudre ce problème (je peux vous envoyer mon code par mail dans ce cas). Est-ce que c’est un problème de mémoire de mon ordinateur (processeur centrino duo 1.73 ghz et une RAM de 2 Go) ?
Merci d’avance

Reply

Marsh Posté le 04-02-2011 à 11:09:20   

Reply

Marsh Posté le 04-02-2011 à 14:04:07    

C'est un problème de pile corrompu.
 
Cela peut venir, par exemple :
 
- un tableau qui est déclaré avec une taille trop petite par rapport à ce que l'on veut y mettre,
- une chaîne de caractères qui n'est pas terminée par un caractère nul,
- un indice qui est négatif (toto[-1]),
- une mauvaise libération des ressources (par exemple un oubli d'un free; par exemple un free() en trop (if (toto != NULL) free(toto);, mais toto n'a pas été initialisé, et contient autre chose que NULL par hasard); par exemple, un mélange entre delete du C++ et free du C; etc).
 
 

Reply

Marsh Posté le 04-02-2011 à 14:13:40    

Voici mon programme principal:
 
 
 

Code :
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4. #include <gsl/gsl_math.h>
  5. #include <gsl/gsl_sf_pow_int.h>
  6. #include <gsl/gsl_linalg.h>
  7. #include <gsl/gsl_matrix.h>
  8. #include <gsl/gsl_vector.h>
  9. #include <gsl/gsl_blas.h>
  10. #include <gsl/gsl_sf_exp.h>
  11. #include "cubature.h"
  12. int count;
  13. int n;
  14. double bivar_cdf(unsigned dim, const double * x1, void *data)
  15. {
  16.   int i,j;
  17.   int s;
  18.   double det,val, S;
  19.   double *R;
  20.   double P =dim;
  21.   R = (double *) malloc(1 * sizeof(double));
  22.   gsl_permutation* p = gsl_permutation_alloc(dim);
  23.   gsl_vector * x = gsl_vector_alloc (dim);
  24.   gsl_vector * xs = gsl_vector_alloc (dim);
  25.   gsl_matrix * A = gsl_matrix_alloc (dim, dim);
  26.   gsl_matrix * At = gsl_matrix_alloc (dim, dim);
  27.   gsl_matrix * SIG = gsl_matrix_alloc (dim, dim);
  28.   gsl_matrix *SIGi = gsl_matrix_alloc (dim, dim);
  29.   ++count;
  30.   for (i=0; i < dim; i++){
  31.     for (j=0; j < dim; j++){
  32.       if (i < j) gsl_matrix_set (A,i,j,0);
  33.    
  34.       else gsl_matrix_set (A,i,j,1);
  35.  
  36.     }}
  37. for (i=0; i < dim; i++) gsl_vector_set (x,i,x1[i]);
  38.   gsl_matrix_transpose_memcpy(At,A);
  39.   gsl_blas_dgemm (CblasNoTrans, CblasNoTrans,1.0, A, At, 0.0, SIG);
  40.   gsl_linalg_LU_decomp( SIG, p, &s );
  41.   gsl_linalg_LU_invert(SIG,p,SIGi);
  42.   det  = gsl_linalg_LU_det(SIG,s);
  43.   gsl_blas_dgemv(CblasNoTrans, 1.0, SIGi, x, 0.0, xs);
  44.   gsl_blas_ddot(x,xs,R);
  45.   S = exp ((-R[0]/2));
  46.   val =(1/(pow((2*M_PI),(P/2)))*(pow(abs(det),0.5)))*S;
  47.   return val;
  48. }
  49. int main()
  50. {
  51. int i;
  52. double val, err;
  53. double tol = 1.e-6;
  54. double *xmin, *xmax;
  55. unsigned dim = 6;
  56. unsigned maxEval =  1000000;
  57. printf("#==================================================================\n" );
  58. printf("# Parameters:\n" );
  59. printf("# dim = %d   tol = %f   maximum number of evals = %d\n",dim,tol,maxEval);
  60. xmin = (double *) malloc(dim * sizeof(double));
  61. xmax = (double *) malloc(dim * sizeof(double));
  62. for (i = 0; i < dim; i++) {
  63.    xmin[i] = -99;
  64.    xmax[i] = 1.96;
  65.  
  66. }
  67.  
  68.    count = 0;
  69.    adapt_integrate(bivar_cdf, 0, dim, xmin, xmax, maxEval, 0, tol, &val, &err);
  70. printf("#------------------------------------------------------------------\n" );
  71. printf("# Results:\n" );
  72. printf("# Integration value = %g\n",val);
  73. printf("# Est. err = %g\n", err);
  74. printf("# Number of function evals required = %d\n", count);
  75. printf("#==================================================================\n" );
  76. free(xmax);
  77. free(xmin);
  78. return 0;
  79. }


 
RQ:  si maxEval =  100000 le programme s'exécute normalement.


Message édité par gilou le 04-02-2011 à 15:57:28
Reply

Marsh Posté le 04-02-2011 à 14:37:48    

unsigned maxEval =  1000000;

Avec quelle valeur de maxEval est-ce que cela ne va plus ?
 
Par ailleurs, il serait plus lisible d'écrire unsigned int ou unsigned long int ou unsigned char.
Si c'est un projet pour DOS, il me semble que unsigned int est sur 16 bit donc que sa valeur maximale est 65535.
 

double *R;  
...
  R = (double *) malloc(1 * sizeof(double));  
...
gsl_blas_ddot(x,xs,R);

R est alloué avec malloc, mais n'est pas libéré.
 
D'ailleurs, il serait plus facile et plus performant de remplacer cette allocation, par une simple déclaration.

double R;
...
gsl_blas_ddot(x,xs,&R);


Message édité par billgatesanonym le 04-02-2011 à 14:39:04
Reply

Marsh Posté le 04-02-2011 à 14:46:35    

Juste un point de détail:

Citation :

Par ailleurs, il serait plus lisible d'écrire unsigned int ou unsigned long int ou unsigned char.  
Si c'est un projet pour DOS, il me semble que unsigned int est sur 16 bit donc que sa valeur maximale est 65535.

En C, si ça ne tient pas dans un int, le compilateur doit automatiquement le mettre dans un long. Donc à priori, ça devrait tenir.
EDIT: C'était en K&R C, mais apparemment, l'ANSI C impose unsigned = unsigned int en ce cas.
 
Si le pb est avec la taille de maxEval, comme on ne sait pas comment la valeur est utilisée dans adapt_integrate(bivar_cdf, 0, dim, xmin, xmax, maxEval, 0, tol, &val, &err); on ne peut pas dire grand chose.
 
A+,


Message édité par gilou le 04-02-2011 à 16:25:09

---------------
There's more than what can be linked! --    Iyashikei Anime Forever!    --  AngularJS c'est un framework d'engulé!  --
Reply

Marsh Posté le 04-02-2011 à 15:27:04    

voici la fonction adapt-integrate:
 

Code :
  1. #include "cubature_copyright.h"
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <math.h>
  5. #include <limits.h>
  6. #include <float.h>
  7. #include <string.h>
  8. void Usage();
  9. typedef double (*integrand) (unsigned ndim, const double *x, void *);
  10. int adapt_integrate(integrand f, void *fdata,
  11.         unsigned dim, const double *xmin, const double *xmax,
  12.         unsigned maxEval,
  13.         double reqAbsError, double reqRelError,
  14.         double *val, double *estimated_error);
  15. typedef struct {
  16.     double val, err;
  17. } esterr;
  18. /*
  19.   * Function prototypes
  20.   */
  21. static double relError(esterr ee);
  22. typedef struct {
  23.     unsigned dim;
  24.     double *center;   /* length 2*dim = center followed by half-width */
  25.     double *width;    /* length 2*dim = center followed by half-width */
  26.     double vol;       /* cache volume = product of widths */
  27. } hypercube;
  28. typedef struct {


Message édité par gilou le 04-02-2011 à 15:57:45
Reply

Marsh Posté le 04-02-2011 à 15:32:39    

C'est son proto, mais ça ne nous dit toujours pas ce qu'elle fait de maxEval
Par contre effectivement, quand tu passes ça en paramètre de fonction, le unsigned est probablement casté en unsigned int, et la ta valeur 100000 pourrait ne pas passer.
A+,


Message édité par gilou le 04-02-2011 à 15:34:15

---------------
There's more than what can be linked! --    Iyashikei Anime Forever!    --  AngularJS c'est un framework d'engulé!  --
Reply

Marsh Posté le 04-02-2011 à 15:56:26    

OK gilou, je vais essayer
voici le programme cubature.c
 

Code :
  1. #include "cubature.h"
  2. #include "cubature_debug.h"
  3. /*
  4.    Adaptive multidimensional integration on hypercubes (or, really,
  5.    hyper-rectangles) using cubature rules.
  6.    A cubature rule takes a function and a hypercube and evaluates
  7.    the function at a small number of points, returning an estimate
  8.    of the integral as well as an estimate of the error, and also
  9.    a suggested dimension of the hypercube to subdivide.
  10.    Given such a rule, the adaptive integration is simple:
  11.    1) Evaluate the cubature rule on the hypercube(s).
  12.       Stop if converged.
  13.    2) Pick the hypercube with the largest estimated error,
  14.       and divide it in two along the suggested dimension.
  15.    3) Goto (1).
  16. */
  17. /*
  18.   * Integrate the function f from xmin[dim] to xmax[dim], with at most
  19.   * maxEval function evaluations (0 for no limit), until the given
  20.   * absolute is achieved relative error.  val returns the integral, and
  21.   * estimated_error returns the estimate for the absolute error in val.
  22.   * The return value of the function is 0 on success and non-zero if there
  23.   * was an error.
  24.   */
  25. /*
  26. *=======================================================
  27. * cubature subroutines themselves follow.
  28. *=======================================================
  29. */
  30. static double relError(esterr ee)
  31. {
  32.     return (ee.val == 0.0 ? HUGE_VAL : fabs(ee.err / ee.val));
  33. }
  34. /*
  35. * Compute volume of hypercube from the product of its widths
  36. */
  37. static double compute_vol(const hypercube * h)
  38. {
  39. unsigned i;
  40. double vol = 1;
  41. for (i = 0; i < h->dim; i++)
  42.    vol *= 2.0 * h->width[i];
  43. return vol;
  44. }
  45. /*
  46. * This should be more efficient than looping to copy contents and still
  47. * requires a single malloc.  Saves on indirect addressing arithmetic
  48. * later, easier to understand the code.
  49. */
  50. static hypercube make_hypercube(unsigned dim, const double *center,
  51.        const double *width)
  52. {
  53. unsigned long int i,dlen;
  54. hypercube h;
  55. h.dim = dim;
  56. dlen = sizeof(double)*dim;
  57. h.center = (double *) malloc(2*dlen);
  58. h.width = &h.center[dim];
  59. memcpy(h.center,center,dlen);
  60. memcpy(h.width,width,dlen);
  61. h.vol = compute_vol(&h);
  62. if(debug(D_CUBATURE)){
  63.    printf("# make_hypercube(): h.center = (" );
  64.    for(i = 0;i<dim;i++){
  65.      printf("%4.2f",h.center[i]);
  66.      if(i != dim-1) printf("," );
  67.    }
  68.    printf(" ) h.width = (" );
  69.    for(i = 0;i<dim;i++){
  70.      printf("%4.2f",h.width[i]);
  71.      if(i != dim-1) printf("," );
  72.    }
  73.    printf(" )\n" );
  74. }
  75. return h;
  76. }
  77. static hypercube make_hypercube_range(unsigned dim, const double *xmin,
  78.            const double *xmax)
  79. {
  80. unsigned long int i,dlen;
  81. hypercube h;
  82. h.dim = dim;
  83. dlen = sizeof(double)*dim;
  84. h.center = (double *) malloc(2*dlen);
  85. h.width = &h.center[dim];
  86. for (i = 0; i < dim; ++i) {
  87.    h.center[i] = 0.5 * (xmin[i] + xmax[i]);
  88.    h.width[i] = 0.5 * (xmax[i] - xmin[i]);
  89. }
  90. h.vol = compute_vol(&h);
  91. /* printf("Made ranged hypercube at %0x, %0x\n",h.center,h.hwidth); */
  92. return h;
  93. }
  94. /*
  95. * Destroy a hypercube.  Note that the data was malloc'd only
  96. * to h->center, so we don't have to do anything with h->hwidth.
  97. */
  98. static void destroy_hypercube(hypercube *h)
  99. {
  100. /* printf("About to free hypercube %0x\n",h->center); */
  101. free(h->center);
  102. h->dim = 0;
  103. }
  104. static region make_region(const hypercube * h)
  105. {
  106. region R;
  107. if(debug(D_CUBATURE)){
  108.    printf("# make_region():  Entry.\n" );
  109. }
  110. R.h = make_hypercube(h->dim, h->center, h->width);
  111. R.splitDim = 0;
  112. return R;
  113. }
  114. static void destroy_region(region * R)
  115. {
  116. destroy_hypercube(&R->h);
  117. }
  118. /*
  119. * This takes a region with a splitDim defined and creates two regions
  120. * (hypercubes) with half the width in the selected dimension.  The original
  121. * region R contains the left-half region, the region R2 contains the
  122. * right hand region on return.
  123. */
  124. static void cut_region(region * R, region * R2)
  125. {
  126. unsigned d = R->splitDim, dim = R->h.dim;
  127. /* Halve the width and volume */
  128. R->h.width[d] *= 0.5;
  129. R->h.vol *= 0.5;
  130. /* Make a new hypercube from the center to the new width. */
  131. R2->h = make_hypercube(dim, R->h.center, R->h.width);
  132. /* Shift the center of the original back to the left */
  133. R->h.center[d] -= R->h.width[d];
  134. /*  Shift the center of the new one over to the right */
  135. R2->h.center[d] += R->h.width[d];
  136. }
  137. static void destroy_rule(rule * r)
  138. {
  139. if (r->destroy)
  140.     r->destroy(r);
  141. free(r);
  142. }
  143. /*
  144. * This routine returns a region with its "worst direction" set.
  145. * At this point the integral for the region is already set as well.
  146. */
  147. static region eval_region(region R, integrand f, void *fdata, rule * r)
  148. {
  149. if(debug(D_CUBATURE)){
  150.    printf("# eval_region():  Entry.\n" );
  151. }
  152. R.splitDim = r->evalError(r, f, fdata, &R.h, &R.ee);
  153. return R;
  154. }
  155. /*
  156. *============================================================
  157. * Functions to loop over points in a hypercube.
  158. *============================================================
  159. */
  160. /*
  161. * Based on orbitrule.cpp in HIntLib-0.0.10
  162. */
  163. /*
  164. * ls0 returns the least-significant 0 bit of n (e.g. it returns 0 if the LSB
  165. * is 0, it returns 1 if the 2 LSBs are 01, etcetera).
  166. */
  167. #if (defined(__GNUC__) || defined(__ICC)) && (defined(__i386__) || defined (__x86_64__))
  168. /*
  169. * use x86 bit-scan instruction, based on count_trailing_zeros() macro in
  170. * GNU GMP's longlong.h.
  171. */
  172. static unsigned ls0(unsigned n)
  173. {
  174. unsigned count;
  175. n = ~n;
  176. __asm__("bsfl %1,%0": "=r"(count):"rm"(n));
  177. return count;
  178. }
  179. #else
  180. static unsigned ls0(unsigned n)
  181. {
  182. const unsigned bits[] = {
  183.   0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 4,
  184.   0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 5,
  185.   0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 4,
  186.   0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 6,
  187.   0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 4,
  188.   0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 5,
  189.   0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 4,
  190.   0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 7,
  191.   0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 4,
  192.   0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 5,
  193.   0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 4,
  194.   0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 6,
  195.   0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 4,
  196.   0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 5,
  197.   0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 4,
  198.   0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 8,
  199. };
  200. unsigned bit;
  201. bit = 0;
  202. while ((n & 0xff) == 0xff) {
  203.    n >>= 8;
  204.    bit += 8;
  205. }
  206. return bit + bits[n & 0xff];
  207. }
  208. #endif
  209. /*
  210. * This rule is used for sum5/lambda5 displacements.  Note that "r" is  
  211. * actually plus or minus the (half) width scaled by lambda(s).
  212. *
  213. * Evaluate the integral on all 2^n points (+/-r,...+/-r) * A Gray-code
  214. * ordering is used to minimize the number of coordinate updates in p.
  215. */
  216. static double evalR_Rfs(integrand f, void *fdata, unsigned dim, double *p,
  217.       const double *c, const double *r)
  218. {
  219. double sum = 0;
  220. unsigned i, ip;
  221. unsigned signs = 0;    /* 0/1 bit = +/- for corresponding element of r[] */
  222. if(debug(D_CUBATURE)){
  223.    printf("# evalR_Rfs():  Entry.\n" );
  224. }
  225. /*
  226.   * We start with the point where r is ADDed in every coordinate (This implies
  227.   * signs=0).  The points[i] are center[i] + width[i].
  228.   */
  229. for (i = 0; i < dim; ++i)
  230.    p[i] = c[i] + r[i];
  231. /* Loop through the points in gray-code ordering */
  232. for (i = 0;; ++i) {
  233.    unsigned mask;
  234.    if(debug(D_CUBATURE)){
  235.      printf("# evalR_Rfs():  Evaluating f at (" );
  236.      for(ip = 0;ip<dim;ip++){
  237.        printf("%4.2f",p[ip]);
  238.        if(ip != dim-1) printf("," );
  239.      }
  240.      printf(" )\n" );
  241.    }
  242.    sum += f(dim, p, fdata);
  243.    unsigned d = ls0(i);  /* which coordinate to flip */
  244.    if (d >= dim)
  245.       break;
  246.    /* flip the d-th bit and add/subtract r[d] */
  247.    mask = 1U << d;
  248.    signs ^= mask;
  249.    p[d] = (signs & mask) ? c[d] - r[d] : c[d] + r[d];
  250. }
  251. if(debug(D_CUBATURE)){
  252.    printf("# evalR_Rfs():  Done.\n" );
  253. }
  254. return sum;
  255. }
  256. /*
  257. * This one is used to evaluate sum4 at lambda4, which is really
  258. * lambda.  Go figure.
  259. */
  260. static double evalRR0_0fs(integrand f, void *fdata, unsigned dim,
  261.         double *p, const double *c, const double *r)
  262. {
  263. unsigned i, j, ip;
  264. double sum = 0;
  265. if(debug(D_CUBATURE)){
  266.    printf("# evalRR0_0fs():  Entry.\n" );
  267. }
  268. for (i = 0; i < dim - 1; i++) {
  269.    p[i] = c[i] - r[i];
  270.    for (j = i + 1; j < dim; j++) {
  271.      p[j] = c[j] - r[j];
  272.      if(debug(D_CUBATURE)){
  273.        printf("# evalRR0_0fs():  Evaluating f at (" );
  274.        for(ip = 0;ip<dim;ip++){
  275.          printf("%4.2f",p[ip]);
  276.          if(ip != dim-1) printf("," );
  277.        }
  278.        printf(" )\n" );
  279.      }
  280.      sum += f(dim, p, fdata);
  281.      p[i] = c[i] + r[i];
  282.      if(debug(D_CUBATURE)){
  283.        printf("# evalRR0_0fs():  Evaluating f at (" );
  284.        for(ip = 0;ip<dim;ip++){
  285.          printf("%4.2f",p[ip]);
  286.          if(ip != dim-1) printf("," );
  287.        }
  288.        printf(" )\n" );
  289.      }
  290.      sum += f(dim, p, fdata);
  291.      p[j] = c[j] + r[j];
  292.      if(debug(D_CUBATURE)){
  293.        printf("# evalRR0_0fs():  Evaluating f at (" );
  294.        for(ip = 0;ip<dim;ip++){
  295.          printf("%4.2f",p[ip]);
  296.          if(ip != dim-1) printf("," );
  297.        }
  298.        printf(" )\n" );
  299.      }
  300.      sum += f(dim, p, fdata);
  301.      p[i] = c[i] - r[i];
  302.      if(debug(D_CUBATURE)){
  303.        printf("# evalRR0_0fs():  Evaluating f at (" );
  304.        for(ip = 0;ip<dim;ip++){
  305.          printf("%4.2f",p[ip]);
  306.          if(ip != dim-1) printf("," );
  307.        }
  308.        printf(" )\n" );
  309.      }
  310.      sum += f(dim, p, fdata);
  311.      p[j] = c[j];  /* Done with j -> Restore p[j] */
  312.      if(debug(D_CUBATURE)){
  313.        printf("# evalRR0_0fs():  Restoring p to (" );
  314.        for(ip = 0;ip<dim;ip++){
  315.          printf("%4.2f",p[ip]);
  316.          if(ip != dim-1) printf("," );
  317.        }
  318.        printf(" )\n" );
  319.      }
  320.    }
  321.    p[i] = c[i];    /* Done with i -> Restore p[i] */
  322.    if(debug(D_CUBATURE)){
  323.      printf("# evalRR0_0fs():  Restoring p to (" );
  324.      for(ip = 0;ip<dim;ip++){
  325.        printf("%4.2f",p[ip]);
  326.        if(ip != dim-1) printf("," );
  327.      }
  328.      printf(" )\n" );
  329.    }
  330. }
  331. if(debug(D_CUBATURE)){
  332.    printf("# evalRR0_0fs():  Done.\n" );
  333. }
  334. return sum;
  335. }
  336. /*
  337. * This evaluates sum1, sum2 and sum3, mislabelled for no good
  338. * reason (relative to their call) by one.  They correspond as:
  339. * center(p)->sum1(0), lambda2(Lambda2)->sum2(1), lambda4(Lambda)->sum3(2)
  340. * which is just incredibly Evil.  I'll really think hard about fixing
  341. * this ONCE I've figured out why the routine is calling points that
  342. * are WAY past the integration limits, which is almost certainly a simple
  343. * bug.
  344. */
  345. static double evalR0_0fs4d(integrand f, void *fdata, unsigned dim,
  346.          double *p, const double *c, double *sum0_,
  347.          const double *r1, double *sum1_,
  348.          const double *r2, double *sum2_)
  349. {
  350. double maxdiff = 0;
  351. unsigned i, ip, dimDiffMax = 0;
  352. double sum0, sum1 = 0, sum2 = 0;  /* copies for aliasing, performance */
  353. double ratio = r1[0] / r2[0];
  354. ratio *= ratio;
  355. if(debug(D_CUBATURE)){
  356.    printf("# evalR0_0fs4d():  Entry.\n" );
  357. }
  358. /*
  359.   * Evaluate at the center of the current hypercube
  360.   */
  361. if(debug(D_CUBATURE)){
  362.    printf("# evalR0_0fs4d():  Evaluating f at (" );
  363.    for(ip = 0;ip<dim;ip++){
  364.      printf("%4.2f",c[ip]);
  365.      if(ip != dim-1) printf("," );
  366.    }
  367.    printf(" )\n" );
  368. }
  369. sum0 = f(dim, c, fdata);
  370. for (i = 0; i < dim; i++) {
  371.    double f1a, f1b, f2a, f2b, diff;
  372.    p[i] = c[i] - r1[i];
  373.    if(debug(D_CUBATURE)){
  374.      printf("# evalR0_0fs4d():  Evaluating f at (" );
  375.      for(ip = 0;ip<dim;ip++){
  376.        printf("%4.2f",p[ip]);
  377.        if(ip != dim-1) printf("," );
  378.      }
  379.      printf(" )\n" );
  380.    }
  381.    sum1 += (f1a = f(dim, p, fdata));
  382.    p[i] = c[i] + r1[i];
  383.    if(debug(D_CUBATURE)){
  384.      printf("# evalR0_0fs4d():  Evaluating f at (" );
  385.      for(ip = 0;ip<dim;ip++){
  386.        printf("%4.2f",p[ip]);
  387.        if(ip != dim-1) printf("," );
  388.      }
  389.      printf(" )\n" );
  390.    }
  391.    sum1 += (f1b = f(dim, p, fdata));
  392.    p[i] = c[i] - r2[i];
  393.    if(debug(D_CUBATURE)){
  394.      printf("# evalR0_0fs4d():  Evaluating f at (" );
  395.      for(ip = 0;ip<dim;ip++){
  396.        printf("%4.2f",p[ip]);
  397.        if(ip != dim-1) printf("," );
  398.      }
  399.      printf(" )\n" );
  400.    }
  401.    sum2 += (f2a = f(dim, p, fdata));
  402.    p[i] = c[i] + r2[i];
  403.    if(debug(D_CUBATURE)){
  404.      printf("# evalR0_0fs4d():  Evaluating f at (" );
  405.      for(ip = 0;ip<dim;ip++){
  406.        printf("%4.2f",p[ip]);
  407.        if(ip != dim-1) printf("," );
  408.      }
  409.      printf(" )\n" );
  410.    }
  411.    sum2 += (f2b = f(dim, p, fdata));
  412.    p[i] = c[i];
  413.    if(debug(D_CUBATURE)){
  414.      printf("# evalR0_0fs4d():  Setting p back to (" );
  415.      for(ip = 0;ip<dim;ip++){
  416.        printf("%4.2f",p[ip]);
  417.        if(ip != dim-1) printf("," );
  418.      }
  419.      printf(" )\n" );
  420.    }
  421.    diff = fabs(f1a + f1b - 2 * sum0 - ratio * (f2a + f2b - 2 * sum0));
  422.    if (diff > maxdiff) {
  423.      maxdiff = diff;
  424.      dimDiffMax = i;
  425.    }
  426. }
  427. *sum0_ += sum0;
  428. *sum1_ += sum1;
  429. *sum2_ += sum2;
  430. if(debug(D_CUBATURE)){
  431.    printf("# evalR0_0fs4d():  Done.\n" );
  432. }
  433. return dimDiffMax;
  434. }
  435. #define num0_0(dim) (1U)
  436. #define numR0_0fs(dim) (2 * (dim))
  437. #define numRR0_0fs(dim) (2 * (dim) * (dim-1))
  438. #define numR_Rfs(dim) (1U << (dim))
  439. /*
  440. *=======================================================================
  441. * Based on rule75genzmalik.cpp in HIntLib-0.0.10: An embedded cubature
  442. * rule of degree 7 (embedded rule degree 5) due to A. C. Genz and A. A.
  443. * Malik.  See:  A. C. Genz and A. A. Malik, "An imbedded [sic] family of
  444. * fully symmetric numerical integration rules," SIAM J. Numer. Anal. 20
  445. * (3), 580-588 (1983).
  446. *=======================================================================
  447. */
  448. #define real(x) ((double)(x))
  449. #define to_int(n) ((int)(n))
  450. static int isqr(int x)
  451. {
  452. return x * x;
  453. }
  454. static void destroy_rule75genzmalik(rule * r_)
  455. {
  456. rule75genzmalik *r = (rule75genzmalik *) r_;
  457. free(r->p);
  458. }
  459. static unsigned rule75genzmalik_evalError(rule * r_, integrand f,
  460.             void *fdata, const hypercube * h,
  461.             esterr * ee)
  462. {
  463. /* lambda2 = sqrt(9/70), lambda4 = sqrt(9/10), lambda5 = sqrt(9/19) */
  464. const double lambda2 = 0.3585685828003180919906451539079374954541;
  465. const double lambda4 = 0.9486832980505137995996680633298155601160;
  466. const double lambda5 = 0.6882472016116852977216287342936235251269;
  467. const double weight2 = 980. / 6561.;
  468. const double weight4 = 200. / 19683.;
  469. const double weightE2 = 245. / 486.;
  470. const double weightE4 = 25. / 729.;
  471. rule75genzmalik *r = (rule75genzmalik *) r_;
  472. unsigned i, dimDiffMax, dim = r_->dim;
  473. double sum1 = 0.0, sum2 = 0.0, sum3 = 0.0, sum4, sum5, result, res5th;
  474. if(debug(D_CUBATURE)){
  475.    printf("# rule75genzmalik_evalError():  Entry.\n" );
  476. }
  477. for (i = 0; i < dim; i++){
  478.    r->p[i] = h->center[i];
  479.    r->widthLambda[i] = h->width[i] * lambda4;
  480.    r->widthLambda2[i] = h->width[i] * lambda2;
  481.    r->widthLambda5[i] = h->width[i] * lambda5;
  482.    if(debug(D_CUBATURE)){
  483.      printf("# i=%d: center = %f, wlam = %f, wlam2 = %f, wlam5 = %f\n",i,
  484.        r->p[i],r->widthLambda[i],r->widthLambda2[i],r->widthLambda5[i]);
  485.    }
  486. }
  487. /*
  488.   * Evaluate function at the center, at f(lambda2,0,...,0) and at
  489.   * f(lambda3=lambda4, 0,...,0).  Estimate dimension with largest error
  490.   */
  491. dimDiffMax =
  492.    evalR0_0fs4d(f, fdata, dim, r->p, h->center, &sum1, r->widthLambda2,
  493.          &sum2, r->widthLambda, &sum3);
  494. /*
  495.   * Calculate sum4 for f(lambda4, lambda4, 0, ...,0)
  496.   */
  497. sum4 = evalRR0_0fs(f, fdata, dim, r->p, h->center, r->widthLambda);
  498. /*
  499.   * Calculate sum5 for f(lambda5, lambda5, ..., lambda5)
  500.   */
  501. sum5 = evalR_Rfs(f, fdata, dim, r->p, h->center, r->widthLambda5);
  502. /* Calculate fifth and seventh order results */
  503. result =
  504.    h->vol * (r->weight1 * sum1 + weight2 * sum2 + r->weight3 * sum3 +
  505.       weight4 * sum4 + r->weight5 * sum5);
  506. res5th =
  507.    h->vol * (r->weightE1 * sum1 + weightE2 * sum2 +
  508.       r->weightE3 * sum3 + weightE4 * sum4);
  509. ee->val = result;
  510. ee->err = fabs(res5th - result);
  511. return dimDiffMax;
  512. }
  513. static rule *make_rule75genzmalik(unsigned dim)
  514. {
  515. rule75genzmalik *r;
  516. if (dim < 2)
  517.    return 0;    /* this rule does not support 1d integrals */
  518. r = (rule75genzmalik *) malloc(sizeof(rule75genzmalik));
  519. r->parent.dim = dim;
  520. r->weight1 =
  521.    (real(12824 - 9120 * to_int(dim) + 400 * isqr(to_int(dim)))
  522.      / real(19683));
  523. r->weight3 = real(1820 - 400 * to_int(dim)) / real(19683);
  524. r->weight5 = real(6859) / real(19683) / real(1U << dim);
  525. r->weightE1 = (real(729 - 950 * to_int(dim) + 50 * isqr(to_int(dim)))
  526.        / real(729));
  527. r->weightE3 = real(265 - 100 * to_int(dim)) / real(1458);
  528. r->p = (double *) malloc(sizeof(double) * dim * 4);
  529. r->widthLambda = r->p + dim;
  530. r->widthLambda2 = r->p + 2 * dim;
  531. r->widthLambda5 = r->p + 3 * dim;
  532. r->parent.num_points = num0_0(dim) + 2 * numR0_0fs(dim)
  533.    + numRR0_0fs(dim) + numR_Rfs(dim);
  534. r->parent.evalError = rule75genzmalik_evalError;
  535. r->parent.destroy = destroy_rule75genzmalik;
  536. return (rule *) r;
  537. }
  538. /***************************************************************************/
  539. /*
  540. * 1d 15-point Gaussian quadrature rule, based on qk15.c and qk.c in GNU
  541. * GSL (which in turn is based on QUADPACK).
  542. */
  543. static unsigned rule15gauss_evalError(rule * r, integrand f, void *fdata,
  544.               const hypercube * h, esterr * ee)
  545. {
  546. /*
  547.   * Gauss quadrature weights and kronrod quadrature abscissae and weights as
  548.   * evaluated with 80 decimal digit arithmetic by L. W. Fullerton, Bell Labs,
  549.   * Nov. 1981.
  550.   */
  551. const unsigned n = 8;
  552. const double xgk[8] = {  /* abscissae of the 15-point kronrod rule */
  553.   0.991455371120812639206854697526329,
  554.   0.949107912342758524526189684047851,
  555.   0.864864423359769072789712788640926,
  556.   0.741531185599394439863864773280788,
  557.   0.586087235467691130294144838258730,
  558.   0.405845151377397166906606412076961,
  559.   0.207784955007898467600689403773245,
  560.   0.000000000000000000000000000000000
  561.       /* xgk[1], xgk[3], ... abscissae of the 7-point gauss rule.
  562.          xgk[0], xgk[2], ... to optimally extend the 7-point gauss rule */
  563. };
  564. static const double wg[4] = {  /* weights of the 7-point gauss rule */
  565.   0.129484966168869693270611432679082,
  566.   0.279705391489276667901467771423780,
  567.   0.381830050505118944950369775488975,
  568.   0.417959183673469387755102040816327
  569. };
  570. static const double wgk[8] = {  /* weights of the 15-point kronrod rule */
  571.   0.022935322010529224963732008058970,
  572.   0.063092092629978553290700663189204,
  573.   0.104790010322250183839876322541518,
  574.   0.140653259715525918745189590510238,
  575.   0.169004726639267902826583426598550,
  576.   0.190350578064785409913256402421014,
  577.   0.204432940075298892414161999234649,
  578.   0.209482141084727828012999174891714
  579. };
  580. const double center = h->center[0];
  581. const double width = h->width[0];
  582. double fv1[n - 1], fv2[n - 1];
  583. const double f_center = f(1, &center, fdata);
  584. double result_gauss = f_center * wg[n / 2 - 1];
  585. double result_kronrod = f_center * wgk[n - 1];
  586. double result_abs = fabs(result_kronrod);
  587. double result_asc, mean, err;
  588. unsigned j;
  589. for (j = 0; j < (n - 1) / 2; ++j) {
  590.    int j2 = 2 * j + 1;
  591.    double x, f1, f2, fsum, w = width * xgk[j2];
  592.    x = center - w;
  593.    fv1[j2] = f1 = f(1, &x, fdata);
  594.    x = center + w;
  595.    fv2[j2] = f2 = f(1, &x, fdata);
  596.    fsum = f1 + f2;
  597.    result_gauss += wg[j] * fsum;
  598.    result_kronrod += wgk[j2] * fsum;
  599.    result_abs += wgk[j2] * (fabs(f1) + fabs(f2));
  600. }
  601. for (j = 0; j < n / 2; ++j) {
  602.    int j2 = 2 * j;
  603.    double x, f1, f2, w = width * xgk[j2];
  604.    x = center - w;
  605.    fv1[j2] = f1 = f(1, &x, fdata);
  606.    x = center + w;
  607.    fv2[j2] = f2 = f(1, &x, fdata);
  608.    result_kronrod += wgk[j2] * (f1 + f2);
  609.    result_abs += wgk[j2] * (fabs(f1) + fabs(f2));
  610. }
  611. ee->val = result_kronrod * width;
  612. /* compute error estimate: */
  613. mean = result_kronrod * 0.5;
  614. result_asc = wgk[n - 1] * fabs(f_center - mean);
  615. for (j = 0; j < n - 1; ++j)
  616.    result_asc += wgk[j] * (fabs(fv1[j] - mean) + fabs(fv2[j] - mean));
  617. err = (result_kronrod - result_gauss) * width;
  618. result_abs *= width;
  619. result_asc *= width;
  620. if (result_asc != 0 && err != 0) {
  621.    double scale = pow((200 * err / result_asc), 1.5);
  622.    if (scale < 1)
  623.      err = result_asc * scale;
  624.    else
  625.      err = result_asc;
  626. }
  627. if (result_abs > DBL_MIN / (50 * DBL_EPSILON)) {
  628.    double min_err = 50 * DBL_EPSILON * result_abs;
  629.    if (min_err > err)
  630.      err = min_err;
  631. }
  632. ee->err = err;
  633. return 0;      /* no choice but to divide 0th dimension */
  634. }
  635. static rule *make_rule15gauss(unsigned dim)
  636. {
  637. rule *r;
  638. if (dim != 1)
  639.    return 0;    /* this rule is only for 1d integrals */
  640. r = (rule *) malloc(sizeof(rule));
  641. r->dim = dim;
  642. r->num_points = 15;
  643. r->evalError = rule15gauss_evalError;
  644. r->destroy = 0;
  645. return r;
  646. }
  647. /*
  648. *========================================================================
  649. * binary heap implementation (ala _Introduction to Algorithms_ by Cormen,
  650. * Leiserson, and Rivest), for use as a priority queue of regions to
  651. * integrate.
  652. *========================================================================
  653. */
  654. static void heap_resize(heap * h, unsigned nalloc)
  655. {
  656. h->nalloc = nalloc;
  657. h->items = (heap_item *) realloc(h->items, sizeof(heap_item) * nalloc);
  658. }
  659. static heap heap_alloc(unsigned nalloc)
  660. {
  661. heap h;
  662. h.n = 0;
  663. h.nalloc = 0;
  664. h.items = 0;
  665. h.ee.val = h.ee.err = 0;
  666. heap_resize(&h, nalloc);
  667. return h;
  668. }
  669. /*
  670. * note that heap_free does not deallocate anything referenced by the items
  671. */
  672. static void heap_free(heap * h)
  673. {
  674. h->n = 0;
  675. heap_resize(h, 0);
  676. }
  677. static void heap_push(heap * h, heap_item hi)
  678. {
  679. int insert;
  680. h->ee.val += hi.ee.val;
  681. h->ee.err += hi.ee.err;
  682. insert = h->n;
  683. if (++(h->n) > h->nalloc)
  684.    heap_resize(h, h->n * 2);
  685. while (insert) {
  686.    int parent = (insert - 1) / 2;
  687.    if (KEY(hi) <= KEY(h->items[parent]))
  688.      break;
  689.    h->items[insert] = h->items[parent];
  690.    insert = parent;
  691. }
  692. h->items[insert] = hi;
  693. }
  694. static heap_item heap_pop(heap * h)
  695. {
  696. heap_item ret;
  697. int i, n, child;
  698. if (!(h->n)) {
  699.    fprintf(stderr, "attempted to pop an empty heap\n" );
  700.    exit(EXIT_FAILURE);
  701. }
  702. ret = h->items[0];
  703. h->items[i = 0] = h->items[n = --(h->n)];
  704. while ((child = i * 2 + 1) < n) {
  705.    int largest;
  706.    heap_item swap;
  707.    if (KEY(h->items[child]) <= KEY(h->items[i]))
  708.      largest = i;
  709.    else
  710.      largest = child;
  711.    if (++child < n && KEY(h->items[largest]) < KEY(h->items[child]))
  712.      largest = child;
  713.    if (largest == i)
  714.      break;
  715.    swap = h->items[i];
  716.    h->items[i] = h->items[largest];
  717.    h->items[i = largest] = swap;
  718. }
  719. h->ee.val -= ret.ee.val;
  720. h->ee.err -= ret.ee.err;
  721. return ret;
  722. }
  723. /*
  724. *========================================================================
  725. * adaptive integration, analogous to adaptintegrator.cpp in HIntLib
  726. *========================================================================
  727. */
  728. static int ruleadapt_integrate(rule * r, integrand f, void *fdata,
  729.           unsigned dim, const hypercube * h, unsigned maxEval,
  730.           double reqAbsError, double reqRelError, esterr * ee)
  731. {
  732. unsigned initialRegions;  /* number of initial regions (non-adaptive) */
  733. unsigned minIter;    /* minimum number of adaptive subdivisions */
  734. unsigned maxIter;    /* maximum number of adaptive subdivisions */
  735. unsigned initialPoints;
  736. heap regions;
  737. unsigned i,j,ip;
  738. int status = -1;    /* = ERROR */
  739. if(debug(D_CUBATURE)){
  740.    printf("# ruleadapt_integrate():  Entry.\n" );
  741. }
  742. initialRegions = 1;    /* or: use some percentage of maxEval/r->num_points */
  743. initialPoints = initialRegions * r->num_points;
  744. minIter = 0;
  745. if (maxEval) {
  746.    if (initialPoints > maxEval) {
  747.      initialRegions = maxEval / r->num_points;
  748.      initialPoints = initialRegions * r->num_points;
  749.    }
  750.    maxEval -= initialPoints;
  751.    maxIter = maxEval / (2 * r->num_points);
  752. } else
  753.    maxIter = UINT_MAX;
  754. if (initialRegions == 0)
  755.    return status;    /* ERROR */
  756. /*
  757.   * Allocate a heap with room for a set of region evaluations
  758.   */
  759. regions = heap_alloc(initialRegions);
  760. /*
  761.   * Evaluate a region and push it onto the heap.
  762.   */
  763. if(debug(D_CUBATURE)){
  764.    printf("# ruleadapt_integrate(): Make hypercube into region and put it on heap\n" );
  765. }
  766. heap_push(&regions, eval_region(make_region(h), f, fdata, r));
  767. /* or:
  768.    if (initialRegions != 1)
  769.       partition(h, initialRegions, EQUIDISTANT, &regions, f,fdata, r);
  770. */
  771. /*
  772.   * Now we work to convergence or maxIter, whichever comes first
  773.   */
  774. for (i = 0; i < maxIter; ++i) {
  775.    region R, R2;
  776.    if (i >= minIter && (regions.ee.err <= reqAbsError
  777.            || relError(regions.ee) <= reqRelError)) {
  778.      status = 0;    /* converged! */
  779.      if(debug(D_CUBATURE)){
  780.        printf("# ruleadapt_integrate(): Converged!\n" );
  781.      }
  782.      break;
  783.    }
  784.    /*
  785.     * Get the worst region (which might be the only region)
  786.     */
  787.    R = heap_pop(&regions);  /* get worst region */
  788.    if(debug(D_CUBATURE)){
  789.      printf("# ruleadapt_integrate(): Worst hypercube is at h.center = (" );
  790.      for(ip = 0;ip<dim;ip++){
  791.        printf("%4.2f",R.h.center[ip]);
  792.        if(ip != dim-1) printf("," );
  793.      }
  794.      printf(" )\n" );
  795.    }
  796.    /* Split it along the working (worst) dimension */
  797.    cut_region(&R, &R2);
  798.    for(j = 0;j<R.h.dim;j++){
  799.      if(debug(D_CUBATURE)){
  800.        printf("# ruleadapt_integrate(): Split worst dimension, R max = %f  R2 max = %f\n",R.h.center[j]+R.h.width[j],
  801.           R2.h.center[j]+R2.h.width[j]);
  802.      }
  803.    }
  804.    /* printf("Split it into %0x and %0x\n",R.h.center,R2.h.center); */
  805.    /* Evaluate its left half */
  806.    if(debug(D_CUBATURE)){
  807.      printf("# # ruleadapt_integrate(): Evaluate left half onto heap.\n" );
  808.    }
  809.    heap_push(&regions, eval_region(R, f, fdata, r));
  810.    /* And its right half */
  811.    if(debug(D_CUBATURE)){
  812.      printf("# # ruleadapt_integrate(): Evaluate right half onto heap.\n" );
  813.    }
  814.    heap_push(&regions, eval_region(R2, f, fdata, r));
  815. }
  816. /*
  817.   * Re-sum the integral and errors for all the regions on the heap
  818.   * and then remove them (clearing the heap).
  819.   */
  820. ee->val = ee->err = 0;
  821. if(debug(D_CUBATURE)){
  822.      printf("# ruleadapt_integrate(): About to destroy %d regions.\n",regions.n);
  823. }
  824. for (i = 0; i < regions.n; ++i) {
  825.    /* printf("i=%d: adding value = %f, error = %f\n",i,regions.items[i].ee.val,
  826.      regions.items[i].ee.err); */
  827.    ee->val += regions.items[i].ee.val;
  828.    ee->err += regions.items[i].ee.err;
  829.    destroy_region(&regions.items[i]);
  830.    if(debug(D_CUBATURE)){
  831.      printf("# ruleadapt_integrate(): Destroyed region %d.\n",i);
  832.    }
  833. }
  834. if(debug(D_CUBATURE)){
  835.    printf("# ruleadapt_integrate(): freeing regions and done.\n" );
  836. }
  837. heap_free(&regions);
  838. return status;
  839. }
  840. int adapt_integrate(integrand f, void *fdata,
  841.         unsigned dim, const double *xmin, const double *xmax,
  842.         unsigned maxEval, double reqAbsError,
  843.         double reqRelError, double *val,
  844.         double *estimated_error)
  845. {
  846. rule *r;
  847. hypercube h;
  848. esterr ee;
  849. if (dim == 0) {    /* trivial integration */
  850.    ee.val = f(0, xmin, fdata);
  851.    ee.err = 0;
  852.    return 0;
  853. }
  854. if(debug(D_CUBATURE)){
  855.    printf("# Debugging routines in cubature.c\n" );
  856. }
  857. /*
  858.   * Use rule15gauss for 1d integrals, rule75genzmalik for d>1
  859.   */
  860. r = dim == 1 ? make_rule15gauss(dim) : make_rule75genzmalik(dim);
  861. if(debug(D_CUBATURE)){
  862.    printf("# adapt_integrate(): Make a ranged hypercube over integration volume.\n" );
  863. }
  864. h = make_hypercube_range(dim, xmin, xmax);
  865. if(debug(D_CUBATURE)){
  866.    printf("# adapt_integrate(): calling ruleadapt_integrate with this hypercube.\n" );
  867. }
  868. /*
  869.   * We need to pass dim for debugging only
  870.   */
  871. int status = ruleadapt_integrate(r, f, fdata, dim, &h,maxEval, reqAbsError,
  872.     reqRelError, &ee);
  873. if(debug(D_CUBATURE)){
  874.    printf("# adapt_integrate(): Done.  Cleaning up.\n" );
  875. }
  876. *val = ee.val;
  877. *estimated_error = ee.err;
  878. destroy_hypercube(&h);
  879. destroy_rule(r);
  880. return status;
  881. }


Message édité par gilou le 04-02-2011 à 15:58:07
Reply

Marsh Posté le 04-02-2011 à 16:29:48    

Bon au fait, ça plante pour quelle type de valeur.
Parce que comme dans le code on a du static heap heap_alloc(unsigned nalloc)
Peut être qu'à un moment la pile va bouffer trop de mémoire, et je vois pas trop de tests de plus au niveau de l'alloc pour verifier que tout s'est bien passé :heink:  
A+,


---------------
There's more than what can be linked! --    Iyashikei Anime Forever!    --  AngularJS c'est un framework d'engulé!  --
Reply

Marsh Posté le 04-02-2011 à 16:52:43    

En fait, comme j'ai mentionné je suis pas un connaisseur en informatique et en programmation.  Déjà, les deux derniers codes (cubature.c et cubature.h) c'est pas mon oeuvre. j'ai trouvé c'est deux codes sur le net.
Commet je fais des tests au niveau de l'allocation ?

Reply

Marsh Posté le 04-02-2011 à 16:52:43   

Reply

Marsh Posté le 04-02-2011 à 19:20:48    

Heap_alloc fait en fait appel à heap_realloc qui fait appel à realloc:

Code :
  1. static void heap_resize(heap * h, unsigned nalloc)
  2. {
  3. h->nalloc = nalloc;
  4. h->items = (heap_item *) realloc(h->items, sizeof(heap_item) * nalloc);
  5. }

Si realloc échoue, ça va renvoyer NULL, et ça, ça devrait être testé, ce qui n'est pas le cas ici.
A+,


---------------
There's more than what can be linked! --    Iyashikei Anime Forever!    --  AngularJS c'est un framework d'engulé!  --
Reply

Sujets relatifs:

Leave a Replay

Make sure you enter the(*)required information where indicate.HTML code is not allowed