[GRASS5] v.extract problems

Glynn Clements glynn.clements at virgin.net
Fri Apr 19 19:43:10 EDT 2002


rgrmill at rt66.com wrote:

> 
> I think the change you made is a good one, but I found another problem 
> that might not be related.  I ran the updated code against a vector file 
> with more than 10,000 categories and tried to extract subsets ranging 
> from 11 to more than 8,000 categories and consistently got seg faults.  
> The problem always happens at line 272 in main.c.

Well, I changed the loop logic from:

	   cat_count = 0;
	   while(cat_array[cat_count])
	     {
	       G_set_cat( cat_array[cat_count], G_get_cat(cat_array[cat_count],
							  &cats), &temp_cats );
	       cat_count++;
	     }

to:
	for (i = 0; i < cat_count; i++)
	    G_set_cat(cat_array[i], G_get_cat(cat_array[i],&cats), &temp_cats);

AFAICT, every element of cat_array between 0 and cat_count-1 is
guaranteed to have been assigned by a prior call to call to add_cat()
so, if any of the values are bogus, the problem is in the
initialisation.

I can't see where, in the original code, an element would be
explicitly set to zero. The array is initialised to zeroes by virtue
of being static data, so the first unused element would be zero. So
looping (i = 0; i < cat_count; ...) should have the same effect unless
an explicit zero was added with add_cat. That would to terminate the
list prematurely, which doesn't make much sense to me.

Also, I've just noticed what appears to be another problem with the
original code. In the fourth case (file of category numbers), each
supplied range overwrites the beginning of the array, rather than
being appended to it:

         while (1)
           {
           if (!fgets (buffr, 39, in)) break;
           sscanf (buffr, "%[a-zA-Z., -_/$%@!#0-9]", text); 
	   	/*sscanf (buffr, "%s", text); */
		/*scan %s stops at whitespace?*/
           scan_cats (text, &x, &y);
-->	   cat_index = 0;
           while (x <= y)
              {
              cat_array[cat_index] = x++; cat_index++; 
              } 
           }

> The problem might be related to skipped values in the category list, but 
> I can't confirm that.

Note that there is a double indirection here. The elements of
cat_array are the categories; the indices into cat_array *aren't*
themselves categories. E.g. if you used:

	v.extract list=1-3,5-7 ...

then cat_array would be {1,2,3,5,6,7} and cat_count would be 6.

In any case, I'll have another look at in the morning.

-- 
Glynn Clements <glynn.clements at virgin.net>



More information about the grass-dev mailing list