Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2009-10

array [boolean] and typecasting fail

5 messages · 3 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

David Emerson

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.

ik
to David Emerson

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/

David Emerson wrote 36 lines
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 - http://lists.freepascal.org/mailman/listinfo/fpc-pascal
David Emerson
to ik
ik wrote
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.

Well, pascal is a typed language, so while I do realize that the information is stored as false=0 and true=1, and that's how the array is indexed internally when I make such a declaration, I expect a pascal compiler to treat the boolean type intelligently. (It usually does :)

"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 [...]

I did cast it to boolean! That's why it smells like a bug.

Show quoted text 6 lines
> var > b : boolean; > > begin > b := boolean (6 and 4); > writeln (t_or_f [b]);

Cheers,
~David.

Martin
to David Emerson
David Emerson wrote 8 lines
I did cast it to boolean! That's why it smells like a bug >> var >> b : boolean; >> >> begin >> b := boolean (6 and 4); >> writeln (t_or_f [b]); >>

A type cast does not changes the value. A typecast merely allows you to store a value into a field of a different type

same for enums (boolean is an enum)

type t = (zero, one, two);
var a: t;
a := t(1); // ok
a := t(5); // compiles, gives rangecheck if enabled, otherwise works,
but a is not any of the enum-values
boolvar := boolean(4); // just puts 4 into the variable. doesn't make 4
a valid boolean value

So you need to do

  b := (6 and 4)  0; // that is a boolean value
  writeln (t_or_f [b]);

Martin

David Emerson
to Martin

Thanks, Martin -- I'm doing the if/then, and am being reminded of what typecasting actually does!

Martin wrote
A type cast does not changes the value. A typecast merely allows you to store a value into a field of a different type