R.13.5 When does a function have persistent side effects

Moderators: misra-c, david ward

Post Reply
misra-c
Posts: 569
Joined: Thu Jan 05, 2006 1:11 pm

R.13.5 When does a function have persistent side effects

Post by misra-c » Fri Mar 27, 2015 1:28 pm

Consider the following example:

Code: Select all

 bool fn ( int x )
 {
   static int y;
   if ( x > 0 )
   {   y = x;  }  /* causes persistent side effect if reached */
   return ....
 }
 
 void call ( bool flag )
 {
    if ( flag &&  fn(-3) ) /* Does RHS contain persistent side effect ? */
    { .... }
 }
In this example the call of "fn(-3)" never executes the code "y=x" and therefore no persistent side effect will occur.
Is this a violation of rule 13.5?
---
Posted by and on behalf of
the MISRA C Working Group

misra-c
Posts: 569
Joined: Thu Jan 05, 2006 1:11 pm

Re: R.13.5 When does a function have persistent side effects

Post by misra-c » Fri Mar 27, 2015 1:32 pm

A function is considered to have persistent side effects if there is a path through the function that might cause a persistent side effect. This determination takes no consideration of the possible values for parameters or other non-local objects.

Therefore, "fn(-3)" is considered to have persistent side effects" even though in this case the value of "y" is known not to change.
---
Posted by and on behalf of
the MISRA C Working Group

Post Reply

Return to “8.13 Side effects”