[comp.lang.pascal] Re^2: Control variables in FOR loops

leif@ambone.UUCP (Leif Andrew Rump) (01/18/89)

dik@cwi.nl (Dik T. Winter) writes:
>In article <00056@meph.UUCP> gsarff@meph.UUCP (Gary Sarff) writes:
> > In article <4267@okstate.UUCP> garnett@a.cs.okstate.edu (John Garnett)
> > >Isn't it also true that the value of 'i' will be undefined following the
> > >termination of the loop contained in procedure 'barf'?
> > 
> > Depends on how the compiler makes 'i' undefined, if there is some way to
> > put a NaN (not a number) in i for example.
>But a compiler is not required to take action to make 'i' undefined.
>Undefined is just that: the standard does not predict the value present

It will ease debugging if the runtime system is able to detect 'NaN'.
A lot of computers use two complement meaning (in a 16 bit
environment) that the range goes from -32768 to 32767. Use -32768 as
'Nan' and you have a 'normal' range -32767 to 32767...

 Leif Andrew Rump, AmbraSoft A/S, Roejelskaer 15, DK-2840 Holte (Denmark)
 UUCP: leif@ambone.dk, phone: +45 2424 111, touch phone: +45 422 817 + 313

   > > > Why are tall Irish girls with red hair so wonderful ? ? ? < < <

abcscnuk@csuna.UUCP (Naoto Kimura) (01/24/89)

In article <449@ambone.UUCP> leif@ambone.UUCP (Leif Andrew Rump) writes:
>dik@cwi.nl (Dik T. Winter) writes:
>>In article <00056@meph.UUCP> gsarff@meph.UUCP (Gary Sarff) writes:
>> > In article <4267@okstate.UUCP> garnett@a.cs.okstate.edu (John Garnett)
>> > >Isn't it also true that the value of 'i' will be undefined following the
>> > >termination of the loop contained in procedure 'barf'?
>> > 
>> > Depends on how the compiler makes 'i' undefined, if there is some way to
>> > put a NaN (not a number) in i for example.
>>But a compiler is not required to take action to make 'i' undefined.
>>Undefined is just that: the standard does not predict the value present
>
>It will ease debugging if the runtime system is able to detect 'NaN'.
>A lot of computers use two complement meaning (in a 16 bit
>environment) that the range goes from -32768 to 32767. Use -32768 as
>'Nan' and you have a 'normal' range -32767 to 32767...
>
> Leif Andrew Rump, AmbraSoft A/S, Roejelskaer 15, DK-2840 Holte (Denmark)
> UUCP: leif@ambone.dk, phone: +45 2424 111, touch phone: +45 422 817 + 313
>
>   > > > Why are tall Irish girls with red hair so wonderful ? ? ? < < <

If you turn on range checking, -32768 was 'NaN' in 3.0.  All TP has to
do is put that value into the index variable once the loop is exited.
But it would only detect the problem when range checking was turned on.

I'm not sure what 4.0 and 5.0 say about -32768 though (haven't tried it
yet).  I ran into the -32768 illegal integer value accidentally with 3.0
when I was trying to allocate 64Kbyte arrays.  When I tried to access
the 32768th element by using -32768 as the index, my program crashed.
Turning off the range checking "fixed" the problem.  Finally decided to
screw trying to allocate 64Kbyte arrays (went and allocated the array as
2 32K byte arrays), since I could really only allocate 64K-1 byte sized
arrays, and didn't want to bother fiddling with the memory allocation
routines to make sure that I wasn't doing screwy things (like modify a
1-byte sized variable that might follow the array in memory) when I
tried to access the 64Kth byte.

                //-n-\\				Naoto Kimura
        _____---=======---_____			(csun!csuna!abcscnuk)
    ====____\   /.. ..\   /____====
  //         ---\__O__/---         \\	Enterprise... Surrender or we'll
  \_\                             /_/	send back your *&^$% tribbles !!

abcscnuk%CSUNA.UUCP@cunyvm.cuny.edu (Naoto Kimura) (01/25/89)

In article <449@ambone.UUCP> leif@ambone.UUCP (Leif Andrew Rump) writes:
>dik@cwi.nl (Dik T. Winter) writes:
>>In article <00056@meph.UUCP> gsarff@meph.UUCP (Gary Sarff) writes:
>> > In article <4267@okstate.UUCP> garnett@a.cs.okstate.edu (John Garnett)
>> > >Isn't it also true that the value of 'i' will be undefined following the
>> > >termination of the loop contained in procedure 'barf'?
>> >
>> > Depends on how the compiler makes 'i' undefined, if there is some way to
>> > put a NaN (not a number) in i for example.
>>But a compiler is not required to take action to make 'i' undefined.
>>Undefined is just that: the standard does not predict the value present
>
>It will ease debugging if the runtime system is able to detect 'NaN'.
>A lot of computers use two complement meaning (in a 16 bit
>environment) that the range goes from -32768 to 32767. Use -32768 as
>'Nan' and you have a 'normal' range -32767 to 32767...
>
> Leif Andrew Rump, AmbraSoft A/S, Roejelskaer 15, DK-2840 Holte (Denmark)
> UUCP: leif@ambone.dk, phone: +45 2424 111, touch phone: +45 422 817 + 313
>
>   > > > Why are tall Irish girls with red hair so wonderful ? ? ? < < <

If you turn on range checking, -32768 was 'NaN' in 3.0.  All TP has to
do is put that value into the index variable once the loop is exited.
But it would only detect the problem when range checking was turned on.

I'm not sure what 4.0 and 5.0 say about -32768 though (haven't tried it
yet).  I ran into the -32768 illegal integer value accidentally with 3.0
when I was trying to allocate 64Kbyte arrays.  When I tried to access
the 32768th element by using -32768 as the index, my program crashed.
Turning off the range checking "fixed" the problem.  Finally decided to
screw trying to allocate 64Kbyte arrays (went and allocated the array as
2 32K byte arrays), since I could really only allocate 64K-1 byte sized
arrays, and didn't want to bother fiddling with the memory allocation
routines to make sure that I wasn't doing screwy things (like modify a
1-byte sized variable that might follow the array in memory) when I
tried to access the 64Kth byte.

                //-n-\\                Naoto Kimura
        _____---=======---_____            (csun!csuna!abcscnuk)
    ====____\   /.. ..\   /____====
  //         ---\__O__/---         \\    Enterprise... Surrender or we'll
  \_\                             /_/    send back your *&^$% tribbles !!

mark@hpcllmr.HP.COM (Mark Rozhin) (02/04/89)

>Isn't it also true that the value of 'i' will be undefined following the
>termination of the loop contained in procedure 'barf'? 

this usually means you (the programmer) cannot count on any particular value,
rather than counting on the program bombing.

mr

mark@hpcllmr.HP.COM (Mark Rozhin) (02/04/89)

the HP Pascal compiler also reject you test case as follows:

page   1  HP PASCAL/UX HP92431A.05.01    COPYRIGHT HEWLETT-PACKARD CO. 1986
          FRI, FEB  3, 1989, 12:08 PM

    0     1.000   0   program nonsense(input,output);
    0     2.000   0       var
    0     3.000   0       i : integer;
    1     4.000   0
    0     5.000   0       procedure barf;
    1     6.000   1       begin
    1     7.000   1       for i := 1 to 10 do
                              ^
**** ERROR #  1   INVALID CONTROL VARIABLE IN FOR STATEMENT (230)
    2     8.000   1           write(i:3);
    3     9.000   1       write('   i=',i)     (****)
    4    10.000   1       end;
    4    11.000   0
    0    12.000   0       procedure ack;
    1    13.000   1       begin
    1    14.000   2       for i := 1 to 10 do begin
                              ^
**** ERROR #  2   INVALID CONTROL VARIABLE IN FOR STATEMENT (230)
    2    15.000   2           write('i=',i:2,' ');
    3    16.000   2           barf;
    4    17.000   2           writeln('i=',i:2)
    5    18.000   2         end
    5    19.000   1       end;
    5    20.000   0
    1    21.000   1   begin
    1    22.000   1       for i := 1 to 10 do
    2    23.000   1       ack
    3    24.000   1   end.
    3    25.000   0
                      NUMBER OF ERRORS =  2     NUMBER OF WARNINGS =  0
                      PROCESSOR TIME 0: 0: 1    ELAPSED TIME 0: 0: 1
                      NUMBER OF LINES =   25    LINES/MINUTE =  2459.0
                      NUMBER OF NOTES =    0

mr

mark@hpcllmr.HP.COM (Mark Rozhin) (02/04/89)

>It will ease debugging if the runtime system is able to detect 'NaN'.
>A lot of computers use two complement meaning (in a 16 bit
>environment) that the range goes from -32768 to 32767. Use -32768 as
>'Nan' and you have a 'normal' range -32767 to 32767...

very nice idea. kind of like IEEE floating point. but isn't there code
out there that relies on -32768?

mr

cperry@lakesys.UUCP (Christopher Perry) (02/05/89)

I have been using version 4 of Turbo Pascal (MS-DOS) and am very interested
in the new additions and changes that have been made with version 5.  Am 
curious if version 5 did more cleaning up of bugs that were present in 
version 4, or if version 4 was pretty clean yet they added new featuress
in version 5.  Replies will be helpful. Thanks in advance.  Chris

mark@hpcllmr.HP.COM (Mark Rozhin) (02/10/89)

>I've had the same problem with INVALID CONTROL VARIABLE using HP
>Pascal on the Hp3000 at school.  I think it doesn't follow along the
>scope rules for standard pascal.  If you declare I as local to the
>procedure, it works.

from the ISO standard:

   6.8.3.9 For-statements.

   "The control variable shall be an entire-variable whose identifier
   is declared in the variable-declaration-part of the block
   closest-containing the for-statement."

ANSI reads the same (by definition and if you look it up).

now if you go back to the second edition user report (by wirth), it
omits any restriction. is that what you mean by standard pascal?

m