MISRA Discussion Forums

Full Version: MISRA Rules Ambiguity
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
My name is Igor Gvero and I am a Product Manager/Owner at Klocwork. Our developers had few questions with respect to ambiguity of some rules. Questions are below.

1. Concerning the Rule 5-0-15 of the MISRA C++ 2008 standard, is the following example compliant with the rule?

struct Bar
int32_t* dataP[2];

void f()
int32_t myarray[10];
Bar b;
b.dataP[0] = myarray;
b.dataP[1] = myarray;

int32_t c = (b.dataP[1])[3]; // Is this expression compliant with the rule 5-0-15?

The reason that I am asking this question is that the examples for the rule 5-0-15 seem to be contradictory about the use of pointers as arrays. For example, the following are examples from the documentation of the rule 5-0-15:

void my_fn(uint8_t * p1, uint8_t * p2[ ])
p1[ 5 ] = 0; // Non-compliant – p1 was not declared as array

uint8_t a[ 10 ];
uint8_t * p;
p = a;
p[ 5 ] = 0; // Compliant : seems to be contradictory with the previous example where it is non-compliant.

I would appreciate if you could explain the exact rule for the use of pointers as arrays in your answer.

2. Concerning the Rule 2-10-2 of the MISRA C++ 2008 standard, is operator overloading allowed for this rule? I think it should be allowed, but I would like to be sure. The rule discuss about identifiers. An operator name should not be an identifier, thus operator overloading should be allowed, but it is not clear from the rule. For example, is the following example compliant with the rule?


namespace myNamespace {
class myClass {
static void* operator new(size_t); // Is this operator overloading compliant with rule 2-10-2?

3. Concerning the Rule 5-0-2 of the MISRA C++ 2008 standard, can one use parentheses for the operand of the sizeof operator? The rule states that “Parentheses are not required for the operand of a unary operator”. Sizeof is unary operator, thus parentheses should not be required. However, it is common practice to use parentheses with this operator (and even some MISRA examples use this style like rule 5-3-4). Here are some examples to help understand my question:

uint8_t* a = ...;

sizeof *a; // Should be compliant with the rule.
sizeof (int); // Should be compliant with the rule too since the operand is a cast.

sizeof(*a); // This is the one that I am not sure of. I think that it should be accepted but the rule seems to say otherwise.
Rule questions should be posted under the relevant section. Section 6 means chapter 6 of the document which contains the rules. Thus a query on rule 5-0-15 should be placed under topic "MISRA C++ > MISRA C++:2008 rules > 6.5 Expressions (C++)", a question on rule 2-10-2 under "6.2 Lexical conventions" and so on.