c - Detecting if data in a struct has been corrupted before reading it -


someone has told me there techniques in embedded c can check if data has been corrupted. more interested in detecting whether data has been corrupted because of other process corrupting ram memory.

i have tried find information on internet couldn't find anything.

one "surround" members of struct boundary values , before read these members, check these values still expected ones:

#define boundary_value 0xdeadbeef  typedef struct {     uint32_t top_boundary;     int32_t some_data[4];     uint32_t bottom_boundary; } tmydummystruct;  tmydummystruct getsomedata( void ) {     return (tmydummystruct){         .top_boundary = boundary_value,         .some_data = {1, 2, 3, 4},         .bottom_boundary = boundary_value,     }; }  bool isdatacorrupted( tmydummystruct* data_struct ) {     if( data_struct->top_boundary == boundary_value &&         data_struct->bottom_boundary == boundary_value)     {         return false;     }     return true; } 

the other technique create crc member in struct , check/update every time read/write:

typedef struct {     int32_t some_data[4];     uint32_t crc; } tmydummystruct;  uint32_t getcrcof( int32_t[] );  tmydummystruct getsomedata( void ) {     tmydummystruct retval = {         .some_data = {1, 2, 3, 4},         .crc = 0,     };     retval.crc = getcrcof(retval.some_data);     return retval; }  bool isdatacorrupted( tmydummystruct* data_struct ) {     if( data_struct->crc == getcrcof( data_struct->some_data ) )     {         return false;     }     return true; } 

my questions are: know more techniques or better one? there place these techniques documented?

thanks

it depends on sort of corruption want detect , penalty willing pay in terms of memory , runtime detect it. tradeoff.

a simple scheme boundary scheme suggested in first example. detect if bytes in row or page corrupted, not detect single bit errors.

a more complex scheme store parity byte @ end of data. parity calculated xoring bytes in structure. detect single bit errors in data, not detect 2 bit errors. not have runtime.

further along complexity scale have crc code. can 16 bit or 32 bit , can detect multiple bit errors in data. read on hamming distance more information number of bit errors can corrected.

then there error correcting codes. these codes can detect multi bit errors , correct single or 2 bit errors. these used in fault tolerant microprocessors.


Comments

Popular posts from this blog

Hatching array of circles in AutoCAD using c# -

ios - UITEXTFIELD InputView Uipicker not working in swift -

Python Pig Latin Translator -