Hi, all
Having fun with Schema; thanks, Frank!
I mentioned yesterday that I was getting a compiler warning when I used Cards rather than Integers for Schema, and Frank wanted to know more. Below is a (remarkably familiar) test program.
When I compile the program, I get the warning: Warning: comparison is always "True" due to limited range of data type for the three "i" loops. Oddly enough, the "j" and "k" loops get no warning, though in other similar programs I've had warnings for each of nested loops.
The medCard variables do not produce warnings when changed to shortCard, nor does the shortCard variable produce an error if it is changed to medCard. As you might guess, byteCard produces the same warning as shortCard.
regards, Toby
----------------------------------- program testmem;
type Fieldary(sY, sZ : medCard) = array[1..sY,1..sZ] of Real; FPary(sX : medCard) = array[0..sX] of ^Fieldary;
var Field : ^FPary; i, j, k : byteCard; nx, ny, nz : medCard;
begin write('Enter sizes in x, y, and z: '); readln(nx, ny, nz); New(Field, nx); for i := 0 to nx do New(Field^[i], ny, nz); writeln('Memory allocated OK'); for i := 0 to nx do for j := 1 to nx do for k := 1 to nx do Field^[i]^[j,k] := 1.0 * i * j * k; writeln('Freeing memory...'); for i := 0 to nx do Dispose(Field^[i]); Dispose(Field); end.
Toby Ewing wrote:
I mentioned yesterday that I was getting a compiler warning when I used Cards rather than Integers for Schema, and Frank wanted to know more. Below is a (remarkably familiar) test program.
... except last time you said "Card" which I interpreted as `Cardinal'. If you had mentioned the actual types you mean, it would have been more useful to me ...
When I compile the program, I get the warning: Warning: comparison is always "True" due to limited range of data type for the three "i" loops. Oddly enough, the "j" and "k" loops get no warning, though in other similar programs I've had warnings for each of nested loops.
It's because of the 1. At the start of the loop, the compiler compares the upper and lower bounds (only if <=, the loop is executed). So we have `0 <= nx' and `1 <= nx'. The first is always true. GPC warns for always true comparisons when written explictly by the user, but not in this case (but rather just skip the test). A slight bug made it still warn in some strange cases (which indeed depend on the types involved). Here's a fix (toby1.pas).
Frank
Frank wrote:
It's because of the 1. At the start of the loop, the compiler compares the upper and lower bounds (only if <=, the loop is executed). So we have `0 <= nx' and `1 <= nx'. The first is always true. GPC warns for always true comparisons when written explictly by the user, but not in this case (but rather just skip the test). A slight bug made it still warn in some strange cases (which indeed depend on the types involved). Here's a fix (toby1.pas).
BTW, is there any switch to turn this warning off (even in cases where it is appropriate)?
Emil
Emil Jerabek wrote:
Frank wrote:
It's because of the 1. At the start of the loop, the compiler compares the upper and lower bounds (only if <=, the loop is executed). So we have `0 <= nx' and `1 <= nx'. The first is always true. GPC warns for always true comparisons when written explictly by the user, but not in this case (but rather just skip the test). A slight bug made it still warn in some strange cases (which indeed depend on the types involved). Here's a fix (toby1.pas).
BTW, is there any switch to turn this warning off (even in cases where it is appropriate)?
No special switch for this warning -- just the general `{$W-}'.
Frank