我在 32 位嵌入式系统上转换和修改指针时遇到了一个奇怪的问题(redbee econotag 运行 contiki OS 具体而言)。
uint32_t array[2];
array[0] = 0x76543210;
array[1] = 0xfedcba98;
uint8_t* point = ((uint8_t*)array)+1;
printf("%08x \n", *(uint32_t*)point );
在我的电脑上输出:
98765432
嵌入式设备上的输出:
10765432
我的计算机的行为与我预期的一样,但是嵌入式设备似乎在到达单词末尾时环绕。为什么会这样?
最佳答案
您的目标“redbee econotag”被声明为具有 ARMv4 架构的 ARM7。 ARMv4 不像 ARMv7 或英特尔机器那样提供未对齐的内存访问。
On ARMv4 and ARMv5 architectures, and on the ARMv6 architecture depending on how it is configured, care needs to be taken when accessing unaligned data in memory, lest unexpected results are returned. For example, when a conventional pointer is used to read a word in C or C++ source code, the ARM compiler generates assembly language code that reads the word using an LDR instruction. This works as expected when the address is a multiple of four, for example if it lies on a word boundary. However, if the address is not a multiple of four, the LDR returns a rotated result rather than performing a true unaligned word load. Generally, this rotation is not what the programmer expects.
关于在嵌入式设备上转换指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14032434/