Copying pointers

Discussion in 'C Programming' started by Johs32, Mar 14, 2006.

  1. Johs32

    Johs32 Guest

    I have made a pcb pointer called current. I have then made another pointer
    (old_thread) that I initialize to point to the same area that current
    points to.

    If I make changes to current afterwards it also changes what old_thread
    points to.

    Why do old_thread change when I change current afterwards? If I make changes
    to current AFTER copying it to old_thread, I don't see why this affects
    old_thread.

    struct pcb {
    int state;

    };

    static struct pcb T1;
    struct pcb *current = &T1;


    struct pcb *old_thread = current;
    printf("Before: %d\n",old_thread->state); //this prints 0
    current->state = 222;
    printf("After: %d\n",old_thread->state); // this prints 222

    Why does the above code not print 0 in both cases? When I change current it
    should not affect old_thread.

    Johs
     
    Johs32, Mar 14, 2006
    #1
    1. Advertising

  2. Johs32 said:

    > struct pcb *old_thread = current;
    > printf("Before: %d\n",old_thread->state); //this prints 0
    > current->state = 222;
    > printf("After: %d\n",old_thread->state); // this prints 222
    >
    > Why does the above code not print 0 in both cases? When I change current
    > it should not affect old_thread.


    You didn't change current. You changed the thing that current points to.

    Since old_thread has the same value as current, naturally it points to the
    same object that current points to. So when that thing changes, you can
    observe the change through either pointer.

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at above domain (but drop the www, obviously)
     
    Richard Heathfield, Mar 14, 2006
    #2
    1. Advertising

  3. Johs32

    Johs32 Guest

    Richard Heathfield wrote:

    > Johs32 said:
    >
    >> struct pcb *old_thread = current;
    >> printf("Before: %d\n",old_thread->state); //this prints 0
    >> current->state = 222;
    >> printf("After: %d\n",old_thread->state); // this prints 222
    >>
    >> Why does the above code not print 0 in both cases? When I change current
    >> it should not affect old_thread.

    >
    > You didn't change current. You changed the thing that current points to.
    >
    > Since old_thread has the same value as current, naturally it points to the
    > same object that current points to. So when that thing changes, you can
    > observe the change through either pointer.
    >


    ok is there someway to store what a pointer points to at a specific time in
    the execution and "save" this for later use eventhough the original pointer
    gets modified later on?
     
    Johs32, Mar 14, 2006
    #3
  4. On 2006-03-14, Johs32 <> wrote:
    > I have made a pcb pointer called current. I have then made another pointer
    > (old_thread) that I initialize to point to the same area that current
    > points to.
    >
    > If I make changes to current afterwards it also changes what old_thread
    > points to.
    >
    > Why do old_thread change when I change current afterwards? If I make changes
    > to current AFTER copying it to old_thread, I don't see why this affects
    > old_thread.
    >
    > struct pcb {
    > int state;
    >
    > };
    >
    > static struct pcb T1;
    > struct pcb *current = &T1;
    >
    >
    > struct pcb *old_thread = current;
    > printf("Before: %d\n",old_thread->state); //this prints 0
    > current->state = 222;
    > printf("After: %d\n",old_thread->state); // this prints 222
    >
    > Why does the above code not print 0 in both cases? When I change current it
    > should not affect old_thread.
    >
    > Johs
    >


    You will kick yourself here :)

    Because they still both point to the same pcb object : T1.

    Run a debugger and you would see that both current
    and old_thread contain exactly the same pointer values.

    Also, dont forget to initialise your "state" : its nicer that way.

    --
    Debuggers : you know it makes sense.
    http://heather.cs.ucdavis.edu/~matloff/UnixAndC/CLanguage/Debug.html#tth_sEc
     
    Richard G. Riley, Mar 14, 2006
    #4
  5. On Tuesday 14 March 2006 08:39, Johs32 opined (in
    <dv5vf8$87c$-c.dk>):

    > Richard Heathfield wrote:
    >
    >> Johs32 said:
    >>
    >>> struct pcb *old_thread = current;
    >>> printf("Before: %d\n",old_thread->state); //this prints 0
    >>> current->state = 222;
    >>> printf("After: %d\n",old_thread->state); // this prints 222
    >>>
    >>> Why does the above code not print 0 in both cases? When I change
    >>> current it should not affect old_thread.

    >>
    >> You didn't change current. You changed the thing that current points
    >> to.
    >>
    >> Since old_thread has the same value as current, naturally it points
    >> to the same object that current points to. So when that thing
    >> changes, you can observe the change through either pointer.
    >>

    >
    > ok is there someway to store what a pointer points to at a specific
    > time in the execution and "save" this for later use eventhough the
    > original pointer gets modified later on?


    Yes. Just make a copy of your data somewhere else (e.g. use temporary
    variable).

    int i;
    int *pi = &i;
    int temp;

    *pi = 42; /* `i` is 42 */
    temp = *pi; /* `temp` is now 42 */
    *pi = 2*42; /* `i` is now 84, `temp` is still 42 */

    OTH, if you're worried that the pointer may later point to something
    else, then you just need to save its original value into another
    pointer.

    int i, j;
    int *pi;
    int *ptmp;

    pi = &i; /* `pi` points to `i` */
    ptmp = pi; /* `ptmp` now points to `i`, too */
    pi = &j; /* `pi` points to `j` now, `ptmp` still points to `i` */

    --
    BR, Vladimir

    Madness takes its toll.
     
    Vladimir S. Oka, Mar 14, 2006
    #5
  6. Johs32

    Eric Sosman Guest

    Johs32 wrote On 03/14/06 03:14,:
    > I have made a pcb pointer called current. I have then made another pointer
    > (old_thread) that I initialize to point to the same area that current
    > points to.
    >
    > If I make changes to current afterwards it also changes what old_thread
    > points to.
    >
    > Why do old_thread change when I change current afterwards? If I make changes
    > to current AFTER copying it to old_thread, I don't see why this affects
    > old_thread.


    Write down the address of your house on a slip of paper;
    the paper is a "pointer" to your house. Make a photocopy of
    the paper, obtaining a second "pointer." Give the photocopy
    to a house painter (give the painter a pointer) and tell him
    to paint the house at the given address, changing its color.

    After the house has been repainted, pick up the original
    piece of paper and visit the address you find written on it.
    Did the painter's actions change the color of the house at
    that address?

    The two pointers -- the original slip of paper and the
    photocopy -- exist independently of each other, but they
    both refer to the same house. It doesn't matter which of
    the two pointers you use to locate the house; you'll get to
    the same house either way. The pointers are not the same as
    the thing they point to.

    --
     
    Eric Sosman, Mar 14, 2006
    #6
  7. Johs32

    Jack Klein Guest

    On Tue, 14 Mar 2006 09:39:04 +0100, Johs32 <> wrote in
    comp.lang.c:

    > Richard Heathfield wrote:
    >
    > > Johs32 said:
    > >
    > >> struct pcb *old_thread = current;
    > >> printf("Before: %d\n",old_thread->state); //this prints 0
    > >> current->state = 222;
    > >> printf("After: %d\n",old_thread->state); // this prints 222
    > >>
    > >> Why does the above code not print 0 in both cases? When I change current
    > >> it should not affect old_thread.

    > >
    > > You didn't change current. You changed the thing that current points to.
    > >
    > > Since old_thread has the same value as current, naturally it points to the
    > > same object that current points to. So when that thing changes, you can
    > > observe the change through either pointer.
    > >

    >
    > ok is there someway to store what a pointer points to at a specific time in
    > the execution and "save" this for later use eventhough the original pointer
    > gets modified later on?


    Your new question makes it appear that you have not completely
    overcome your original confusion, because you are still talking about
    "the original pointer gets modifier". Of course, the pointer is not
    being modified at all, rather the object pointed to is modified. The
    pointer itself remains completely unchanged.

    Of course, since you have a pointer to some object, you may copy that
    object.

    struct pcb save_it = *old_thread;

    This saved copy will not be changed by any modifications to the
    original object (not pointer), no matter how the original object is
    accessed.

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://c-faq.com/
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++
    http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
     
    Jack Klein, Mar 15, 2006
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Phil
    Replies:
    1
    Views:
    666
    llewelly
    Sep 16, 2003
  2. Martin Magnusson
    Replies:
    5
    Views:
    420
    Ron Natalie
    Sep 24, 2003
  3. jimjim

    copying an array of pointers to structures

    jimjim, Dec 13, 2003, in forum: C Programming
    Replies:
    5
    Views:
    427
    Barry Schwarz
    Dec 14, 2003
  4. arnuld

    Pointers are pased by copying ?

    arnuld, Sep 16, 2008, in forum: C Programming
    Replies:
    5
    Views:
    366
    arnuld
    Sep 16, 2008
  5. cerr

    pointers, pointers, pointers...

    cerr, Apr 7, 2011, in forum: C Programming
    Replies:
    12
    Views:
    714
Loading...

Share This Page