vxl

BCS BCS at pathlink.com
Wed May 17 11:43:44 PDT 2006


Tom S wrote:
> sclytrack at pi.be wrote:
> 
>> On another note, I still have a question about D. D does range check 
>> on arrays
>> data[x] = 2;
>> Is it possible to compile this without the range checking. Or do I have
>> to revert to int * data to avoid those?
> 
> 
> Just compile with -release :)
> 
> 

Mixing and matching range checks might be useful. Some sort of per op 
suppression would be enough.

Here is an example where this would be useful. It changes a node in an 
array based tree and updates subtree sums. Most of the array accesses 
are safe but hard to automatically prove (probably wont be optimized).


struct node
{
	int v;
	int sum;
}

int fn(node[] arr, int i, int v)
{
	int sum;

	// range check this (exception on error desired)
	arr[i].v = v;

	sum = v;

	if(i<<1 < arr.length)
		// don't check this (I already did)
		sum += arr[i<<1].v;

	if((i<<1)|0x01 < arr.length)
		// don't check this (I already did)
		sum += arr[(i<<1)|0x01].v;

	// don't check this (i hasn't  changed)
	arr[i].sum = sum;

	i>>=1;
	
		// don't check anything else, i is always valid
	while(i>0)
	{
		sum = arr[i].v;

		if(i<<1 < arr.length)
			sum += arr[i<<1].v;

		if((i<<1)|0x01 < arr.length)
			sum += arr[(i<<1)|0x01].v;

		arr[i].sum = sum;

		i>>=1;
	}

	return arr[1].sum;
}



More information about the Digitalmars-d mailing list