C++ Bus error in SPARC arcitecture

烂漫一生 提交于 2020-01-13 07:17:11

问题


I would like to understand why I am getting a bus error with this code.

int main()
{
int p=34;
int *pp= (int *) ((char *)&p+1);
cout<<*pp<<"\n";
return 0;
}

回答1:


It will no doubt be an alignment issue. On many architectures, certain types have to be aligned properly, an example being that 4-byte integers must start on a 4-byte boundary.

If you access non-aligned data, some architectures won't care, others will run slower, still others (such as in this case) will fall in a screaming heap.

When you create the integer p, it will be aligned correctly on the stack at an address which is a correct multiple.

By moving that address up on byte, and de-referencing that as an int, you're causing the SIGBUS.

This link at Oracle shows the alignment requirements. In short:

  • short integers are aligned on 16-bit boundaries.
  • int integers are aligned on 32-bit boundaries.
  • long integers are aligned on 64-bit boundaries for SPARC systems.
  • long long integers are aligned on 64-bit boundaries.



回答2:


16-bit quantities must be stored at 16-bit or 2-byte alignment, and 32-bit (4 bytes) at addresses which are a multiple of 4.

Many CPUs support unaligned access, but it costs extra circuitry in the chip, and extra execution time to run the extra memory bus cycles to pick up the odd bytes. It is a particularly common RISC processor philosophy that requiring more care on the part of compilers and programmers to lay out data carefully for increased speed and simpler circuitry is an acceptable trade off.

By the way, it's unlikely that a low address like that would be in valid memory anyway. But your example does illustrate that the alignment exception takes priority over the SEGFAULT exception.



来源:https://stackoverflow.com/questions/1892566/c-bus-error-in-sparc-arcitecture

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!