byte array at specyfic addres c++
I want to map some part of process memory as byte array. How to do it?
I have byte array:
byte AmmoBytes[3]
And I want this array to star开发者_开发百科t at address 0xXXXXXXX; How to do it?
byte (& a)[3] = *reinterpret_cast<byte (*)[3]>(0xDEADBEEF);
byte * AmmoBytes = (byte *) 0xXXXXXXXX;
This is unsafe, but you can say
byte * AmmoBytes = (byte *) 0xXXXXXXXX
Generally speaking you can't reliably do this.
If 0xXXXXXXX
represents hardware address(es) then you'll need to write a device driver to gain kernel access to the memory.
If it's a normal memory address then there's no guarantee that it maps to a valid memory location and you're quite likely to crash your program.
What are you really trying to do here?
I would declare the memory using a constant pointer:
byte * const AmmoBytes = (byte * const) 0xFFFF000;
Declaring the pointer as constant helps the compiler detect errors such as mistakenly changing the pointer value instead of the value pointed to by the pointer.
精彩评论