Question

I'm using PC Lint for the first time. I was "linting" my code, when PC Lint warns me about my while(TRUE).

This is what it says:

716: while(1) ... -- A construct of the form while(1) ... was found.

Whereas this represents a constant in a context expecting a Boolean, it may reflect a programming policy whereby infinite loops are prefixed with this construct. Hence it is given a separate number and has been placed in the informational category. The more conventional form of infinite loop prefix is for(;;).

I didn't understand this statement. Can anyone help me to understand it?

Était-ce utile?

La solution

The text says that although while(TRUE) (which gets preprocessed into while(1)) is a perfectly valid infinite loop, the more conventional form of writing an infinite loop is

for(;;)
{
   ...
} 

because it doesn't use any values at all and therefore is less error-prone.

Autres conseils

It says that the more conventional infinite loop is for(;;), which I'd say is an arguable assertion, and that it's categorized this construct as an "informational category" finding - I suspect that if you used the for(;;) instead it would go away. I've always written these as while(1) and never for(;;) myself. If it does what you expect, I'd ignore the findings of PC LINT on this one or switch it if you're worried about someone redefining TRUE because if someone redefined TRUE your loop wouldn't run at all.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top