Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to properly cast NULL according to MISRA?

(char *) NULL or char * msg = NULL triggers MISRA 10.5: The value of an expression should not be cast to an inappropriate essential type.

So what is the correct way to cast NULL to some pointer?

N.B.: I am using MISRA checks in CLion.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Assuming you didn’t define a home-made version of NULL (which would be another MISRA violation), then char* msg = NULL; is fine and MISRA compliant. Conversion from a null pointer constant to a pointer type without casting is explicitly listed as compliant at multiple places in the guidelines.

(char *) NULL looks like nonsense, what’s that supposed to achieve?

Either way, rule 10.5 has nothing to do with any of this since that rule is about value conversions, not about pointer conversions. So it would seem that your MISRA checker is broken, like they tend to be.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading