Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Rule 18.1 and Rule 11.3 compatibility
#1
Hi,

i am not sure about compliance of following example.

Code:
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?

thanks,

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


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

(*ptr) = 0;
ptr++;      //line in question
(*ptr) = 0;      //line in question
<t></t>
#4
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".
Posted by and on behalf of the MISRA C Working Group


Forum Jump:


Users browsing this thread: 3 Guest(s)