下面是您的代码,添加了调试输出。在主循环中,它在其占用的内存上运行了一些数组(故意),并且到目前为止它工作正常(尽管它可能仍然是平台不兼容的)。我很确定你没foo()
问题,编译器也是,但你可能错误地称呼它。使用调试输出检查所有地址和值并检查范围。
void foo(void* data, unsigned int length)
{
unsigned int i,j;
unsigned int block;
for(i = 0, j = 0; i < length; i+= sizeof(unsigned int ),j++)
{
printf("Debug 0\r\n");
printf("i: 0x%d\r\n", i);
printf("j: 0x%d\r\n", j );
#if 0//working code
block = ((unsigned char*)data)[i + 3];
block <<= 8;
block |= ((unsigned char*)data)[i + 2];
block <<= 8;
block |= ((unsigned char*)data)[i + 1];
block <<= 8;
block |= ((unsigned char*)data)[i + 0];
#else//not working code
block = ((unsigned int *)data)[j];
#endif
printf("data: 0x%x\r\n", data );
printf( " ((unsigned int )data +j) = 0x%x\r\n", ((unsigned int )data +j) );
printf( " ((unsigned int *)data)[j] = 0x%x\r\n", ((unsigned int *)data)[j]);
printf("block: 0x%x\r\n", block );
printf("Debug 1\r\n");
printf("\r\n");
printf("\r\n");
}
}
int main(void)
{
unsigned int array[10] = {0xFE,0xED,0xBA,0xBE,0xDE,0xAD,0xBE,0xEF,0xCA,0xFE};
printf ("array: 0x%x\r\n",array);
printf ("array[1]:%x\r\n",array[1]);
foo((void*) array, 50);
}
它的输出:
数组:0x1d0fdde0 数组[1]:ed 调试 0 i:0x0 j:0x0 数据:0x1d0fdde0 ((unsigned int )data +j) = 0x1d0fdde0 ((unsigned int *)data)[j] = 0xfe 块:0xfe 调试 1
调试 0 i:0x4 j:0x1 数据:0x1d0fdde0 ((unsigned int )data +j) = 0x1d0fdde1 ((unsigned int *)data)[j] = 0xed 块:0xed 调试 1
调试 0 i:0x8 j:0x2 数据:0x1d0fdde0 ((unsigned int )data +j) = 0x1d0fdde2 ((unsigned int *)data)[j] = 0xba 块:0xba 调试 1
调试 0 i:0x12 j:0x3 数据:0x1d0fdde0 ((unsigned int )data +j) = 0x1d0fdde3 ((unsigned int *)data)[j] = 0xbe 块:0xbe 调试 1
调试 0 i:0x16 j:0x4 数据:0x1d0fdde0 ((unsigned int )data +j) = 0x1d0fdde4 ((unsigned int *)data)[j] = 0xde 块:0xde 调试 1
调试 0 i:0x20 j:0x5 数据:0x1d0fdde0 ((unsigned int )data +j) = 0x1d0fdde5 ((unsigned int *)data)[j] = 0xad 块:0xad 调试 1
调试 0 i:0x24 j:0x6 数据:0x1d0fdde0 ((unsigned int )data +j) = 0x1d0fdde6 ((unsigned int *)data)[j] = 0xbe 块:0xbe 调试 1
调试 0 i:0x28 j:0x7 数据:0x1d0fdde0 ((unsigned int )data +j) = 0x1d0fdde7 ((unsigned int *)data)[j] = 0xef 块:0xef 调试 1
调试 0 i:0x32 j:0x8 数据:0x1d0fdde0 ((unsigned int )data +j) = 0x1d0fdde8 ((unsigned int *)data)[j] = 0xca 块:0xca 调试 1
调试 0 i:0x36 j:0x9 数据:0x1d0fdde0 ((unsigned int )data +j) = 0x1d0fdde9 ((unsigned int *)data)[j] = 0xfe 块:0xfe 调试 1
调试 0 i:0x40 j:0x10 数据:0x1d0fdde0 ((unsigned int )data +j) = 0x1d0fddea ((unsigned int *)data)[j] = 0x0 块:0x0 调试 1
调试 0 i:0x44 j:0x11 数据:0x1d0fdde0 ((unsigned int )data +j) = 0x1d0fddeb ((unsigned int *)data)[j] = 0x0 块:0x0 调试 1
调试 0 i:0x48 j:0x12 数据:0x1d0fdde0 ((unsigned int )data +j) = 0x1d0fddec ((unsigned int *)data)[j] = 0x0 块:0x0 调试 1
如果您更新更多详细信息和/或新结果,任何人都会获得更多理解并做出更好的答案,您将获得更可靠的答案。