# Thread: Accessing variables with memory address

1. ## Accessing variables with memory address

Let's say I have an int at address 0x5FA70. I have another int storing that variable. How do I use that variable to manipulate the data at the memory address stored by that variable?

Code:
```/* this is the variable i want to access. it's memory address is
0x5FA70. the kicker is I cannot use this variable in any way to
access it's value, only that memory address */
int a = 645;

/* this variable stores 0x5FA70, the address of a */
int b = 0x5FA70;```
Let's say I want to add 1 to a, then I want to assign the value of a to a new int, c. How would I do this without using the variable a in any way and only using it's address? Thanks in advance.

2. Do you know what * does when you place it in front of a variable?

3. Code:
`volatile unsigned int* const b = (unsigned int *)0x5FA70;`

4. Originally Posted by tabstop
Do you know what * does when you place it in front of a variable?
Ah, but what if a is a double, struct, float, etc?

Originally Posted by bithub
Code:
`volatile unsigned int* const b = (unsigned int *)0x5FA70;`
The big question is, how fast is that?

5. Originally Posted by ITAmember
Ah, but what if a is a double, struct, float, etc?
Why would it matter? That's not really relevant, so far as I can see (I mean, you have to know what type it is, but how you deal with the indirection doesn't change).

Originally Posted by ITAmember
The big question is, how fast is that?
It's loading a constant into a memory location. I don't know that there are any operations that take less time. (Now if you're asking about speed of using b, that's something else.)

6. The big question is, how fast is that?
You are declaring a pointer and initializing it to a value. I'd say it's pretty fast. The only valid reason I can think of for doing something like this is if you are working on a system that has memory-mapped addresses. Otherwise, you are probably doing something unnecessary and dangerous.

7. Fast compared to what?

If the value is in a register, it will be faster than accessing it through a pointer.

And with regards to "other types", you have to know what the type is of the variable. [And of course, it's not very maintainable to acess variables at fixed addresses in memory. It only take a different compiler or a change in compiler options for the variable to move to another address].

--
Mats

8. When I said "how fast is that?" I was referring to the (unsigned int *). I'm guessing it's just something to tell the compiler how to generate the code and doesn't actually use any instructions.

I was using the fixed address for demonstrations purposes, in the real program I'll use b = &a; to get the address.

9. My initial tests. My goal is to access the type member of the dynamic_int struct instance "test1". So far it does not work. I'm using Visual C++ 2008 Express Edition on a P4.

Code:
``` #include <stdio.h>

/*define a dynamic int. a dynamic object will hold the mem address of the struct holding
the current value of that dyanmic object*/
typedef struct
{
int type;
int data;
} dynamic_int;

int main()
{
/*create a dynamic integer for tests*/
dynamic_int test1;
/* initalize the data members */
test1.type = 0;
test1.data = 1;
/*print the memory addresses for the struct, the type member, and the data member*/
printf("test1 %#x, type %#x, data %#x\n", &test1, &(test1.type), &(test1.data));
/*try the print the value of the type member, then print the address of that member*/
printf("test1.type = %i, address = %#x\n", (int *)((&test1)), (&test1));
return 0;
}```

10. >> My goal is to access the type member of the dynamic_int struct instance "test1". So far it does not work.

Code:
``` #include <stdio.h>

/*define a dynamic int. a dynamic object will hold the mem address of the struct holding
the current value of that dyanmic object*/
typedef struct
{
int type;
int data;
} dynamic_int;

int main()
{
/*create a dynamic integer for tests*/
dynamic_int test1;
/* initalize the data members */
test1.type = 1024;
test1.data = 1;
/*print the memory addresses for the struct, the type member, and the data member*/
printf("test1 %#x, test1.type %#x, test1.data %#x\n", &test1, &(test1.type), &(test1.data));
/*try the print the value of the type member, then print the address of that member*/
printf("test1.type = %i, address = %#x\n", *(int *)&test1, &test1);
return 0;
}```
But keep in mind that due to potential structure padding, the member you are trying to access (except for the first) may not be where you think it is (use the offsetof macro or an alignment compiler directive to remedy this).

At any rate, if you knew the address of a variable you could simply do:

Code:
`*(int*)0x5FA70 = 1024;`
Not a very good idea, of course.

11. Here's the output

Code:
```test1 0x12ff5c, type 0x12ff5c, data 0x12ff60
test1.type = 1245020, address = 0x12ff5c```
The address for test1.type in the second line is the same as "type" in the first line. Two integers accessing the exact same memory, why do they have different values?

12. Originally Posted by ITAmember
Here's the output

Code:
```test1 0x12ff5c, type 0x12ff5c, data 0x12ff60
test1.type = 1245020, address = 0x12ff5c```
The address for test1.type in the second line is the same as "type" in the first line. Two integers accessing the exact same memory, why do they have different values?
They don't. (Note that none of the values on the first line are values, they are all addresses.)

13. When I print test1.type it comes out as 0.

14. If you're using the code I posted, you should get 1024 for test.type. AFAIK, the first member of the structure is guaranteed to have the same base address as the structure (and even your output verifies this), and so there is no way you'd get that result.

15. Are you saying there is no way for me to get 0 to print? I'm kind of confused now.

Popular pages Recent additions