Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
11.5: Conversion from point to void
#1
According to the Exception to 11.3, it is permissible to convert a pointer to object into pointer to char, signed char, or unsigned char. There doesn't appear to be an Example within the MISRA document, but I believe it would look like
Code:
uint8_t  *p8;
uint16_t *p16;

p8 = ( uint8_t * ) p16;  /* Compliant - exception */
According to 11.5, it is permissible to convert a pointer to object into a pointer to void, but any conversion of pointer to void into pointer to object is non-compliant:
Code:
uint16_t *p16;
uint32_t *p32;
void     *p;

p = ( void * ) p16;       /* Compliant */
p32 = ( uint32_t * ) p; /* Non-compliant */
According to the C99 specification, "A pointer to void shall have the same representation and alignment requirements as a pointer to character type."
Given that the MISRA rules, themselves, allow conversion of pointer to object into both pointer to void and pointer to character, and that the spec indicates that pointer to void and pointer to character can not have conflicting alignment requirements, should it be allowed to convert from pointer to void to pointer to character:
Code:
void    *p;
uint8_t *p8;

p8 = ( uint8_t * ) p; /* Compliant? */
<t></t>
#2
The exception to rule 11.3 exists because conversion from a "pointer to object" to a "pointer to character" type is a commonly used programming idiom for accessing the underlying byte data of the object.

There is no such compelling requirement for conversions from other types such as "pointer to void" and so no exception has been made in that case.

The conversion from void* to another pointer type requires a deviation as no type information is included. This includes conversions from void* to char*.
Posted by and on behalf of the MISRA C Working Group
#3
For functions which take arbitrary data and operate on it in a byte-wise fashion, for example, calculating a CRC, which would be the preferred method according to the MISRA guidelines:

Code:
uint32_t CRC(size_t length, void * data);
which internally converts data to a pointer to uint8_t (with the requisite deviation), or

Code:
uint32_t CRC(size_t length, uint8_t * data);
and would this require an explicit cast to pointer to uint8_t at each calling location? Or would the Exception to 11.3 allow for an implicit conversion?
<t></t>
#4
The MISRA Working group can not make a comment on a preferred style of programming.

The exception to 11.3 only refers to casts, since rule 11.3 only applies to casts. Implicit conversions from pointer to object type to pointers to character type are constraint errors( C90 6.3.16.1, C99 6.5.16.1).
Posted by and on behalf of the MISRA C Working Group


Forum Jump:


Users browsing this thread: 2 Guest(s)