Making constants in MISRA compliant C++ - Printable Version +- MISRA Discussion Forums (https://forum.misra.org.uk) +-- Forum: MISRA C++ (https://forum.misra.org.uk/forumdisplay.php?fid=18) +--- Forum: MISRA C++:2008 rules (https://forum.misra.org.uk/forumdisplay.php?fid=19) +---- Forum: 6.1 General (C++) (https://forum.misra.org.uk/forumdisplay.php?fid=130) +---- Thread: Making constants in MISRA compliant C++ (/showthread.php?tid=1213) |
Making constants in MISRA compliant C++ - Insane Vent Storm - 09-11-2015 I am trying to declare a constant for use in fixing the size of several C++ arrays. I have tried 3 options: 1) #'define MaxTracksConst 8 2) const Rhp_int32_t MaxTracksConst(8); 3) enum %s {one, two, three, four, five, six, seven, MaxTracksConst} but our LDRA MISRA checker finds fault with all of them ( 1. fails 16-2-2, 2. fails 3-1-1 and 3. fails 4-5-2 ). Am I missing something, or is there an option that will pass MISRA C++ 2008 checks ? Kind regards Insane Vent Storm Re: Making constants in MISRA compliant C++ - Will Freitag - 13-09-2016 I've been faced with the same issue and I choosed kinda 3): public header: Code: enum SIZES module: Code: void myFunc() Anybody any idea about this? Re: Making constants in MISRA compliant C++ - dg1980 - 14-09-2016 Did you know you can compare against PC-LINT for free? See http://www.gimpel-online.com/OnlineTesting.html. Be sure to write //lint -indirect(au-misra-cpp-alt.lnt) at the top of your sample code to get MISRA C++ 2008 checks. From my experience, all these tools either suffer from false positives or missing checks or both. In your case no violation of 4-5-2 is reported and after reading the rule again i agree. Also, number 2) is not a violation of 3-1-1 if the constant is in a header file, initialized with a literal and thus can be replaced at compile-time (no storage allocated). Re: Making constants in MISRA compliant C++ - dg1980 - 14-09-2016 Addendum for 2): of course you need to minimize visibility of the constant ( there is a separate rule for that), e.g. use static const in a class. Again, no storage is allocated unless the address is taken, so no violation of 3-1-1 possible. Re: Making constants in MISRA compliant C++ - misra cpp - 11-10-2016 The preferred method of declaring constants in C++ is 2, though 3 is acceptable in the specific case of declaring or accessing an array. Your checker shouldn't report 2 as a violation of the ODR (3-1-1) In a future version or Technical Corrigendum we ought to consider clarifying that 3-1-1 does not apply to const declarations. If the const were made static, 2-10-5 may also need modification to allow it as an exception Re: Making constants in MISRA compliant C++ - Will Freitag - 17-10-2016 I see! Actually, FlexeLint does NOT report a deviation on const definitions in header files (I havn't tried this at all because of 3-1-1). And I also agree, this exception should be mentioned in 3-3-1's text and example, too. Many thanks for your support! Re: Making constants in MISRA compliant C++ - misra cpp - 24-01-2017 You're welcome Re: Making constants in MISRA compliant C++ - rrosier - 14-10-2020 Could I also suggest that when making constants, an exception might also be needed in Rule 0-1-6 (A project shall not contain instances of non-volatile variables being given values that are never subsequently used), eg.: Code: std::int32_t doIt_const3_neq0(const std::int32_t aVal) In the code above, it is possible that kConst3 may be given a value, but that value is never subsequently used (it depends on the branches taken). The program will not compile if kConst3 is not defined, so it is used by the compiler. If the correct set of branches are taken, it also effects program operation, so it is not dead code. But, its usage can only be determined based on each individual program execution - some days it may not be used, other days it may be used. However, the alternative (to avoid breaching rule 0-1-6) seems to be to use literal (magic) values directly in the code, which does not match current programming practice. Re: Making constants in MISRA compliant C++ - misra cpp - 02-12-2020 Your code shouldn't be getting a violation of 0-1-6. As you say, 0-1-6 worries about "variables being given values that are never subsequently used" - NEVER being the operative word. Analysis of this function should determine that kConst3 MAY be used, depending upon the value of aVal, so no report is required |