thanks
thanks
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
Think of this:
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.Code:int a; int* p;
Now, you cannot change this memory addresses. Whatever you do a will be at [0x111] and p at [0x112]. Now lets do this:
Your memory will look like this:Code:a = 5; p = 6;
So the memory address 0x111 is variable a. This are the same in a way.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)
Now, lets do this:
Now you will have:Code:p = &a;
Because the memory address of a is 0x111. That value you assign to p. That is all.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)
Now if you do this:
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.Code:int c; c = *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:
Finally, you can do this:Code:[0x111] = 5 (variable a) [0x112] = 0x111 (variable p) [0x113] = 5 (variable c) ... [0x200] = something (a memory location not used yet)
You will have this:Code:p = 0x200;
Whats wrong with that? If you do this:Code:[0x111] = 5 (variable a) [0x112] = 0x200 (variable p) [0x113] = 5 (variable c) ... [0x200] = something (a memory location not used yet)
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.Code:c = *p;
Final note. The address 0x111,... are chosen by the program! You have not control on where the variables will be stored!
Last edited by C_ntua; 10-15-2008 at 02:36 PM.
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.
"What's up, Doc?"
"'Up' is a relative concept. It has no intrinsic value."
Yes you can modify a pointer which points to itself 4 bytes at a time.
Output:Code:void *pnt; pnt = &pnt; printf("%i %ld\n", &pnt, (long)pnt); *(long *)pnt = 1; printf("%i %ld\n", &pnt, (long)pnt);
1309944 1309944
1309944 1
Your mileage may vary.
(Ouch, this hurt my brain.)
"What's up, Doc?"
"'Up' is a relative concept. It has no intrinsic value."
Depending on your rig, there are registers capable of writing up to 16bytes simultaneously.
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.Originally Posted by transgalactic2
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
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?
thanks
this question was out of personal curiosity
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.
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.
"What's up, Doc?"
"'Up' is a relative concept. It has no intrinsic value."