[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: NEW & extending redefinition



> I recently noticed that NEW(v,n) doesn't check that `n' is a valid
> length, or that it was actually able to allocate the required amount
> of heap space.  This raises two questions:
> 
> a) What is a valid length?  A negative number is illegal and should
> abort the program like any other failed run-time check.  But is a
> length of zero allowed?

Well, if we are ever going to have an Oberon-X extension to OOC (I hope),
then they use zero length arrays in place of NIL because they no longer
have a concept of pointers to arrays.  Obviously, a zero length means 
the array just has a header with no contents.  So, access to any 
elements would trigger either a runtime or compile time exception.

Perhaps this behaviour could be one of the "extensions" you mention
below.
 
> Example:
>   VAR v: POINTER TO ARRAY OF CHAR;
>   ...
>   NEW (v, 0)
> 
> Should this trigger a run-time check?  Without pathological 0-length
> arrays all iterations over open arrays are known to be executed at
> least once (i.e., are REPEAT UNTIL loops), saving an extra test for
> loops like "FOR i := 0 TO LEN(a)-1 DO".

Either runtime or compile time check if the extensions are disabled.
 
> b) Should a failed NEW() return NIL or trigger a run-time check?  Most
> code simply assumes that NEW will always bring back a new pointer, so
> implementing the second alternative might be better.

I prefer a NIL pointer being returned.