MISRA Discussion Forums

Full Version: Rule 18.1 and Rule 11.3 compatibility
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

i am not sure about compliance of following example.

uint32_t varialble = 0;
uint8_t * ptr = (uint8_t *)variable;

(*ptr) = 0;
ptr++;        //line in question
(*ptr) = 0;        //line in question

While exception to the rule 11.3 clearly says, that converting the pointer to object to pointer to char is ok and can be used for accessing individual bytes, Rule 18.1 says that object that is not an array shall be treated as an array of single element and does not allow dereferencing a pointer beyond the end of it.

Can an object that is not an array be treated as an array of 8-bit variables with length of its byte size for this purpose? or can i only access the first byte of the object this way?


Should the second line of your example read:
uint8_t * ptr = (uint8_t *)&variable;
i.e. you are taking the address of variable? Otherwise, you are dereferencing a null pointer.
Yes, sorry, the line in the example should have been like you said.

uint32_t variable = 0;
uint8_t * ptr = (uint8_t *)&variable;

(*ptr) = 0;
ptr++;      //line in question
(*ptr) = 0;      //line in question
The intention was that such pointer accesses should be be compliant with Rule 18.1, but the working group accept that this is not clear in the documentation.

A future technical clarification will include the following note in the Amplification for rule 18.1.
Quote: Note: A pointer to an object type(T) which has been converted to a pointer to a character type (see Exception to rule 11.3) shall be treated as an array of character type with bound equal to sizeof(T).
In the above example "ptr" should be treated as an "array of 4 unsigned char".