[previous] MISRA C:2012  Rule-13.6:  (Mandatory) [next] The operand of the sizeof operator shall not contain any expression which has potential side-effects

Amplification:

Any expressions appearing in the operand of a sizeof operator are not normally evaluated. This rule mandates that the evaluation of any such expression shall not contain side-effects, whether or not it is actually evaluated.

A function call is considered to be a side effect for the purposes of this rule.

Exception:

An expression of the form sizeof ( V ), where V is an lvalue with a volatile qualified type that is not a variable-length array, is permitted.

Example Code:


#pragma PRQA_MESSAGES_OFF 2982,2984

#include "misra.h"
#include "m3cmex.h"

static const char * rule_1203a( void );

extern int16_t rule_1306( void )
{
   volatile uint16_t vui;
   uint16_t x;
   uint16_t u = 1U;
   uint16_t v = 2U;
   int16_t r;

   x = sizeof( int16_t );                               /*                     */
   x = sizeof( u++ );                                   /* 3307                */
   x = x + sizeof( u );                                 /*                     */
   x = x + sizeof( u++ );                               /* 3307                */
   x = x + sizeof( rule_1203a() );                      /* 3307                */
   x = x + sizeof( u + v );                             /*                     */
   x = sizeof(int16_t [10]);                            /*                     */
   x = sizeof(int16_t [u16a]);                          /*      0945 1051      */
   x = sizeof(int16_t [u16a++]);                        /*      0945 1051      */    /* Non-compliant */
   x = sizeof (void (*[u16a]) (int32_t buf[vui]));      /*      0945 1051 1052 */    /* Non-compliant */
   r = (int16_t)x + (int16_t)u + (int16_t)v;            /*                     */

   return r;
}

static const char * rule_1203a( void )
{
   return "ABC";
}


QAC messages that encompass this guideline:

3307 The operand of 'sizeof' is an expression with implied side effects, but they will not be evaluated.



(c) The Motor Industry Research Association, 2012
QA C Source Code Analyser 8.1.2
MISRA C:2012 Compliance Module 1.0
© 2013 Programming Research
www.programmingresearch.com
Personality Groups | Glossary | Message Index | MISRA C:2012 Rule Index Contents