Makes size_t foo = 0; Do I need a throw?

Looking at this answer and knowing that 0 is an octal constant :

For hexadecimal [constants [and octal according to comments]], this is the first type that can insert a value: int , unsigned int , long , unsigned long , long long , unsigned long long

Therefore, I come out that this does not need to be done:

 size_t foo = 0; 

However, due to the strict MISRA-C utility, I am returning an illegal implicit type conversion message - MISRA-C: 2004 Rule 10.1.

Am I mistaken, or is this a tool by mistake?

(NB: I changed to size_t foo = 0U; since it is much easier than arguing with QA, but I would like to satisfy my curiosity.)

+6
source share
1 answer

I'm not sure what you are trying to achieve, but ...

  • 0 is of type signed int
  • 0U is of type unsigned int

size_t requires type size_t - and sizes are (usually) unsigned

So, for strict compliance, size_t foo = 0U; , although it would be more correct to use size_t foo = (size_t)0;

As an aside, a discussion of whether 0 is a decimal, octal, or anything else does not matter ... it is still zero.

-

Edit to add:

Although the explicit conversion between signed and unsigned was deprecated in MISRA C: 2004 (although it was widely rejected), for MISRA C: 2012, Rule 10.3 explicitly allows a non-negative integer expression of a constant of essentially a signed type, an object of a substantially unsigned type can be assigned if its value can be represented by this type

This is the standard way to say you don't need U

+3
source

All Articles