[Forensim-help] r166 - pkg/src

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Sat Oct 31 22:22:42 CET 2009


Author: hindahaned
Date: 2009-10-31 22:22:42 +0100 (Sat, 31 Oct 2009)
New Revision: 166

Modified:
   pkg/src/recurs.c
Log:
Correction for an important bug involving pointers.

Modified: pkg/src/recurs.c
===================================================================
--- pkg/src/recurs.c	2009-10-31 17:17:47 UTC (rev 165)
+++ pkg/src/recurs.c	2009-10-31 21:22:42 UTC (rev 166)
@@ -1,35 +1,55 @@
 #include <R.h>
 #include <Rmath.h>
+//#define DEBUG_TOOL
 
+#ifdef DEBUG_TOOL
+	FILE *file;
+#endif
 
 void taballoc (double ***tab, int l1, int c1)
 {
     int i, j;
 
-    if ( (*tab = (double **) calloc(l1+1, sizeof(double *))) != 0) {
-        for (i=0;i<=l1;i++) {
-            if ( (*(*tab+i)=(double *) calloc(c1+1, sizeof(double))) == 0 ) {
+
+#ifdef DEBUG_TOOL
+	if( l1 <= 0 || c1 <= 0 )
+	{
+		fprintf(file, "allocation error with line %d column %d\n", l1, c1);
+	}
+#endif
+
+    if ( (*tab = (double **) calloc(l1/*+1*/, sizeof(double *))) != 0) {
+        for (i=0;i</*=*/l1;i++) {
+            if ( (*(*tab+i)=(double *) calloc(c1/*+1*/, sizeof(double))) == 0 ) {
+#ifdef DEBUG_TOOL
+				fprintf(file, "error no memory to alloc %d bytes\n", (c1+1)*sizeof(double));
+#endif
                 return;
-                for (j=0;j<i;j++) {
+                /*for (j=0;j<i;j++) {
                     free(*(*tab+j));
-                }
+                }*/
             }
         }
     }
+#ifdef DEBUG_TOOL
+	else
+	{
+		fprintf(file, "error no memory to alloc %d bytes\n", (l1+1)*sizeof(double *));
+	}
+#endif
 
-    **(*tab) = l1;
-    **(*tab+1) = c1;
-
+    //**(*tab) = l1;
+    //**(*tab+1) = c1;
 }
 
 
 
-void freetab (double **tab)
+void freetab (double **tab, int n)
 {
-    int     i, n;
+    int     i;//, n;
 
-    n = *(*(tab));
-    for (i=0;i<=n;i++) {
+    //n = *(*(tab));
+    for (i=0;i</*=*/n;i++) {
             free((char *) *(tab+i) );
     }
     free((char *) tab);
@@ -59,6 +79,12 @@
             a[ c - 1 ] = r - S;
             for(j=0;j<c;j++)
              {
+#ifdef DEBUG_TOOL
+                if( cpt < 0 || cpt >= nbLignes)
+                {
+                    fprintf(file, "error try to reach line %d although nbligne = %d\n", cpt + 1, nbLignes);
+                }
+#endif
                 matrice[ cpt][j]=a[j];
              }
             cpt++;
@@ -85,11 +111,18 @@
 
 void recurs(int *r, int *c, int *matR, int *nbLigne, int *nbCol)
 {
+#ifdef DEBUG_TOOL
+    //file = fopen("c:/debug.txt","ab");
+    file = fopen("/home/dibule/Recurs/debug.txt","ab");
+    fprintf(file, "begin debug recurs\n");
+#endif
+
     double **matrice;
 	taballoc(&matrice, *nbLigne, *nbCol);
     int *a = malloc(sizeof(int)*(*c));
     vide(a, *c);
 
+
     cpt = 0;
 	recurs2(*r, *c, 0, 0, a, matrice, *nbLigne);
 
@@ -106,8 +139,17 @@
 		    }
 		}
 
-    freetab(matrice);
+    freetab(matrice, *nbLigne);
     free(a);
+
+#ifdef DEBUG_TOOL
+    for( int i = 0; i < (*nbCol)*(*nbLigne); i++ )
+    {
+        fprintf(file, "%d ", matR[i]);
+    }
+    fprintf(file, "end debug recurs\n");
+    fclose(file);
+#endif
 }
 
 



More information about the Forensim-commits mailing list