How to debug segmentation fault?

前端 未结 4 1119
我在风中等你
我在风中等你 2020-12-16 08:33

It works when, in the loop, I set every element to 0 or to entry_count-1. It works when I set it up so that entry_count is small, and I write it by hand instead of by loop (

4条回答
  •  太阳男子
    2020-12-16 09:13

    After a few days of experimentation, I figured out what was really going on.

    For some reason the machine segfaults on unaligned access. That is, the integers I was writing were not being written to memory boundaries that were multiples of four bytes. Before the loop I computed the offset and shifted the array up that much:

    int offset = (4 - (uintptr_t)(memory) % 4) % 4;
    memory += offset;
    

    After doing this everything behaved as expected again.

提交回复
热议问题