-1

If I have the follow code " *k != (Queue *)0 * ", there is a violation of the rule 11.9. But why ?

Qich can I rewrite thise code for make it compliant to MISRA 11.9?

Epik
  • 47
  • 6
  • 3
    `(Queue *)0` is actually not a _null pointer constant_. `0` is a null pointer constant and when converted to a pointer type, the result is a _null pointer_. However, regardless of language lawyer stuff, `(Queue *)0` is definitely code smell and you shouldn't write strange code like that, MISRA or no MISRA. – Lundin May 26 '20 at 09:54
  • It's worth noting that there's no guarantee that a null pointer is actually zero. There have been (I think it's historical) systems where the null pointer NULL wasn't zero, and so recreating it with a literal 0 as you have done is not guaranteed to be portable. See https://stackoverflow.com/questions/2597142/when-was-the-null-macro-not-0 – bazza May 26 '20 at 10:24
  • @bazza Nope, `0` is a null pointer constant and 100% portable. This is some subtle crap, but null pointers, null pointer constants and the NULL macro are 3 different, related terms. `0` in this case is not the binary representation of a null pointer. An exotic compiler which has a different representation for null pointers than "all zeroes" need to generate that representation when it encounters a conversion from a null pointer constant into a pointer. – Lundin May 26 '20 at 10:50
  • @Lundin, hmmm, I see what you mean. I was relying on the answer to that linked Q https://stackoverflow.com/a/2597197/2147218. The draft C18, C11 standards in section 7.19 say that "NULL expands to an implementation defined null-pointer constant". All that 6.3.2.3 requires is that NULL == (void*)0 is true, the zero being "a null pointer constant"; as you say there can be more than 1 such constant! So it doesn't necessarily follow that (Queue*)0 == NULL. This allows for, say, (int_vec_table_t *)0 != NULL, which is good for platforms where the interrupt vector table is at address 0. – bazza May 26 '20 at 12:09
  • @Lundin, at least according to the few words that I've selectively quoted. There's quite possibly other parts of the standard that results in something like (Queue*)0==NULL being true - I'm by no means an expert on the content of the standards... – bazza May 26 '20 at 12:24
  • @bazza It's not too clear what the standard means with NULL being an "implementation-defined null pointer constant". The only two null pointer constants defined by the standard are `0` and `(void*)0`. So the only sensible interpretation of the standard is that NULL can either expand to `0` or to `(void*)0`, but nothing else. What a _null pointer_ expands to is another story: "If a null pointer constant is converted to a pointer type, the resulting pointer, called a _null pointer_, is guaranteed to compare unequal to a pointer to any object or function." – Lundin May 26 '20 at 13:05

1 Answers1

4

You have to use the Keyword "NULL" to make it compliant:

*k != NULL
A.R.C.
  • 910
  • 11
  • 22