Reputation: 47
Typedefs that indicate size and signedness should be used in place of the basic numerical types.
Please, can you help me? Why does my tool detect a violation for this code:
typedef int int_t;
How can I solve this?
Upvotes: 1
Views: 1589
Reputation: 213428
MISRA-C:2012 supports both C90 and later standards. In modern C we should simply use stdint.h
and no typedef
, so that's what I would strongly recommend. Cooking up your own custom types is bad practice.
If you are stuck with C90, then you need to make identical typedefs as the ones present in stdint.h
. Something like this:
#if !defined(__STDC_VERSION__) || (__STDC_VERSION__ < 199901L)
typedef unsigned char uint8_t;
/* and so on... */
#else
#include <stdint.h>
#endif
C90 did not define __STDC_VERSION__
and C94 has it as 199409L
, so the above macro covers everything pre-C99.
The above needs to be placed in some project-wide universal header.
Do the same thing with bool
, false
and true
since you don't have stdbool.h
in C90 either.
Upvotes: 1
Reputation: 1468
The bitwidth of int
depends on the hardware architecture (and sometimes on other parameters like the selection and configuration of the toolchain).
The type only has a minimum bitwidth/value range, but is not required to have certain exact properties.
In contrast, the types int8_t
, int16_t
, int32_t
, int64_t
and their corresponding unsigned counterparts (same type names but with a u
prefix) are defined in
stdint.h
to provide an exact bitwidth/value range - but leave the question open how efficient they can be used when ported to a different platform.
In Embedded systems (especially those with requirements of safety, security and/or availability), the main concern is that for a given variable type, every platform supports the bitwidth the programmer expected (or conversely, every programmer recognizes the limits of the type used). Therefore, a good recommendation is to start with the fixed-width types, and only use other types if optimization is really necessary and the risks induced by using non-fixed-width types can be prevented by other measures (which at that time legitimate a MISRA rule deviation).
Upvotes: 2