Posts: 70
Threads: 8
Joined: Dec 2007
Reputation:
0
The rule says "tests of a value against zero". As I understand it, this applies to everything that isn't Boolean: integers, pointers, float numbers, strings...
Pointers are especially troublesome, since they aren't always containing address 0, but could also contain the mysterious address NULL, which could be defined as 0 or (void*)0. Using an implicity comparison can therefore cause your compiler to whine about implicit typecasting.
It is generally considered good programming practice to explicitly compare pointers against NULL, no matter what MISRA advises regarding implicit tests against zero.
<t></t>
Posts: 70
Threads: 8
Joined: Dec 2007
Reputation:
0
But you are testing against zero, rather than NULL. The difference is subtle, and only a matter of coding style. This cannot cause any problems, but it can perhaps cause implicit typecast warnings.
I interpret the rule as "everything that isn't Boolean should be explicitly tested". Consider this:
if(!y)
If y is a pointer, this is surely poor coding style, since ! is a pure Boolean operator.
<t></t>
Posts: 632
Threads: 18
Joined: Jan 2006
Reputation:
1
The interpretation, as expounded by lundin, is correct. The rule is intended to apply to all tests against 0 and this inclues testing pointers against NULL.
The rationale for this rule is to make a clear distinction between types that are being interpreted as Booleans (flags) and another other types.
Posted by and on behalf of the MISRA C Working Group