Dynamic Out of Bounds checking for array operator.



Hello,

Suppose the windows api says:

sometype *array; // array of sometype.
int elements; // elements in array.

Delphi has no way to access this via a dynamic array.

One reason could be scared of out of bounds situations and not detecting
those.

A trick could be added to the delphi language to give the programmer the
ability
to add the bounds at runtime... so that the compiler can still generate the
out of bounds
instructions... the only problem is the compiler does not yet know the
bounds.

Maybe delphi could include some instructions to modify other instructions...
or maybe this is not necessary
and the out of bounds checking/debug code could use variables in memory.

Therefore these variables in memory could be set by the programmer before
his code.

So for example:

// overloaded versions:
SetArrayBounds( Array, Elements ); // array elements for newbies. (-1
omitted)
SetArrayBounds( Array, 0, Elements-1 ); // for better programmers first
element, last element.

for vIndex := 0 to Elements-1 do
begin
Array[vIndex] := ...;
end;

This special trick/function would then set the bounds for the array... so
that the debugging code can check for more complex out of bound situations

for vIndex := SomeCalculationA to SomeCalculationB do
begin

end;

// or some variable access or even constant.
Array[vSomeIndex] := ...;
Array[-20] := ...;

If the programmer forgets to set the bounds then the compiler can even
detect this at compile time and ofcourse also at runtime...

At compile time because the SetArrayBounds was not found for the array.

At run time because the bounds are all ffffffffetc could be special value to
indicate it... this means this special value cannot be used as a range for
these kinds of arrays <- which could be a problem so:

Alternatively but a little bit more expensive would be a boolean indicating
if the bounds were initialized/set yes/no.

For release code these bounds might optionally even be omitted. Like
asserts.
Simply add an option to the project options to get rid of these bounds.

"Dynamic bounding" On/Off.

Sounds like a really good plan to me ! ;)

(Maybe the same could be done for pointers and pointer arithmetic but I'll
save that for another time :))

Bye,
Skybuck.


.



Relevant Pages

  • Re: Teaching new tricks to an old dog (C++ -->Ada)
    ... attributes helps both the writer and the compiler. ... > catch at compile time any out of boundaries access. ... C++ array like types don't have these properties. ... Maybe, if one gets used to template programming, the basis of the ...
    (comp.lang.ada)
  • Re: Teaching new tricks to an old dog (C++ -->Ada)
    ... attributes helps both the writer and the compiler. ... > catch at compile time any out of boundaries access. ... C++ array like types don't have these properties. ... Maybe, if one gets used to template programming, the basis of the ...
    (comp.lang.cpp)
  • Re: ruby programming best practice
    ... is there a way to find out these spots in my program, like a compiler do in Delphi/C. ... I will naturally assume arr is an array. ... In delphi, for example, such error is detected while compile, e.g.. ... I know Ruby works differently, but is there a way exists, like a normal compiler, or something like FxCop for the C# language? ...
    (comp.lang.ruby)
  • Re: working with byte arrays
    ... > pure Delphi - and Delphi is often virtually unbeatable, ... > it uses an optimizing compiler. ... And in addition to what J French said, if you go to Advanced Compile options ... and check "Remove Array Bounds check", then array access will be even faster ...
    (microsoft.public.vb.general.discussion)
  • Re: MonthCalendar with or without week-ends and week-ends being bold
    ... Weekdays should be loadable at compile time as a constant ... >> array. ... >> It is said that later Delphi supports international date standards. ... > linked in by the compiler as resource strings. ...
    (comp.lang.pascal.delphi.misc)