You cannot have a pointer or reference to a register. Registers do not form part of the computer’s main memory, and are not addressable using memory addresses.
Your code pieces, despite using a #define
called REGISTER
, are completely unrelated to registers. They attempt to write to normal, addressable memory. On some operating system architectures, specific memory addresses may be mapped directly to hardware ports (including, on some devices, registers). Most modern operating systems, however, don’t support this. They use the concept of virtual memory instead, and pointers do not map directly to hardware locations.
Concerning the code you’ve posted, the two pieces are not identical. The second won’t compile because you are trying to assign an integer to a pointer. You can make it compile by dereferencing the pointer before the assignment (*realRegister = 0x10101010;
) but the two pieces of code are still not identical:
The first piece of code writes its value at the address
reinterpret_cast<char*>(0x60000000) + sizeof(unsigned int)
The second piece of code, by contrast, writes its value at the address
reinterpret_cast<char*>(0x60000000) + 4
– The two pieces of code are only identical on machines where sizeof(unsigned int) == 4
.