Does unaligned memory access always cause bus errors?

后端 未结 3 1674
予麋鹿
予麋鹿 2020-11-28 13:39

According to the Wikipedia page Segmentation fault, a bus error can be caused by unaligned memory access. The article gives an example about how to trigger a bus er

3条回答
  •  日久生厌
    2020-11-28 14:16

    Consider the following example I have just tested on ARM9:

    //Addresses       0     1     2    3     4     5     6     7      8    9
    U8 u8Temp[10] = {0x11,0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0x00};
    
    U32 u32Var;
    
    u32Var = *((U32*)(u16Temp+1));  // Let's read four bytes starting from 0x22
    
    // You would expect that here u32Var will have a value of 0x55443322 (assuming we have little endian)
    // But in reallity u32Var will be 0x11443322!
    // This is because we are accessing address which %4 is not 0.
    

提交回复
热议问题