For rule 5-2-12: replace "variable" with "value"? - 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.5 Expressions (C++) (https://forum.misra.org.uk/forumdisplay.php?fid=134) +---- Thread: For rule 5-2-12: replace "variable" with "value"? (/showthread.php?tid=959) |
For rule 5-2-12: replace "variable" with "value"? - James Widman - 12-04-2013 Rule 5-2-12 is: "An identifier with array type passed as a function argument shall not decay to a pointer." Why does the rule apply only to identifiers and function arguments? Example: Code: typedef char S[10]; Each of these calls would be in violation if the rule applied to values rather than variables. Also the rationale says, "If a design requires arrays of different lengths, then a class should be used to encapsulate the array objects and so ensure that the dimensionality is maintained." This question was motivated by a test case from one of our users: Code: #include ... against which PC-lint, in MISRA C++ mode, says that rule 5-2-12 has been violated. That's incorrect according to the letter of 5-2-12, but then, 5-2-12 is apparently violated in: Code: #include But given the rationale of the rule, it seems like MISRA probably wants to make an exception for the case of std::basic_string::assign(const char*) because it converts from a null-terminated character array to an object of class type that encapsulates the array value. What about the restriction to cases of function arguments? Why does this restriction not apply to other contexts where there is a decay from array to pointer type? (Think of initialization/assignment in the context of a return statement, in the definition of a variable, in a curly-braced initializer, or in a men-initializer.) If this rule applies only to function arguments but not initializers, then there is a funny difference in the treatment of initializers: If a class object is initialized by a constructor that takes a pointer, then this rule applies when the initializer value is initially of array type (because a constructor is a function and the initializer value is the argument to that function). But if you change the type of the declared object to a bare pointer type, the rule does not apply. Re: For rule 5-2-12: replace "variable" with "value"? - misra cpp - 11-10-2016 The conclusion in the question is correct, these various examples are permitted by the rule as it is currently worded. However, they were not intended to be permitted. A possible modification may be added to a TC to say "an argument of array type" Re: For rule 5-2-12: replace "variable" with "value"? - dg1980 - 12-01-2018 I have another question about the example code on page 83. If Code: f2(a);// Non-compliant - Dimension "10" lost due to array to pointer conversion is changed to Code: f2(&a[0]); no more 5-2-12 violation is reported by our static analysis tool. But my guess is, this is a loophole not intended by MISRA? Thanks. Re: For rule 5-2-12: replace "variable" with "value"? - misra cpp - 17-01-2018 Your example is compliant f2 takes a pointer to int, &a[0] is a pointer to int - there is no decaying array Re: For rule 5-2-12: replace "variable" with "value"? - dg1980 - 19-01-2018 misra cpp Wrote:Your example is compliant f2 takes a pointer to int, &a[0] is a pointer to int - there is no decaying arrayOk, let me rephrase the question: is the code below fully compliant? Code: namespace nMISRA Re: For rule 5-2-12: replace "variable" with "value"? - misra cpp - 17-04-2018 Your code does not violate 5-2-12, as you are passing an explicit pointer - its not an array identifier that has decayed to a pointer |