Rule 3-4-1 issues

Moderators: david ward, misra cpp

CGeddes
Posts: 1
Joined: Fri Sep 26, 2014 10:21 am
Company: Curtiss Wright

Rule 3-4-1 issues

Postby CGeddes » Thu Oct 16, 2014 2:22 pm

We're having issues complying with rule 3-4-1 in a number of functions that initialise a temporary variable which is subsequently modified and tested within a loop. I appreciate in many cases this can be resolved with recursion, however, it seems an excessive solution given the additional considerations required.

The only other solution we've found (that doesn't conflict with other rules) is to declare and initialise a struct with the members set to the required values. e.g.

Code: Select all

void f (void)
{
    struct FOO
    {
        int Index;
        int Result;
    } ;
   
    for (FOO Test = {0, 0}; Test.Index < 10; Test.Index++)
    {
        Test.Result += SomeTestFunction( );
   
        if (Test.Result > 5)
        {
            break;
        }
    }
}

What is the an 'expected' implementation of such functionality?

misra cpp
Posts: 145
Joined: Mon Jun 02, 2008 1:55 pm
Company: MISRA

Re: Rule 3-4-1 issues

Postby misra cpp » Tue Oct 11, 2016 10:25 am

This program is equivalent and compliant. Result has to be declared outside the loop, as the value in loop n+1 depends upon the value in loop n, so Result cannot be inside the body of the loop

Code: Select all

void f ( void )
{
  int Result = 0;
 
  for ( int Index = 0; Index < 10; Index++ )
  {
    Result += SomeTestFunction( );
   
    if ( Result > 5 )
    {
      break;
    }
  }
}
Last edited by misra cpp on Wed Oct 12, 2016 11:04 am, edited 1 time in total.
Reason: Reformat example
Posted by and on behalf of
the MISRA C++ Working Group


Return to “6.3 Basic concepts (C++)”

Who is online

Users browsing this forum: No registered users and 4 guests