Dynamic Out of Bounds checking for array operator.
- From: "Skybuck Flying" <BloodyShame@xxxxxxxxxxx>
- Date: Thu, 18 Dec 2008 05:15:15 +0100
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.
.
- Prev by Date: Re: Ideas/Help with code
- Next by Date: Re: Ideas/Help with code
- Previous by thread: Ideas/Help with code
- Next by thread: BDE upgrade to avoid 4gig problem
- Index(es):
Relevant Pages
|