# Thread: changing the address of a pointer question..

1. thanks

2. It's the compiler's job to associate the symbol foo with a memory location so the behaviour is undefined if you try to relocate it to another place or it could become a foobar

3. Think of this:
Code:
```int a;
int* p;```
You declare two variables. One variable of int and one variable of a pointer to int. You have allocated two memory spaces (thus variables). Each memory space that holds the variables has a fixed memory address. Lets say [0x111] and [0x112] respectively.
Now, you cannot change this memory addresses. Whatever you do a will be at [0x111] and p at [0x112]. Now lets do this:
Code:
```a = 5;
p = 6;```
Your memory will look like this:
Code:
```[0x111] = 5 (variable a)
[0x112] = 6 (variable p)
[0x113] = something (a memory location not used yet)
...
[0x200] = something (a memory location not used yet)```
So the memory address 0x111 is variable a. This are the same in a way.
Now, lets do this:
Code:
`p = &a;`
Now you will have:
Code:
```[0x111] = 5 (variable a)
[0x112] = 0x111 (variable p)
[0x113] = something (a memory location not used yet)
...
[0x200] = something (a memory location not used yet)```
Because the memory address of a is 0x111. That value you assign to p. That is all.
Now if you do this:
Code:
```int c;
c = *p;```
Then lets say c goes to [0x113]. Now c will have the value of where p points to. That means it will have the value of the variable that is stored in the value of p.
Which means in our example that c will have the value of the memory location [0x111] because p has the value of 0x111. That memory location (which is effectively variable a) has the value 5. So c will have the value 5 assign to it. So:
Code:
```[0x111] = 5 (variable a)
[0x112] = 0x111 (variable p)
[0x113] = 5 (variable c)
...
[0x200] = something (a memory location not used yet)```
Finally, you can do this:
Code:
`p = 0x200;`
You will have this:
Code:
```[0x111] = 5 (variable a)
[0x112] = 0x200 (variable p)
[0x113] = 5 (variable c)
...
[0x200] = something (a memory location not used yet)```
Whats wrong with that? If you do this:
Code:
`c = *p;`
then you will assign to c the value that is on the memory address [0x200] which is not a variable used by the program. It is not allocated. So it cannot be used (at least with safety) and you have no idea what it's value.

Final note. The address 0x111,... are chosen by the program! You have not control on where the variables will be stored!

4. A pointer has an address. It also contains and address, and using the dereferencing operator, you can access the value stored at the address that the pointer contains. You can also modify the address the pointer contains, but modifying the address of the pointer is not possible because it's simply decided by the operating system where it's located.

A pointer can point to itself, and modify itself, although this will most likely cause issues. I am not sure you can actually modify a pointer 4 bytes at a time, that is beyond the scope of my knowledge of the inner workings of the operating systems I used(Anyone who can elaborate?) but I know for a fact that a pointer can modify itself, but if you write one byte to it, you must consider that the address contained in the pointer will also change, meaning that any subsequent writes will be completely fubar'd.

5. Yes you can modify a pointer which points to itself 4 bytes at a time.
Code:
```void *pnt;

pnt = &pnt;
printf("&#37;i %ld\n", &pnt, (long)pnt);
*(long *)pnt = 1;
printf("%i %ld\n", &pnt, (long)pnt);```
Output:
1309944 1309944
1309944 1

Your mileage may vary.

(Ouch, this hurt my brain.)

6. Originally Posted by nonoob
Yes you can modify a pointer which points to itself 4 bytes at a time.
Code:
```void *pnt;

pnt = &pnt;
printf("%i %ld\n", &pnt, (long)pnt);
*(long *)pnt = 1;
printf("%i %ld\n", &pnt, (long)pnt);```
Output:
1309944 1309944
1309944 1

Your mileage may vary.

(Ouch, this hurt my brain.)
Alright, cool. I was just wondering if the operating system did in fact have the hardware capabilities to actually write more than one byte each time. But it makes sense that it can, since I guess the latter would be insanely inefficient.

7. Depending on your rig, there are registers capable of writing up to 16bytes simultaneously.

8. Originally Posted by transgalactic2
you want to present the number 42 as an integer and put it into foo_ptr
No, I want (or rather, you want, for reasons I cannot fathom) to "present" the number 42 as an address and store that address in foo_ptr. Remember, a pointer is not an (ordinary) integer. A pointer is an address.

9. I guess by now, and I am certain I am probably not the first to have asked this during the course of this thread. But why would you want to do this in the first place? What possible advantage can come of this practice?

10. thanks

this question was out of personal curiosity

11. Originally Posted by IceDane
Alright, cool. I was just wondering if the operating system did in fact have the hardware capabilities to actually write more than one byte each time. But it makes sense that it can, since I guess the latter would be insanely inefficient.
I'm not quite sure you understand.

Since the beginning of time the hardware chips could do 1, 2, 4, 8, 10 bytes at a time (if you remember 80-bit internal floating-point registers on math coprocessors). Some of the MMX and further graphics register extensions can do more, as was pointed out.

I guess it begs the question as to the atomicity of internal operations. Do addresses get evaluated over the time interval they are changing? Surely flipping 64 bits takes some finite amount of time. I would suggest that what's really going on is that there are temporary latches, holding registers, whose contents get promoted to other registers... then written back. These things happen within the time resolution (clock), and thus are not subject to partial re-evaluation at any sub-time level.

12. Originally Posted by matsp
It would be a bit like you going out on the street and starting to change the numbers on the house... Once you live in a particular house, you can't just change it's number [I'm sure the local government can, but that is something else] - and a variable is located by the "local government", which is this case is the combination of compiler and operating system (the compiler being your "local government", and the OS being the "central government" for the entire system - the compiler still needs to abide by the OS's rules).

--
Mats
Actually, I would see changing a house's as trying to uproot a house and move it to a new address
And if that house is a variable, then you can understand why it's just not possible/feasible to change its address.

Btw, c_ntua, nice demonstration of how pointer work
I'm going to bookmark it.

13. Originally Posted by nonoob
I'm not quite sure you understand.

Since the beginning of time the hardware chips could do 1, 2, 4, 8, 10 bytes at a time (if you remember 80-bit internal floating-point registers on math coprocessors). Some of the MMX and further graphics register extensions can do more, as was pointed out.

I guess it begs the question as to the atomicity of internal operations. Do addresses get evaluated over the time interval they are changing? Surely flipping 64 bits takes some finite amount of time. I would suggest that what's really going on is that there are temporary latches, holding registers, whose contents get promoted to other registers... then written back. These things happen within the time resolution (clock), and thus are not subject to partial re-evaluation at any sub-time level.
Thanks.

And no, to be perfectly honest I very, very limited knowledge of how this works on the hardware level. I'll have to look into that. Just not sure what to search for / where to start, but thanks for the explanation.

Popular pages Recent additions