C ++ SPARC总线错误 [英] C++ Bus error in SPARC arcitecture
问题描述
我想了解为什么我使用此代码遇到总线错误。
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;
}
推荐答案
对齐问题。在许多架构上,某些类型必须正确对齐,例如,4字节整数必须在4字节边界上开始。
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.
当您创建 整数 p
时,它将在堆栈上正确对齐的地址是正确的倍数。
When you create the integer p
, it will be aligned correctly on the stack at an address which is a correct multiple.
通过在字节上移动该地址,并解除引用 int
,您会导致 SIGBUS
。
By moving that address up on byte, and de-referencing that as an int
, you're causing the SIGBUS
.
Oracle上的此链接显示对齐要求。简而言之:
This link at Oracle shows the alignment requirements. In short:
- 整数在16位边界上对齐。
- 长整型在64位边界上对齐。
- 长整数在64位边界上对齐。
- 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.
这篇关于C ++ SPARC总线错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!