Lord KJWilliams
Member
I wrote a program long ago, in C that detects and exits infinite loops in my programming, if they happen.
For instance, in order to detect a infinite loop, I use a time counter to count the duration in seconds, of where a loop can happen. I start the counter before the loop starts, and update the counter in the loop in each iteration. If the counter exceeds the number of seconds, it exits it, using either abort(), break, or goto to a label. In GML you dont have labels ( e.g. part1: ) , abort(), or goto ( which is used with labels ). You have break in GML, but if the invention of break is based on C or C++ invention of break, then you can only break outside of one loop. If you have a nested for loops, a break will only break to the the next for loop, which where the only few valid uses of goto works in C. The use of goto is sneered at by C programmers in that community, in most cases where it leads to spaghetti code ( which is a nightmare from my use in Commodore Business Machines BASIC ).
In my code below , I use a platform specific function called delay() which delays the code using a count in CPU clock cycles. Using CPU clock cycles allows a more tight control of the program since CPU clock cycles are smaller than seconds.
In this program the anti_hang interrupts the do..while loop before it reaches 5000, and delays each iteration of the loop by 10 CPU clock cycles. GML should have a function that uses a delay function based on CPU processing cycles.
( Written using DJGPP for MS-DOS )
Another invention of mine is a count down , using a do..while loop, which uses goto and a label.
This is similar to the behavior of how a bomb's counter count downs before it explodes.
In this example of programming , I can set up my code so that if there is an infinite loop, the program can break out of it if the user does not press a key. But in GML there is no provision for breaking out of a nested loop.
Lets say I have a nested loop and I want to get out of the deepest loop that is inside
Psuedocode for a counter of time:
Any help appreciated,
Thanks
For instance, in order to detect a infinite loop, I use a time counter to count the duration in seconds, of where a loop can happen. I start the counter before the loop starts, and update the counter in the loop in each iteration. If the counter exceeds the number of seconds, it exits it, using either abort(), break, or goto to a label. In GML you dont have labels ( e.g. part1: ) , abort(), or goto ( which is used with labels ). You have break in GML, but if the invention of break is based on C or C++ invention of break, then you can only break outside of one loop. If you have a nested for loops, a break will only break to the the next for loop, which where the only few valid uses of goto works in C. The use of goto is sneered at by C programmers in that community, in most cases where it leads to spaghetti code ( which is a nightmare from my use in Commodore Business Machines BASIC ).
In my code below , I use a platform specific function called delay() which delays the code using a count in CPU clock cycles. Using CPU clock cycles allows a more tight control of the program since CPU clock cycles are smaller than seconds.
In this program the anti_hang interrupts the do..while loop before it reaches 5000, and delays each iteration of the loop by 10 CPU clock cycles. GML should have a function that uses a delay function based on CPU processing cycles.
( Written using DJGPP for MS-DOS )
Code:
// anti-hang function breaks out of continuous loops
#include <stdio.h>
#include <time.h>
#include "t_delay.h"
// global variable
unsigned char kjw_bailout = 1;// in minutes
int anti_hang(char);
int main(void)
{
int x = 0;
anti_hang(0);// initialize anti_hang before loop ( or loop nest )
do
{
//x++;
//printf("%04d\r",x);
time_delay(10);
printf("I cant break out..\n");
// this is where anti_hang works
if( (anti_hang(1)) == 1) { break; }
} while (x <= 5000);
printf("\n");
printf("..now anti_hang stopped the endless loop..", x);
return 0;
}
// loop breaker
int anti_hang(char x)
{
static int start_time, finish_time;// start and ending time
// set up time variables
static time_t t;
static struct tm *s;
static int timediff = 0;
// Get the starting time from the computers clock
if (x == 0)
{
t = time(NULL); /* gets time of day */
s = localtime(&t); /* converts date/time to a structure */
start_time = (int)s->tm_min;// get starting minutes of the hour
}
// update the difference
if (x == 1)
{
t = time(NULL);
s = localtime(&t);
finish_time = (int)s->tm_min; // get ending minutes of the hour
// get the difference time in minutes
if (start_time > finish_time)
{ timediff = finish_time + (60 - start_time); }
else
{ timediff = finish_time - start_time; }
}
// determine whether a loop is not breaking by comparing the
// timediff with kjw_bailout value
if(timediff >= kjw_bailout) { return 1; }
// if not then return 0
return 0;
}
This is similar to the behavior of how a bomb's counter count downs before it explodes.
Code:
#include <stdio.h>
#include <conio.h>
#include <dos.h>
int main(void)
{
double x = 10.00;//user defined delay
do
{
printf("%05.2lf...\r",x);//temp
delay(50);
x -= 0.01;
// the only acceptable instance of using goto
if(x < 0) { goto getout; }
} while(!kbhit());
printf("....countdown aborted\n");//temp
return 1;
getout: ;
printf("The timer ran out\n");//temp
return 0;
}
Lets say I have a nested loop and I want to get out of the deepest loop that is inside
Psuedocode for a counter of time:
Code:
int x,y,z,a;
a = 0;// this represents a condition if the loop is detected as a infinite loop.
//hours
for(x=0;x<10;x++)
{
//minutes
for(y=0;y<10;y++)
{
//seconds - this is the deepest loop
for(z=0;z<10;z++)
{
delay(10);// each second waits for 10 CPU clock cycles
// if a = 0, then its true
if (a) { goto getout; }
}
}
}
getout: ;
- How would you get out of a nested loop in GML, if you only have break?
- How would you detect and exit a inifinite loop ?
Any help appreciated,
Thanks