Hi
1) p[h] defaults to zero
2) the value of h can exceed the array size
3) In BP 7, If you modify the index inside a for-loop the results are undefined After you exit the for-loop the index is undefined unless you exited with a goto
in C, You can mdify the index inside the for-loop The value of the index is visable after you exit the loop (ref K&R 2nd Ed, p61)
So what does the Pascal specs say? (I wish I had a copy of each)
Russ russwhit@mind.net ----------
From: Frank Heckenbach frank@g-n-u.de To: gpc@gnu.de Subject: Re: posible bug question... Date: Friday, May 19, 2000 10:17 AM
Richard Sharman wrote:
Frank Heckenbach writes: | Norberto Alfredo Bensa wrote: | The problem is the following: You use a for loop counter (h) after | the loop, and in Pascal this is undefined (this might be one of the | most despised features of Pascal, but it has its reasons). | Therefore, it's quite possible that one compiler does what you want | and another compiler (or another version of the same compiler) | doesn't. |
Isn't it also wrong to attempt to modify a FOR loop index variable inside the body for the FOR statement?
Yes, it is. I'd missed that one. (Using a while loop will avoid this problem as well, of course.)
for h := 1 to j-1 do if 2*p[h]>i then h := j { <------- } else if i mod p[h]= 0 then break;
I thought this was not allowed in Pascal, but I see gpc accepts it.
I agree, GPC should check this more strictly (at least give a warning).
Frank
-- Frank Heckenbach, frank@g-n-u.de, http://fjf.gnu.de/ GPC To-Do list, latest features, fixed bugs: http://agnes.dida.physik.uni-essen.de/~gnu-pascal/todo.html
On 20 May 2000, at 9:31, Russ Whitaker wrote:
So what does the Pascal specs say?
The Extended Pascal standard, section 6.9.3.9.1, says:
"After a for-statement is executed, other than being left by a goto-statement, the control-variable shall be undefined."
That's remarkably clear. :-) That means that variable "b" in the code below isn't guaranteed to be true:
procedure p; var i : integer; b : Boolean; begin for i := 1 to 10 do ; b := (i > 10); end;
The next part of the standard is a bit less clear:
"Neither a for-statement nor any procedure-and-function-declaration-part of the block that closest-contains a for-statement shall contain a statement threatening (see 6.9.4) a variable-access denoting the variable denoted by the control-variable of the for-statement."
I believe that says that you can't modify the control variable of a for- statement (a "threat" is defined in the standard as one of ten ways that a variable may be assigned a value). So this would clearly be illegal:
procedure p; var i : integer; begin for i := 1 to 10 do i := 11; end;
However, it also says that a block containing a for-statement cannot contain a threat to the control variable. In other words, this would be illegal too:
procedure p; var i : integer; begin i := 0; for i := 1 to 10 do end;
I suppose the reason is to disallow something like this:
procedure p; var i : integer; label 1, 2, 3; begin goto 1; 2: i := 11; goto 3;
1: for i := 1 to 10 do begin goto 2; 3: end end;
...which is functionally equivalent to the second code example.
So it would seem that to comply with the EP standard, GPC must prohibit any threat (i.e., variable assignment) to a for-statement control variable anywhere within the scope of that variable, and not just within the for- statement itself.
-- Dave