What does an “alignment trap” error means?

前端 未结 2 755
灰色年华
灰色年华 2021-01-16 12:06

One of my friends is having a big problem trying to debug a code that started showing \"alignment trap\" errors. The problem happens when a global structure is accessed by a

2条回答
  •  说谎
    说谎 (楼主)
    2021-01-16 12:45

    An alignment trap is triggered by ARM whenever an unaligned access is made. What is an unaligned access? It's when a multibyte value is accessed where its pointer is not a multiple of its alignment, e.g. when a uint32_t is accessed by dereferencing a pointer that isn't a multiple of 4.

    You can get them if you have __attribute__((packed)) data structures like this:

    struct foo {
        uint8_t a;
        uint32_t b;
    } __attribute__((packed));
    

    Accesses to b will be unaligned and therefore will cause an alignment trap. You have to memcpy the data into an aligned value, and then access it.

提交回复
热议问题