Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Do not use the sizeof() operator
#6
Hi Lundin,

I feel that you are assuming that a byte has 8 bits.
This assumption holds for 99% of cases but not all as hanthem has found out.

A byte is large enough to hold a character of the basic execution character set. (C90 3.4)
A char is large enough to hold a character of the basic execution character set (C90 6.1.2.5 p2)
The number of bits in a char is defined in the macro CHAR_BIT (C90 5.2.4.2.1)
CHAR_BIT can be any value but must be at least 8 (c90 5.2.4.2.1)
The sizeof(char) = 1 byte (C90 6.3.3.4)

From this one can conclude that char holds a byte and a byte has CHAR_BIT bits.

C99 is more explicit in that footnote 40 of clause 6.2.6.1 paragraph 3 says:
Quote:A byte contains CHAR_BIT bits, and the values of type unsigned char range from 0 to (2^CHAR_BIT) - 1.

The size of things in C is not defined in C and hence the need for MISRA rule 6.3.
You can write MISRA compliant code on 16 bit character systems but you can't use int8_t or uint8_t (or similar) as they can't exist!

Similar problem:
http://www.misra.org.uk/forum/viewtopic....0d149b6620
<t></t>
Reply


Messages In This Thread

Forum Jump:


Users browsing this thread: 2 Guest(s)