MISRA Discussion Forums
Infinite loop - Printable Version

+- MISRA Discussion Forums (https://forum.misra.org.uk)
+-- Forum: MISRA C (https://forum.misra.org.uk/forumdisplay.php?fid=4)
+--- Forum: MISRA-C: 2004 rules (https://forum.misra.org.uk/forumdisplay.php?fid=17)
+---- Forum: 6.13 Control Statement Expressions (https://forum.misra.org.uk/forumdisplay.php?fid=40)
+---- Thread: Infinite loop (/showthread.php?tid=396)



Infinite loop - JonM - 12-09-2007

What is the preferred implementation of an infinite loop?

Code:
for ( ; ; ) ...

or

Code:
while (1 == 1) ...

or

something else?

Thanks in advance.

Jonathan


- misra-c - 26-09-2007

In the TC, we allow the for loop example with all three expressions being empty for a deliberate infinite loop.

Code:
/* deliberate infinite loop */
for ( ; ; )
{
   ...
}



- JonM - 27-09-2007

Yes I saw that it was allowed. Is while (1==1) also allowed? Which is preferred?


- George Brown - 09-01-2008

Hi Jon,

Looks like \"they\" said

Code:
/* deliberate infinite loop */
for ( ; ; )
{
   ...
}

was the allowed form is infinite loops. Given one way to write them, there is no need for alternatives.

I was taught Pascal back in the early 80's, and would like to write this in the following form. C99 defines a \"boolean\" type, so MISRA-C3 could be different!
Code:
/* deliberate infinite loop */
while(true)
{
   ...
}

George