【发布时间】:2014-04-03 21:10:34
【问题描述】:
我想假设 C 语言中的数组是微处理器中的一个内存区域,因此我可以在 PC 上编译一些代码。我编写了一个小程序来尝试使语法正确,但程序无法运行,当我更改访问变量的方式时,它要么崩溃,要么无法编译——太晚了,我不明白为什么.请问这是怎么回事?
// original code in microprocessor header that I need to change if I compile on the host
// BASE is simply a hex value that is later used as an address or a hex value
#define BASE (0x0000)
// used later in header like this (cannot change the way this is done)
#define OFFSET 0x0001
#define PERIPHERAL (BASE + OFFSET)
// also used like (also cannot change):
uint32_t var = PERIPHERAL | HEXMASK;
// here is how I intend to replace the uC specific code
// replace the BASE DEFINE with the next 2 lines of code:
// instead of writing to memory location, write to array of bytes instead, so declare it:
uint8_t BASE_memory[4] = {0, 0, 0, 0};
// define BASE as hex value that can be used as drop-in replacement in either of the 2 uses shown above
#define BASE ((uint32_t)(BASE_memory))
// now test usage
// access contents of BASE_memory[0]
printf("contents of BASE_memory[0] == %02x\n", *((uint32_t *)(BASE)));
// now I want to access PERIPHERAL, the second element of the array, i.e. BASE_memory[1]
printf("contents of BASE_memory[1] == %02x\n", *((uint32_t *)(PERIPHERAL)));
【问题讨论】:
-
你似乎在玩指针算法。当您尝试访问 BASE[1] 时,应该获取字节 4 到 8 还是字节 1 到 5?两者似乎都没有被分配,这是一个潜在的(但不太可能)错误,但是第二个有可能在某些架构上触发未对齐的访问错误,但在其他架构上则不会。为什么 (uint32_t *) ?
-
抱歉,现在在 iPad 上输入并输入 BASE 而不是 BASE_memory,现在已编辑。微处理器是 32 位的,但在数据表中对微处理器内存映射的引用是以字节为单位的,因此 uint32_t 因为更容易看到我在硬件代码中正在做什么。我基本上希望能够在我的 PC 代码中使用 (BASE + hex offset) 结构来允许在 PC 上进行单元测试(不必费力地从制造商那里获得巨大的标头并进行更改)。非常感谢您看这个!
-
您是要完成 8 位访问还是 32 位访问?因为现在您正在执行未对齐的(在第一种情况下或第二种情况下或两者兼有)32 位访问,而在第二种情况下包括未分配的(至少为此目的)内存,因为您正在访问 4 的第 5 个字节字节数组...
-
啊,谢谢。我当然是个白痴。当我增加数组时,它运行正常,我可以通过 printfs 中的 32 位或 8 位访问来访问。我收到有关强制转换 #define BASE ((uint32_t)(BASE_memory)) 的警告
-
非常感谢您的帮助,非常感谢 - 从您提到的几点开始,这里存在许多问题。当我跟进他们将 uint32_t 更改为 uintptr_t 时,一切都按预期工作。
标签: c dereference