According to:
MISRA C-2012 Rule 11.3 violation while trying to do a typecast from char to int pointer
below piece of code is violating strict aliasing rule but what I'm missing here is what could compiler do wrong in such example that the result would be unexpected/undefined? Also no warning from compiler about any issue.
// compiled with -fstrict-aliasing -Wstrict-aliasing=2 -O3 -std=c99
uint8_t buffer[4];
static uint32_t get_word(uint8_t *cp)
{
return *((uint32_t *)cp);
}
int main()
{
buffer[0] = 8;
printf("value: %u", get_word(buffer));
return 0;
}
Let's not talk why I want to convert buffer of 4 chars to a word like that(instead of shifting) but rather why is this a problem at all and if there are cases where violating this rule will not get me into trouble.