Hey, I seem to run into a problem with variables every now and then. Generally, when I run a loop and read from a file, I *might* get a problem.
The snippit of code I've narrowed it down to is below:
for l := 1 to 9 do
begin
write(l,' ');
blockread(f,bi,4); //bug here
end;
whereas l and bi are both integers, and f is a file opened for reading. Logically, this would give the output "1 2 3 4 5 6 7 8 9", yet, instead it gives the output "1 1 1 1 1".
L and BI are both recycled variables. Here's the kicker. LL is also a recycled variable, and if I switch L with LL, I get "1 5 5 5 ...... 1 1 1 .......<endless loop of 1's>".
POS is another recycled variable, and if I substitute that in, I get the desired result.
I can also change up the BI variable and get all kinds of strange results. I've seen it do "0 0 0".
I use recycled variables all the time, and when I do get a problem, it's normally once every 5 or 6 programs, and it's very similar to this. Making a new variable does solve the problem, but it's kind of a very last resort.
I don't know if you want to see the whole code or not. It's 500 lines, although a lot of it has been commented out to isolate the problem. It's open source, so I have no problem giving it to anyone, along with a sample file or 2 that it is used to read from.
If anyone could explain why my variables aren't acting the way I have explicitly told them to, it would be greatly appreciated.
Thanks
-Ism
Dev-Pascal, Latest version. Compiles with Free Pascal. Successfully compiles. Changing compiler options has no effect on the outcome apart from the obvious.
The snippit of code I've narrowed it down to is below:
for l := 1 to 9 do
begin
write(l,' ');
blockread(f,bi,4); //bug here
end;
whereas l and bi are both integers, and f is a file opened for reading. Logically, this would give the output "1 2 3 4 5 6 7 8 9", yet, instead it gives the output "1 1 1 1 1".
L and BI are both recycled variables. Here's the kicker. LL is also a recycled variable, and if I switch L with LL, I get "1 5 5 5 ...... 1 1 1 .......<endless loop of 1's>".
POS is another recycled variable, and if I substitute that in, I get the desired result.
I can also change up the BI variable and get all kinds of strange results. I've seen it do "0 0 0".
I use recycled variables all the time, and when I do get a problem, it's normally once every 5 or 6 programs, and it's very similar to this. Making a new variable does solve the problem, but it's kind of a very last resort.
I don't know if you want to see the whole code or not. It's 500 lines, although a lot of it has been commented out to isolate the problem. It's open source, so I have no problem giving it to anyone, along with a sample file or 2 that it is used to read from.
If anyone could explain why my variables aren't acting the way I have explicitly told them to, it would be greatly appreciated.
Thanks
-Ism
Dev-Pascal, Latest version. Compiles with Free Pascal. Successfully compiles. Changing compiler options has no effect on the outcome apart from the obvious.