While Loop Question
xrmichael
Posts: 79
This will probably be a dumb ? but i have seen several reference to an infinite loop, if you create this situation via incorrect code and the master sticks in the loop can you just resend another file. Or is it locked up once and for all.
0
Comments
Thanks, i can get creative now then, and try and make my rs232 command queue
ie:
while (find_string(sRxBuf,"$0D",1))
{
sReply = remove_string(sRxBuf,"$0D",1)
// more code here
}
This while loop can never run infinitely.
Paul
I've never run into this situation (not saying it isn't possible). I've had code get into infinite loops but I could still resend code.
While loops (and recursive functions for that matter) can be a powerful tool in your programming arsenal, you just have to use some logic to make sure that every pass through the loop will approach a state where the while condition is false. The loop posted by a_riot, a good example, will always approach a state where there is no $0D in the buffer because each pass through the loop removes the first instance of $0D from the buffer. Since we don't know beforehand how many $0D will be in the buffer, a while is more appropriate than, say, a for loop.
Weird - I have. Not sure what the circumstances are, but I think it was a WHILE loop.
Interesting. I wonder why my master wouldn't reboot or even accept a telnet connection when I accidentally misplaced a parenthesis that created an infinite while loop. Perhaps the moon was full that day.
Paul
First rule of programming: Never use recursion.
Second rule of programming: See rule number one.
That's obvious, but its easier said than done. Using an invariant you can mathematically prove a while loop will terminate. A nice-to-have in the 'arsenal'.
Paul
Using logic in programming IS easier said than done. But that shouldn't stop one from doing it.
No offense intended. What is not obvious about loops is how to achieve guaranteed loop termination in all circumstances regardless of what the loop does and that is where the loop invariant comes into play so I thought I would mention it. The OP wasn't implying that loops are incorrect code in and of themselves, but that through incorrect code, infinite loops can happen.
I also wanted to make the point that recursion isn't generally a good idea in programming and in fact most compilers will take a recursive function and convert into a while loop. The overhead for a recursive function is so large that invariably they overflow the call stack if you aren't careful.
Paul
All right... sorry about that then.
You make some good points. I had some memory issues with a system last week, stuff was getting allocated and not returned, and certain events (e.g. compiling with debug info) were pushing my NI-3000 over the edge. It got me thinking about strategy in memory management which, although I have programmed in many languages on many platforms, had never been a problem for me before.
We since ordered an NI-3100 to get more memory... but I still am reviewing the code to try to figure out how we can better make use of our resources.
I usually use recursive functions to calculate, or to do some type of unorthodox parsing. I remember once I had a function taking a DEV and a CHAR[] that would lop off the most significant digit, pulse that digit to the DEV, then after a wait, send the remainder of the string and the same DEV to itself (the function). (you can't put a stack var in a wait, but a function parameter is A-OK) So the process would keep going until there was just an empty string, in which case we'd pulse an Enter command and stop.
Of course then someone had to go and point out that this was built in via the XCH command. But this is the kind of thing I'd use a recursive function for.