【发布时间】:2017-10-27 04:47:08
【问题描述】:
假设在地址 0x1ffff670 有一个内存映射设备。设备寄存器只有 8 位。我需要获取该寄存器中的值并加一并写回。
以下是我的做法,
void increment_reg(){
int c;//to save the address read from memory
char *control_register_ptr= (char*) 0x1ffff670;//memory mapped address. using char because it is 8 bits
c=(int) *control_register_ptr;// reading the register and save that to c as an integer
c++;//increment by one
*control_register_ptr=c;//write the new bit pattern to the control register
}
这种方法正确吗?非常感谢。
【问题讨论】:
-
这看起来不错,但您可能希望将指针设为
volatile。
标签: c++ pointers casting memory-mapping