Reputation: 2080
So I'm having a bit of an issue, and need help with this beginner question.
I've changed my example so that it makes sense. I should not have posted one while sleep deprived.
I'm trying to copy a pointer (lets call it p1) in order to create a second pointer (lets call it p2).
When p1 changes the object it is pointing at, I don't want p2 to change along with it, which is what appears to be happening in my program.
Essentially, here is the issue I'm having....
#include <string>
#include <iostream>
int main(){
std::cout << "Pointer Test\n\n";
int num1, num2;
int * p1, * p2;
num1 = 5;
num2 = 10;
p1 = &num1;
p2 = p1;
std::cout << "P1 is pointing at value " << *p1 << " (should be 5)\n";
std::cout << "P2 is pointing at value " << *p2 << " (should be 5)\n\n";
*p1 = num2; //Even though the value of P1 is changing, I don't want the value of P2 to change. I want it to stay on num1
std::cout << "P1 is pointing at value " << *p1 << " (should be 10)\n";
std::cout << "P2 is pointing at value " << *p2 << " (I want this to be 5, not 10)\n";
return 0;
}
/*
Assignments I've tried to make this work...
p2 = p1; //didn't work - *p2 changes when *p1 does, which I am trying to avoid
*p2 = *p1; //didn't work - caused segfault
*p2 = p1; //didn't work - can't convert int to *int, so won't compile
p2 = &(*p1); //didn't work - gave same result as p2 = p1
*/
Any enlightenment on this matter would be greatly appreciated.
Upvotes: 1
Views: 15609
Reputation: 20103
I think you have a misunderstanding of what is going on. The statement *p1 = num2;
is not changing the value of p1
, it is changing the value of whatever p1
points to. Since both p1
and p2
point to the same variable when you change its value through one pointer you will see the same value when you dereference either pointer. What you want to do is have p1
point to num2
, not take the value of num2
and store it in whatever p1
points to.
Change
*p1 = num2;
to
p1 = &num2;
Upvotes: 5
Reputation: 3943
Nonsensical code aside
When p1 changes the object it is pointing at, I don't want p2 to change along with it
If p1
and p2
point to the same object, changing the object p1
points to will change the (same) object p2
points to, period. You'll have to use a different mechanism than raw pointers to avoid that behaviour. Like making copies of the object pointed to instead of copying the pointer.
Also
Do I have to use memcpy? Or is there another way?
Since this is C++, I'd avoid memcpy
.
Defining a copy constructor or assignment operator is a better way of copying objects.
Upvotes: 1
Reputation: 1368
Consider the following code:
int a = 5, b = 10, *foo, *bar;
foo = &a; // foo now points at the address of A
bar = foo; // bar now points at the address at which foo is pointing
foo = &b; // foo now points at the address of B
cout << *foo; // prints 10
cout << *bar; // prints 5
Thats the behaviour you want to achieve if Im not mistaken.
Your code is nonsensical however. IF I'm not wrong, your mistake is that you create a second pointer which points at the address of the first pointer instead of pointing at the address from the first pointer.
Upvotes: 4
Reputation: 11434
Your first code makes no sense. If testObj isn´t a typedef to a pointer,
the *
in the first p1 assignment is wrong (it should be &
), and the second p1 assignment is wrong too, because you can´t assign a temporary object to a pointer, (only addresses to variables or from new).
And always remember to assign an address to a pointer p before using *p
If you want to copy pointers, the copy will point to the same thing as the original one and p2 = p1;
is correct. If you want to copy objects, the pointer thing has nothing to do with it, and how the object is copied should be defined in a copy constructor and operator=.
Depending on the class, memcpy can work or not, don´t rely on it for such cases.
Upvotes: 1
Reputation: 4559
There is no type like obj *
. The "*" binds tighter to "p1". So you need to change the decl from
obj * p1, p2;
To
obj *p1, *p2;
Upvotes: 0