About 17.4 Question?

6.17 Pointers and Arrays

Moderators: misra-c, david ward

Post Reply
Posts: 1
Joined: Tue Apr 05, 2016 3:05 am
Company: Megmeet Drive

About 17.4 Question?

Post by hutry999 » Thu May 05, 2016 10:36 am

I need read some parameters from EEPROM. My Code as below:

typedef struct
Uint16 ErrorCode;
Uint16 ErrDCVoltage;
Uint16 ErrMCUState;
Uint8 ErrMotorModeCmd;
Uint8 ErrDriveState;
Uint16 ErrTorqueCmd;
Uint16 ErrTorqueFed;
Uint16 ErrSpeedSet;
Uint16 ErrSpeed;
Uint8 ErrIGBTTemp;
Uint8 ErrMotorTemp;
Uint16 ErrUd;
Uint16 ErrUq;
Uint16 ErrIdRef;
Uint16 ErrIqRef;
Uint16 ErrIdFed;
Uint16 ErrIqFed;
Uint16 ErrState;
Uint8 Err12V;
Uint8 Angle;
Uint8 AngleInit;
Uint8 ErrMCUTemp;
Uint16 DC_Cur;

void LoadError(ErrorLog_t* Err,Uint16 Index)
Uint16 i;
Uint16 *PrErrLog;
PrErrLog = (uint16 *)Err;
PrErrLog = PrErrLog + i;
GetEEPValue = ReadEeprom(Adr + i*2u);
*PrErrLog = GetEEPValue;

this line code "PrErrLog = PrErrLog + i;" not allowed 17.4

#1476-D (MISRA-C:2004 17.4/R) Array indexing shall be the only allowed form of pointer arithmetic EepromManage.c /SynBase line 292 C/C++ Problem

How do I deal with this situation?
only deal like this:
Err->ErrorCode = ReadEeprom(0u);
Err->ErrDCVoltage = ReadEeprom(1u);
Err->ErrMCUState = ReadEeprom(2u);

if my struct has more than 100 items, What can I do?

Posts: 109
Joined: Wed Apr 27, 2016 2:33 pm
Company: Elektrobit Automotive GmbH

Re: About 17.4 Question?

Post by dg1980 » Mon May 09, 2016 6:44 am

Can't you do something like this:

Code: Select all

void LoadError(ErrorLog_t Err[],Uint16 Index)
  Err[Index] = /*...*/;

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

Re: About 17.4 Question?

Post by misra-c » Fri Jun 24, 2016 1:40 pm

There are a number of issues with this example as written.
  • The line "PrErrLog = (uint16 *)Err;" violates rule 11.4.
  • Cast is to uint16 not Uint16
  • Pointer increment is not consistent, but varies from 0 to SizeErrLog. So the accessed items are 0, 1, 3, 6 ...
  • Using a non-character pointer to access a struct whose members are of different sizes is not a good idea. Accessing using a non-character pointer is implementation dependent
In some circumstances it might be appropriate to access a structure using a pointer. In those cases a deviation is required for this rule. An example of the issues that need to be considered when deviating this rule ( and rule 17.1) can be found in
"MISRA C:2004 Permits Deviation permits for MISRA Compliance"
which are available from the "MISRA resources" section of the MISRA forum (http://www.misra.org.uk/forum/viewtopic.php?t=1562).

The MISRA working group can not give advice on particular coding design issues.
Posted by and on behalf of
the MISRA C Working Group

Post Reply

Return to “6.17 Pointers and Arrays”