Copying pointers

J

Johs32

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
 
R

Richard Heathfield

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.
 
J

Johs32

Richard said:
Johs32 said:


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?
 
R

Richard G. Riley

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.
 
V

Vladimir S. Oka

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` */
 
E

Eric Sosman

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.
 
J

Jack Klein

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,737
Latest member
Georgeengab

Latest Threads

Top