ARM非对齐访问

结构体对齐的几种方式:<pre name="code" class="cpp">typedef struct
{
	unsigned char sbuf[3];
	unsigned int nLen;	
}ST_TEST_ALGIN;//4字节对齐
typedef struct
{
	unsigned char sbuf[3];
	unsigned int nLen;	
}__attribute__((packed)) ST_TEST_ALGIN_1;

#pragma pack(1)
typedef struct
{
	unsigned char sbuf[3];
	unsigned int nLen;	
}ST_TEST_ALGIN_2;
#pragma pack()

#pragma pack(push,1)
typedef struct
{
	unsigned char sbuf[3];
	unsigned int nLen;	
}ST_TEST_ALGIN_3;
#pragma pack(pop)
测试代码
ST_TEST_ALGIN stTest_Align;//  __attribute__((at(0x20002000)));
ST_TEST_ALGIN_1 stTest_Align_1;//  __attribute__((at(0x20003000)));
ST_TEST_ALGIN_2 stTest_Align_2;
ST_TEST_ALGIN_3 stTest_Align_3;


unsigned int nTest_Len;

void SetValue(__packed unsigned int *psValue)/*加上__packed 方可访问奇数地址内存,否则只能按照ARM4字节对齐方式取偶数地址*/
//void SetValue(unsigned int *psValue)
	
{
	nTest_Len = *psValue;
}
void test_H()
{
	
	memset((void*)&stTest_Align,0,sizeof(ST_TEST_ALGIN));	
	memset((void*)&stTest_Align_1,0,sizeof(ST_TEST_ALGIN));

	
	Printf("sizeof(ST_TEST_ALGIN):%d",sizeof(ST_TEST_ALGIN));	
	Printf("sizeof(ST_TEST_ALGIN_1):%d",sizeof(ST_TEST_ALGIN_1));
	
	Printf("sizeof(ST_TEST_ALGIN_2):%d",sizeof(ST_TEST_ALGIN_2));
	
	Printf("sizeof(ST_TEST_ALGIN_3):%d",sizeof(ST_TEST_ALGIN_3));
	
	stTest_Align_1.nLen = 888;
	SetValue(&stTest_Align_1.nLen);
	
	stTest_Align.nLen = 999;	
	SetValue(&stTest_Align.nLen);

	
	SetValue(&stTest_Align_2.nLen);
	
	SetValue(&stTest_Align_3.nLen);
}

 
 
 

猜你喜欢

转载自blog.csdn.net/hxl5955/article/details/52963352