首页 > 代码库 > Bigendian 奇数内存地址取整形crash

Bigendian 奇数内存地址取整形crash

在大端机器(solaris-sparc,hpux-hppa)上从奇数内存地址取一个整形(2 or 4 bytes)会崩溃。如:

unsigned short Res = *(unsigned short *) tmpArray;

应该:

char tmpArray[sizeof(unsigned short)] = {0};
for(int i = 0; i < sizeof(unsigned short); ++i)
{
tmpArray[i] = *(pcNewHdr + i);
}

unsigned short Res =*(unsigned short *) tmpArray;

Bigendian 奇数内存地址取整形crash