Forum > Beginners
For variable being read after loop... Any way to get an error or warning on it?
DarioGL:
We are facing an issue and its that in some parts of our legacy code the variable of a for loop is consulted after the loop ends normally. This has an undetermined behaviour (as the for variable is undefined after the loop).
Is there a way to enable a warning/error/hint so it warns us of this?
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---var i, res: Integer;begin res := i; // Gives warning for i := 0 to 10 do ; res:= i; // No warning
MarkMLl:
--- Quote from: DarioGL on July 31, 2024, 02:32:51 pm ---We are facing an issue and its that in some parts of our legacy code the variable of a for loop is consulted after the loop ends normally. This has an undetermined behaviour (as the for variable is undefined after the loop).
Is there a way to enable a warning/error/hint so it warns us of this?
--- End quote ---
Submit a bug report. The problem is that in the example you gave the control variable i was defined (by Wirth) as having an undefined value, while if there was a break in it (added by Borland post-Wirth) then i would have a defined value.
My own opinion is that this presents a strong argument for being able to define the control variable in a local scope:
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---var res: Integer;begin for i: integer := 0 to 10 do ; res:= i; // Error here
however there would still be no error on
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---var i, res: Integer;begin res := i; // Gives warning for i: integer := 0 to 10 do ; res:= i; // No error here
since Pascal is quite happy with variables that hide others of the same name in an outer scope.
Put another way: I sympathise, even if I can be of little help.
MarkMLl
Eugene Loza:
I don't think it's easy to give a warning there, because sometimes the loop variable can be properly defined after the loop. The documentation states that:
--- Quote ---The value of the loop variable is undefined after a loop has completed or if a loop is not executed at all. However, if the loop was terminated prematurely with an exception or a break or goto statement, the loop variable retains the value it had when the loop was exited.
--- End quote ---
I.e. loop variable can be defined under some circumstances and it's not always so easy to tell if those circumstances always take place or not.
I must admit I'm not a big fan of such uncertainty too, but I believe there are several places where things like that can happen and the most reliable way is to audit&refactor all of them manually.
I.e. you can find all "for" loops in your files (in Lazarus Shift+Ctrl+F or Menu->Search->Find in Files; note "Whole words only" checkbox) and make sure that the variable is not used after the loop (again just searching may help to speed up the process; also Lazarus has a convenient "highlight" feature, just select the loop variable name and it will highlight it in the remaining code; note that the latter won't work too well if the loop variable name is short like "i" as highlighting doesn't have "words only" checkbox (or I couldn't find it) and hence will highlight all "i"s in all words on the screen, making it harder to read).
marcov:
--- Quote from: Eugene Loza on July 31, 2024, 03:36:08 pm ---I don't think it's easy to give a warning there, because sometimes the loop variable can be properly defined after the loop. The documentation states that:
--- End quote ---
If you have a for without gotos or break, you can mark the variable as unitialized after the loop, and the normal warning system should take over.
Eugene Loza:
--- Quote ---you can mark the variable as unitialized
--- End quote ---
Oh, that would be a good thing for myself too. I wonder how to do that? I know about Initialize(...) and Default(...) from System, but couldn't find anything like Uninitialize or Deinitialize.
Or do you mean Finalize(...)? For some reason I've thought it leaves the variable initialized. Just checked and it doesn't seem to create compiler warnings for using the variable after Finalize.
Navigation
[0] Message Index
[#] Next page