MISRA Discussion Forums

Full Version: About 17.4 Question?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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;
for(i=0u;iErrorCode = ReadEeprom(0u);
Err->ErrDCVoltage = ReadEeprom(1u);
Err->ErrMCUState = ReadEeprom(2u);

if my struct has more than 100 items, What can I do?
Can't you do something like this:
void LoadError(ErrorLog_t Err[],Uint16 Index)
  Err[Index] = /*...*/;
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.