-
October 26th, 2009, 07:24 PM
#1
Swap using pointers
I am trying to swap the contents of objects c1 and c2 using pointers.
I defined the function as:
void MySwap4(Complex* c1, Complex* c2)
{
Complex* temp;
temp = c1;
c1 = c2;
c2 = temp;
}
void main()
{
Complex c11(4.5, 2.5);
Complex c22(3.4, 1.1);
MySwap4(&c11, &c22);
}
Why does the swap not work? When I watch the variables inside the function block, I can see c1, c2 interchanging addresses but it does not transpire into swapped value, what does that mean?
-
October 26th, 2009, 07:31 PM
#2
Re: Swap using pointers
Because you're passing the pointers by value, not by reference. The pointers the function is working with are copies of the pointers you pass in.
Try
void MySwap4(Complex*& c1, Complex*& c2)
-
October 26th, 2009, 07:40 PM
#3
Re: Swap using pointers
^That won't work since he's passing the address of local variables in main.
The problem is that you aren't swapping the values to which c1 and c2 are pointing----you're just swapping the pointers themselves. As pointed out, since these pointers are local to the function, this doesn't affect anything.
-
October 26th, 2009, 07:55 PM
#4
Re: Swap using pointers
Originally Posted by Lindley
^That won't work since he's passing the address of local variables in main.
The problem is that you aren't swapping the values to which c1 and c2 are pointing----you're just swapping the pointers themselves. As pointed out, since these pointers are local to the function, this doesn't affect anything.
True. He needs to pass actual pointers by reference.
To the OP, what were you expecting to happen? You can't swap the actual address of objects, only the contents of pointers.
-
October 27th, 2009, 04:26 AM
#5
Re: Swap using pointers
You are both wrong (no offense), but OP clearly said "I am trying to swap the contents of objects c1 and c2 using pointers."
Your problem is that what you are swapping is addresses of local objects, and not the contents themselves. You have two solutions:
1- Swap the content, just like std::iter_swap
Code:
void MySwap4(Complex* c1, Complex* c2)
{
Complex temp = *c1; //Note this is not a pointer
*c1 = *c2;
* c2 = temp;
}
2- Swap the addresses of the objects, but pass the addresses by reference:
Code:
void MySwap4(Complex*& c1, Complex*& c2)
{
Complex* temp = c1; //Note this is not a pointer
c1 = c2;
c2 = temp;
}
You should note that this is really a standard swap, but on an object of type "complex*", which just happens to be a pointer.
The problem with this solution is that it will not work. When you call:
It will not compile. c1 and c2 are local objects, and you cannot modify their address.
A workaround to this problem is the pointer to local variable design, like this:
Code:
Complex C1;
Complex C2;
Complex* pC1 = &C1;
Complex* pC2 = &C2;
MySwap4(pC1, pC2 );
Using this method, you get the advantages of being able to rebind your pointers without the costly copy, and you don't have to manage you memory either. Just be careful of the scope of C1 or C2.
If you want a good example of of when to use the "pointer to local variable design", here is one.
Code:
int a1;
int a2;
cin >> a1;
cin >> a2
int* pSmall = &a1;
int* pBig = &a2;
if (a1 > a2)
{
MySwap4(pSmall , pBig);
}
//Now, *pSmall < *pBig
//Now you can write the rest of your algorithm with pSmall and pBig, without worrying.
-
October 27th, 2009, 04:31 AM
#6
Re: Swap using pointers
Originally Posted by monarch_dodra
You are both wrong (no offense), but OP clearly said "I am trying to swap the contents of objects c1 and c2 using pointers."
Your problem is that what you are swapping is addresses of local objects, and not the contents themselves.
Ahem...
Originally Posted by Lindley
The problem is that you aren't swapping the values to which c1 and c2 are pointing----you're just swapping the pointers themselves. As pointed out, since these pointers are local to the function, this doesn't affect anything.
Also, Lindley's observation is more accurate: it is not the addresses of local objects that are being swapped (though those objects are local to the global main function), but pointers local to the function that are swapped.
By the way, Learned, please declare your global main function as having an int return type. You need not explicitly return a value from main.
Last edited by laserlight; October 27th, 2009 at 04:34 AM.
-
October 27th, 2009, 05:07 AM
#7
Re: Swap using pointers
Originally Posted by laserlight
Ahem...
Also, Lindley's observation is more accurate: it is not the addresses of local objects that are being swapped (though those objects are local to the global main function), but pointers local to the function that are swapped.
By the way, Learned, please declare your global main function as having an int return type. You need not explicitly return a value from main.
Oops. I may have been to fast to read your post. It just seemed you were jumping on how to pass pointers by reference to swap them, that I may not have read the posts in full.
-
October 27th, 2009, 07:42 AM
#8
Re: Swap using pointers
Originally Posted by Learned
I am trying to swap the contents of objects c1 and c2 using pointers.
std::iter_swap?
"It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
Richard P. Feynman
-
October 27th, 2009, 12:49 PM
#9
Re: Swap using pointers
Thank you all. Good information. I am trying to learn to use C++ in Finance so working through the book called "C++ for Financial Engineers" by Duffy. I learned C++ in undergrads but since then has pretty much rusted.
So, any advice to augment my practical understanding in programming will be helpful as well.
-
October 28th, 2009, 03:37 AM
#10
Re: Swap using pointers
Originally Posted by Learned
So, any advice to augment my practical understanding in programming will be helpful as well.
Get a good understanding of the Standard Template Library (STL)
Some useful resources...
http://www.cplusplus.com/reference/
http://www.cppreference.com/wiki/
"It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
Richard P. Feynman
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|