arrays - Passing buffers in C with MISRA compliance -


misra frustrating our developers.

we getting misra errors "do not apply pointer arithmetic pointer" , "pointer not point array".

we using syntax of:

uint8_t const * p_buffer 

to pass buffer function writes buffer spi bus.

given example code fragment:

static void write_byte_to_spi_bus(uint8_t byte);  void write_buffer_to_spi_bus(uint8_t const * p_buffer,                              unsigned int quantity) {   (unsigned int = 0; < quantity; ++i)   {     write_byte_to_spi_bus(*p_buffer++);   } } 

is there way have pointer cell in array , increment satisfy misra?

my interpretation misra wants incrementing indices array , not pointer:

void write_array_to_spi_bus(uint8_t const p_array[],                             unsigned int quantity)     {       (unsigned int = 0; < quantity; ++i)       {         write_byte_to_spi_bus(p_array[i]);       }     } 

many of developers old school , prefer use pointers uint8_t rather passing array.

john bode has given reply how (in effect) second code fragment addresses misra guideline.

i'll address question "is there way have pointer cell in array , increment satisfy misra?"

the short answer "no". rule 17.4 (in misra 2004 - don't have 2012 version handy) states "array indexing shall allowed form of pointer arithmetic". underpinning of changes required make in case.

the longer answer misra guidelines based on premise array syntax somehow safer pointer dereferencing. personal view weakness in misra, since doesn't address problems of array index going out of bounds - has same consequence of pointer arithmetic passes beyond bounds of array.

also, "old school" approach of shadowing variable (using index based loop, , incrementing pointer in loop) not practice either. has lack of safety misra guideline trying prevent, plus making code harder read (mere humans have work harder understand there one-to-one relationship between values of i , p_buffer - , code harder understand easier wrong).


Comments

Popular posts from this blog

twig - Using Twigbridge in a Laravel 5.1 Package -

jdbc - Not able to establish database connection in eclipse -

firemonkey - How do I make a beep sound in Android using Delphi and the API? -