Browse Source

Better handling of NULL pointer

git-svn-id: https://svn.osgeo.org/grass/grass/trunk@45700 15284696-431f-4ddb-bdfa-cd5b030d7da7
Soeren Gebbert 14 years ago
parent
commit
977b8bc27a
2 changed files with 32 additions and 29 deletions
  1. 31 28
      raster3d/r3.stats/main.c
  2. 1 1
      raster3d/r3.stats/r3.stats.html

+ 31 - 28
raster3d/r3.stats/main.c

@@ -52,16 +52,16 @@ typedef struct
 } equal_val_array;
 } equal_val_array;
 
 
 /*prototypes */
 /*prototypes */
-equal_val_array *alloc_equal_val_array(int count);
-void free_equal_val_array(equal_val_array * vals);
-equal_val_array *add_equal_val_to_array(equal_val_array * array, double val);
-int check_equal_value(equal_val_array * array, double val);
-stat_table *create_stat_table(int nsteps, equal_val_array * values,
+static equal_val_array *alloc_equal_val_array(int count);
+static void free_equal_val_array(equal_val_array * vals);
+static equal_val_array *add_equal_val_to_array(equal_val_array * array, double val);
+static int check_equal_value(equal_val_array * array, double val);
+static stat_table *create_stat_table(int nsteps, equal_val_array * values,
 			      double min, double max);
 			      double min, double max);
-void free_stat_table(stat_table * stats);
-void print_stat_table(stat_table * stats);
-void update_stat_table(stat_table * stats, G3D_Region * region);
-void heapsort_eqvals(equal_val_array * data, int n);
+static void free_stat_table(stat_table * stats);
+static void print_stat_table(stat_table * stats);
+static void update_stat_table(stat_table * stats, G3D_Region * region);
+static void heapsort_eqvals(equal_val_array * data, int n);
 static void downheap_eqvals(equal_val_array * data, int n, int k);
 static void downheap_eqvals(equal_val_array * data, int n, int k);
 static void check_range_value(stat_table * stats, double value);
 static void check_range_value(stat_table * stats, double value);
 static void tree_search_range(stat_table * stats, int left, int right,
 static void tree_search_range(stat_table * stats, int left, int right,
@@ -546,14 +546,14 @@ int main(int argc, char *argv[])
 
 
     float val_f;		/* for misc use */
     float val_f;		/* for misc use */
     double val_d;		/* for misc use */
     double val_d;		/* for misc use */
-    stat_table *stats;
+    stat_table *stats = NULL;
     double min, max;
     double min, max;
-    equal_val_array *eqvals;
+    equal_val_array *eqvals = NULL;
 
 
     unsigned int n = 0, nsteps;
     unsigned int n = 0, nsteps;
     int map_type;
     int map_type;
-    char *infile;
-    void *map;
+    char *infile = NULL;
+    void *map = NULL;
     G3D_Region region;
     G3D_Region region;
     unsigned int rows, cols, depths;
     unsigned int rows, cols, depths;
     unsigned int x, y, z;
     unsigned int x, y, z;
@@ -661,18 +661,18 @@ int main(int argc, char *argv[])
 	    }
 	    }
 	}
 	}
 
 
-	/* sort the equal values array */
-	G_message(_("Sort non-null values"));
-	heapsort_eqvals(eqvals, eqvals->count);
+	if (eqvals) {
+            /* sort the equal values array */
+            G_message(_("Sort non-null values"));
+            heapsort_eqvals(eqvals, eqvals->count);
 
 
-	/* create the statistic table with equal values */
-	stats = create_stat_table(eqvals->count, eqvals, 0, 0);
-	/* compute the number of null values */
-	stats->null->count = rows * cols * depths - n;
-
-	if (eqvals)
-	    free_equal_val_array(eqvals);
+            /* create the statistic table with equal values */
+            stats = create_stat_table(eqvals->count, eqvals, 0, 0);
+            /* compute the number of null values */
+            stats->null->count = rows * cols * depths - n;
 
 
+            free_equal_val_array(eqvals);
+        }
     }
     }
     else {
     else {
 
 
@@ -710,11 +710,14 @@ int main(int argc, char *argv[])
 	stats->null->count = rows * cols * depths - n;
 	stats->null->count = rows * cols * depths - n;
     }
     }
 
 
-    /* Compute the volume and percentage */
-    update_stat_table(stats, &region);
-    /* Print the statistics to stdout */
-    print_stat_table(stats);
+    if(stats) {
+        /* Compute the volume and percentage */
+        update_stat_table(stats, &region);
+        /* Print the statistics to stdout */
+        print_stat_table(stats);
 
 
-    free_stat_table(stats);
+        free_stat_table(stats);
+    }
+    
     exit(EXIT_SUCCESS);
     exit(EXIT_SUCCESS);
 }
 }

+ 1 - 1
raster3d/r3.stats/r3.stats.html

@@ -9,7 +9,7 @@ If the flag <em>-e</em> is set, the number of subrange will be ignored.
 
 
 <H2>NOTES</H2>
 <H2>NOTES</H2>
 
 
-As with most GRASS raster3D modules, <EM>r3.univar</EM> operates on the cell
+As with most GRASS raster3D modules, <EM>r3.stats</EM> operates on the cell
 array defined by the current 3D region settings, not the original extent and
 array defined by the current 3D region settings, not the original extent and
 resolution of the input map. See <em><A HREF="g.region.html">g.region</A></em>.
 resolution of the input map. See <em><A HREF="g.region.html">g.region</A></em>.
 <P>
 <P>