Thread: changing the address of a pointer question..

  1. #31
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    thanks

  2. #32
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    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. #33
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    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!
    Last edited by C_ntua; 10-15-2008 at 02:36 PM.

  4. #34
    Ex scientia vera
    Join Date
    Sep 2007
    Posts
    477
    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."

  5. #35
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    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.)

  6. #36
    Ex scientia vera
    Join Date
    Sep 2007
    Posts
    477
    Quote Originally Posted by nonoob View Post
    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.
    "What's up, Doc?"
    "'Up' is a relative concept. It has no intrinsic value."

  7. #37
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Depending on your rig, there are registers capable of writing up to 16bytes simultaneously.

  8. #38
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote 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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #39
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    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. #40
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    thanks

    this question was out of personal curiosity

  11. #41
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Quote Originally Posted by IceDane View Post
    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. #42
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by matsp View Post
    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.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #43
    Ex scientia vera
    Join Date
    Sep 2007
    Posts
    477
    Quote Originally Posted by nonoob View Post
    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.
    "What's up, Doc?"
    "'Up' is a relative concept. It has no intrinsic value."

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. Changing pointer inside function?
    By DrSnuggles in forum C++ Programming
    Replies: 2
    Last Post: 07-20-2008, 05:10 AM
  3. Pointer to specific memory address
    By elnerdo in forum C++ Programming
    Replies: 10
    Last Post: 05-19-2006, 07:35 AM
  4. towers of hanoi problem
    By aik_21 in forum C Programming
    Replies: 1
    Last Post: 10-02-2004, 01:34 PM
  5. Im so lost at . .
    By hermit in forum C Programming
    Replies: 18
    Last Post: 05-15-2002, 01:26 AM