今天再次遇到字节对齐问题,注意在crtdefs.h文件中定义为
#if !defined(UNALIGNED)
#if defined(_M_IA64) || defined(_M_AMD64)
#define UNALIGNED __unaligned
#else
#define UNALIGNED
#endif
#endif

可见在32位系统中用UNALIGNED和没用是一样的,但在ia64或者amd64平台下,就不同了,__unaligned
关键字会指示编译器一次读一个字节,再看看MSDN给的例子:
// unaligned_keyword.cpp
// compile with: /c
// processor: x64 IPF
#include
int main()
{
char buf[100];
int __unaligned *p1 = (int*)(&buf[37]);
int *p2 = (int *)p1;
*p1 = 0; // ok
__try
{
*p2 = 0; // throws an exception
}
__except(1)
{
puts(“exception”);
}
}

C++
2008-11-17 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *