[Gambas-devel] gb3: recent revision regarding FOR...NEXT loops and counter variables

Kevin Fishburne kevinfishburne at ...590...
Thu Sep 1 23:23:34 CEST 2011


On 09/01/2011 05:01 PM, Benoît Minisini wrote:
>> On 09/01/2011 07:35 AM, Benoît Minisini wrote:
>>>> Holy shit something is seriously wrong with a recent commit. All my
>>>> loops are going infinitely by wrapping around to negative values. Guess
>>>> no one's using the most recent build or they'd have noticed. Try
>>>> something like this in a module with no other code:
>>>>
>>>> ' Gambas module file
>>>>
>>>> Public Sub Main()
>>>>
>>>>      Dim Counter As Short
>>>>
>>>>      For Counter = 0 To 32767
>>>>
>>>>        Print Counter
>>>>
>>>>      Next
>>>>
>>>> End
>>> There was a bug fix in loop management.
>>>
>>> And your loop must loop indefinitely, because a short value wraps to
>>> -32768 after 32767, so the limit is never passed.
>>>
>>> The solution is not using a Short as loop variable.
>> I understand what it's doing now. The "final" Next overflows the
>> counter, making the loop infinite. While that makes sense, something
>> doesn't quite smell right. I presume this behavior goes for any integer
>> type, such as Byte, Integer, Long, etc.?
> Of course.

Would be nicer if it simply halted execution with an overflow error. 
Silent overflow could be used as a feature, but if it was unintentional 
it would be hard to find the error in logic if you didn't realize what 
was happening. The program would keep running but behave unexpectedly. 
Are people actually using this as a feature by intentionally overflowing 
variables?


-- 
Kevin Fishburne
Eight Virtues
www: http://sales.eightvirtues.com
e-mail: sales at ...590...
phone: (770) 853-6271





More information about the Devel mailing list