11.4 A cast should not be performed ...

6.11 Pointer Type Conversions

Moderators: misra-c, david ward

Locked
Frank Haug
Posts: 4
Joined: Mon Oct 15, 2007 8:05 am
Company: none
Location: Germany

11.4 A cast should not be performed ...

Post by Frank Haug » Tue Nov 06, 2007 10:13 am

... 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
Posts: 566
Joined: Thu Jan 05, 2006 1:11 pm

Post by misra-c » Tue Dec 11, 2007 9:59 am

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.
---
Posted by and on behalf of
the MISRA C Working Group

Locked

Return to “6.11 Pointer Type Conversions”