[fpc-pascal] array [boolean] and typecasting fail

ik idokan at gmail.com
Fri Oct 23 16:55:35 CEST 2009


The use of 'array[boolean]' does not mean that the array is true or false,
but rather '0..1'. You indicate a range, not a boolean behavior.
"6 and 4" gives 4, and 4 is above the range of "1" (the highest cell of the
array you declared). If you would have cast it to boolean, or provide a
better usage, for example:

6 and 4 = 4

Then it would have worked as expected.

Ido

http://ik.homelinux.org/


On Fri, Oct 23, 2009 at 3:00 AM, David Emerson <dle3ab at angelbase.com> wrote:

> It appears that, in a special case here, the boolean typecast is not
> being done properly/completely, and thus an array is being indexed out
> of bounds. If I compile with rangechecking, I get a rangecheck error;
> without, I get garbage and an EAccessError exception! (trash and crash)
>
> uses math;
>
> type
>  rounding_func = function (x : extended) : longint;
>
> const
>  t_or_f : array [boolean] of string = ('false', 'true');
>  ceil_or_floor : array [boolean] of rounding_func = (@floor, @ceil);
>
> var
>  b : boolean;
>
> begin
>  b := boolean (6 and 4);
>  writeln (t_or_f [b]);
>  writeln (ceil_or_floor [b] (2.5));
> end.
>
> I was previously using boolean (6 and 4) as the array index directly,
> but I thought the example was easier to read this way, and it still
> crashes for me. Using fpc 2.2.4-3 and (mostly) debian 5.0.3 stable
>
> This smells like a bug to me.
>
> Cheers,
> David.
>
> _______________________________________________
> fpc-pascal maillist  -  fpc-pascal at lists.freepascal.org
> http://lists.freepascal.org/mailman/listinfo/fpc-pascal
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20091023/9947d9e3/attachment.html>


More information about the fpc-pascal mailing list