[GRASS5] G3D code question

Glynn Clements glynn at gclements.plus.com
Fri Sep 2 17:48:28 EDT 2005


Soeren Gebbert wrote:

> grass6/lib/g3d/cache.c:329  :
>   "cache_queue_enqueue (c, -1, index);"
> 
> grass6/lib/g3d/cache.c:264  :
>   "if (IS_NOT_IN_QUEUE_ELT (left))"
> 
> left is -1 and the macro checks an array:
>   "(c->locks[elt] == 1)"
> left == elt
> 
> Is this a magic trick?
> "c->locks[-1] == 1" ?
> Im not completly sure, but addressing an array with -1 is not usual and
> may result in a segfault?

In general, there's no rule to prohibit negative indices, but the
resulting address must be valid, i.e. the pointer must point to some
array element after the first. E.g. the lookup tables used by the
is*() functions in <ctype.h> usually do something like:

	static const unsigned int _table[128+256];
	const unsigned int *table = table + 128;

to allow for passing a signed char.

However, that doesn't apply to the specific code which you're dealing
with, where the locks field points to the beginning of a heap block. 
The (left == -1) check should probably be done first.

-- 
Glynn Clements <glynn at gclements.plus.com>




More information about the grass-dev mailing list