Posts: 3
Threads: 2
Joined: Aug 2009
Reputation:
0
I would suggest a new rule for MISRA C.
"Do not use the sizeof() operator".
Output seems to be compiler dependent, e.g. TI compiler for TMS320C2x/C2xx/C5x series returns the number of 16bit units.
They (TI) claim, that there is an ambiguity in the ANSI C definition:
- sizeof() shall return the number of bytes
- sizeof(char) shall return 1
As they implement a char as 16 bit, their solution of the dilemma is to define a byte as 16 bit.
This sounds problematic and will cause issues while porting code, which uses the sizeof() operator.
<t></t>
Posts: 29
Threads: 3
Joined: Dec 2008
Reputation:
0
The definition of "byte" is defined in clause 3.4 of ISO/IEC 9899:1990
Basically, a byte is the size of a character. If a character is 16 bits then a byte is 16 bits.
Thus in my view, the implementation is compliant and the C standard is not ambiguous in this respect.
A byte is not always 8 bits.
<t></t>
Posts: 29
Threads: 3
Joined: Dec 2008
Reputation:
0
The C90 definition of a byte is essentially identical to the C99 one. The notes in C99 are part of the clause in C90.
I see not clause in C99 that defines a char as 8 bits. Do you have a clause reference number please?
The CHAR_BIT macro defined in defines how many bits is a char in both C90 and C99.
CHAR_BIT is required to by AT LEAST 8 in C90 and C99 so 7 bits in a byte is not allowed whereas 16 is.
I would say that Rule 6.3 should cover this issue?
Rules 6.1 and 6.2 discuss char so perhaps a note about the size of a byte/char could be included there?
<t></t>
Posts: 70
Threads: 8
Joined: Dec 2007
Reputation:
0
Well, after reading through C99, it seems every definition of char or a byte I can find is stated as "large enough...", ie at least 8 bits.
On the other hand, there is no text anywhere saying that char is allowed to be larger than 8 bits either.
The only text that is clearly written in the standard regarding this topic is actually the text below the sizeof() operator, which could be
interpreted as a statement of how large the char type is, by using plain logic:
sizeof() shall return the number of bytes of its operand
sizeof(char) shall return 1
Logical conclusion: the size of char is 1 byte
One may argue about whether the sizeof() chapter is a good place to write that definition, but still the text is just as normative as the rest of the standard.
So by that, one could say that 16 bit char is non-standard C. (There is the wchar_t, so there is no reason to make a mess of the ordinary char type...)
Also, there is always a need in embedded programming to communicate with hardware on byte-basis. With a compiler implementing char as 16 bit, you must
write all such code in inline assembler. Therefore I would definitely label such a compiler entirely unsuitable for embedded systems.
<t></t>
Posts: 70
Threads: 8
Joined: Dec 2007
Reputation:
0
It would seem that the real issue here is the poor definition of a byte in ISO C. A better proposal for a new rule would be "A byte shall always be regarded as 8 bits".
Because... making a byte 16 bits or 555 bits is like asking to have your product shot down in horrible ways over and over until everyone is fed up and it becomes obsolete.
I don't think it is motivated to change MISRA-C to support some weird >25 years old CPU core.
<t></t>
Posts: 632
Threads: 18
Joined: Jan 2006
Reputation:
1
The MISRA C Working Group does not believe that a rule precluding all use of sizeof() is warranted.
We note the comments regarding potential improvements to the text of rules 6.1 and 6.2.
Posted by and on behalf of the MISRA C Working Group