Reputation: 12631
Is there a way to set an integer variable at the absolute address 0x67a9
to the value
0xaa55
? The compiler is a pure ANSI compiler.
How to accomplish this?
This is a program related to embedded systems. As in there we can access a specific memory location.
Upvotes: 4
Views: 23309
Reputation: 1255
Depends on how the compiler compiles I would think.
-If when compiling it decodes each instance of a variable to a final address you wouldn't be able to change the address of a regular variable.
-If when compiling it decodes each instance of a variable to reference a spot of a lookup table for its address this might be possible, though you best make sure the address you set it to is allocated.
For example say the 'lookup' address of variable A is 0x1000, and the value found at 0x1000 is 0x2000, so the contents of A is at 0x2000. To change the address of A you simply change the value at 0x1000. Set the value of 0x1000 to 0x3000 and then the contents of A is at 0x3000.
Ya this is how pointers work, but it might be possible that under the hood a compiler might treat them the same (regular variables simply being pointers that are automatically dereferenced.) Of course you could also just make all variables pointers and prepare yourself for a lot of de-referencing in your code.
Upvotes: 0
Reputation: 61991
int *ptr = (int *) 0x67a9;
*ptr = 0xaa55; // MBR signature ?
EDIT
You cannot change the address of a variable, you can only point to some address with a pointer, which is shown above.
Upvotes: 7