CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Oct 2009
    Location
    NY, USA
    Posts
    191

    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?

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    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)

  3. #3
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    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.

  4. #4
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Swap using pointers

    Quote Originally Posted by Lindley View Post
    ^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.

  5. #5
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    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:
    Code:
    MySwap4(&c1, &c2)
    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.

  6. #6
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Swap using pointers

    Quote 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...
    Quote 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.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  7. #7
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: Swap using pointers

    Quote Originally Posted by laserlight View Post
    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.

  8. #8
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,727

    Re: Swap using pointers

    Quote Originally Posted by Learned View Post
    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

  9. #9
    Join Date
    Oct 2009
    Location
    NY, USA
    Posts
    191

    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.

  10. #10
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,727

    Re: Swap using pointers

    Quote Originally Posted by Learned View Post
    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
  •  





Click Here to Expand Forum to Full Width

Featured