MISRA Discussion Forums
11.4 A cast should not be performed ... - 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.11 Pointer Type Conversions (https://forum.misra.org.uk/forumdisplay.php?fid=38)
+---- Thread: 11.4 A cast should not be performed ... (/showthread.php?tid=407)



11.4 A cast should not be performed ... - Frank Haug - 06-11-2007

... between a pointer to object type and a different pointer to object type (advisory).

Additional comment: \" Conversions of this type may be invalid if the new pointer type requires a stricter alignment.\"

---

I think there can be further risks. I suppose - e.g. - casting a signed pointer onto an unsigned pointer (of the same bitwidth and alignment) itself is not risky. Dangerous can be to WORK with the casted pointer regarding its content, it is pointing onto. In this case the cast itself would not be dangerous, but the possibilities afterwards are.

example:

unsigned int foo (signed int* ps)
{
unsigned int* pu;

pu = (unsigned int*) ps; // assumed to be no problem itself
*pu = ... ; // assumed to be no problem itself
// but can be a problem later, when the content of the incoming pointer is used afterwards
}

Can anybody confirm ? I think it can be of interest to know the problems in more precision. Does anybody know other implications (not applying such casts) ?

Regards,
Frank


- misra-c - 11-12-2007

Although the rule justification only refers to alignment problems, the intention of the rule is that ALL pointer casts should be avoided.

Other issues to consider when casting between pointer types include:
endian representation,
signedness,
size.