4

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.

5
  • why would you want to do that ? what have you tried ? what doesn't work ? any error messages ? Commented Aug 25, 2011 at 18:07
  • I dont know how this can be done as we cant modify the address of the variable explicitly. Commented Aug 25, 2011 at 18:10
  • 1
    you can'T - you can only create a pointer pointing to that location and modify the value at that location through that pointer... Commented Aug 25, 2011 at 18:12
  • Thanks Yahia for making me to understand.. Pointer will hold the address.So we could able to change the address only with the pointer variable. Commented Aug 25, 2011 at 18:14
  • yes... see for example the answers of phoxis and Bill - they create a pointer to that location, then change the value at that address by assigning a value to the "dereferenced" pointer Commented Aug 25, 2011 at 18:19

3 Answers 3

11

Try this:

*((int*)0x67a9) = 0xaa55;

Sign up to request clarification or add additional context in comments.

Comments

7
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.

4 Comments

Or better, #define myvar (*(int *)0x67a9) -- then it works just like an ordinary variable of type int.
*ptr=0xaa55 when i uesd this i got a run time error. I'm using visual studio2008.
naturally, because in an OS environment where the memory access is protected addressing some constant memory location like 0x67a9 is illegal, as you have not explicitly allocated it. You cannot just randomly use any address and access it in an OS with memory protection.
Is there any reason why you can't change the memory address of a variable? A variable is just a name so you should be able to swap two names, right? Like in C# you can swap the names of two objects and they point to different memory locations.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.