array !is null fails

Tue Feb 13 11:55:53 PST 2007

Frits van Bommel wrote:
> I'd argue that if you have an array with a non-zero length but null 
> pointer, you've already messed up somewhere. It should have been checked 
> at whatever place sliced the null pointer.

Yup. But then again asserts should be used to check stuff you otherwise 
can't control (incoming arguments and returns from 3rd party code etc.)

I go by the assumption that if an assert *ever* trips then, by 
definition, you have a bug (unless you are trying to trip it in a 
unittest or something).

Conditions that might happen without bugs should be handled with exceptions.

> (The only way to obtain such 
> an array would be either slicing a raw pointer or using pointer 
> casts/unions to access memory as a different type than it actually is, 
> right?)
> And it'd get even worse if you take a non-empty slice from such an array 
> that doesn't start at index 0. Then you have'd an array with non-zero 
> length and non-null pointer that is nevertheless very much invalid, and 
> without any way to detect it[1].

Maybe there should be a gc.IsValid(void[]) function that returns true if 
  the full length of the array is readable (or is GC allocated if the 
first is to hard).

I wonder what it would take to make asserts do that check when given an 

char[] c
assert(c); // is c "good"?

> [1]: That is, short of indexing it and detecting the segfault/access 
> violation caused, or resorting to system-dependent checks like "is it in 
> the first page of memory".

More information about the Digitalmars-d-learn mailing list