- 论坛徽章:
- 0
|
不对齐的访问,当然不是原子的,都会产生异常,何处理异常由处理器,操作系统和编译器共同完成。
不同的平台
X86允许不对齐的访问,当然代价是要多次访问内存。(可以认为产生了异常,只不过此异常有处理器自己处理)
大部分RISC会产生BUS ERROR,由操作系统处理,或者把异常把异常给用户程序。
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
static void
handle_sigbus(int sig)
{
printf("SIGBUS captured\n");
exit(1);
}
int
main(void)
{
char buf[256] = {0};
int *p = (int *)&buf[1];
signal(SIGBUS, handle_sigbus);
*p = 0;
return 0;
}
以Sun Studio的cc手册是这么写的
-xmemalign=ab
Accepted values for a are:
1 Assume at most 1 byte alignment.
2 Assume at most 2 byte alignment.
4 Assume at most 4 byte alignment.
8 Assume at most 8 byte alignment.
16 Assume at most 16 byte alignment.
Accepted values for b are:
i Interpret access and continue execution.
s Raise signal SIGBUS.
f For variants of -xarch=v9 only. Raise signal
SIGBUS for alignments less than or equal to
4, otherwise interpret access and continue
execution. For all other -xarch values, the f
flag is equivalent to i. |
|