Search code examples
c++cpointersiar

pointers in the IAR compiler


Long story short I have this in C using the IAR EWARM compiler.

uint8_t packet[2048];
uint32_t* src = (uint32_t*)&packet[9];
uint32_t var = *src++;

That last line causes a bus fault.

uint8_t packet[2048];
uint32_t* src = (uint32_t*)&packet[9];
uint32_t var = 0xFE;
*src++;

Now there is no bus fault. I can see in the debugger src points to the data I expect it to point to. Increment it works as expected but trying to read it casues a bus fault.

Any help?


Solution

  • It could be that your MCU needs reads of 32 bit integers to be aligned to 32 bits.

    &packet[9] is most certainly not 32 bit aligned, that's why you get a fault.