Thread: Why couldn't I swap the values of two variables with *temp?

  1. #16
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Remember that dereferencing c on the left hand side (*c) will overwrite the pointer.

    I think you need to realize that you ultimately want to swap values. The temp variable is supposed to save your first value so that when you put the second value where the first value was, you can still put the temp value where the second value was. Your function would work if temp weren't a pointer type.
    Code:
    void swap(element *foo, element *bar)
    {
       element temp = *foo;
       *foo = *bar;
       *bar = temp;
    }
    That does exactly as I said.

    If temp has to be a pointer, there is little point to it, because you have to new the extra space, and delete it before the function ends. The "extra space" part kinda has no way around it.

  2. #17
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Also when i was in your position i found interesting this piece of code.No extra value needed
    Code:
    void swap(int *a, int *b)
    {
          *a -= *b;
          *b += *a; // b gets the original value of a
          *a = (*b - *a); // a gets the original value of b
    }

  3. #18
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Interesting, but generally best avoided since the intermediate results can be out of the range of int. The XOR swap has a related idea, but likewise is best avoided as it only works on integral types.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #19
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Quote Originally Posted by laserlight View Post
    Interesting, but generally best avoided since the intermediate results can be out of the range of int. The XOR swap has a related idea, but likewise is best avoided as it only works on integral types.
    Yes,but if someone says now that swap can be done only with an extra variable you can answer this piece of code does not need one

  5. #20
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by std10093
    Yes,but if someone says now that swap can be done only with an extra variable you can answer this piece of code does not need one
    Then when that someone uses input designed to cause your function's computations to overflow/underflow, you will have no words left to say but "oops". Really, if you want to note special cases, go ahead, but in general, what we are talking about is that some extra space is needed for swaps. If you want to contradict, then you should state note that your counterexample is a special case that does not really contribute to this discussion, and is often just trivia that is a poor example to show to a beginner.

    (EDIT: and yes, this example is not new to me; I'm sure both whiteflags and I have known of it long before you joined this forum. My caustic reaction here is because it is so typical to bring up such examples without qualifying that it is trivia rather than good practice, and the fact that stahta01 has the temerity to "like" it and you dare to defend it irks me.)
    Last edited by laserlight; 10-06-2012 at 09:14 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #21
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    No intention to contradict I did not came up with the code.Someone else showed that to me and i thought that it would be good to share it with the others..I did not want in any case to play the smart guy or something like that
    And please notice that i wrote some comments,there was nothing else that i was able to come up with to explain the code

    edit - >Would you like me to delete the code?
    Last edited by std10093; 10-06-2012 at 10:59 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 100
    Last Post: 06-21-2010, 02:22 AM
  2. swap of variables without tmp
    By ZaC in forum C Programming
    Replies: 16
    Last Post: 06-13-2009, 02:28 PM
  3. Swap two variables with out third variable
    By nkanthikiran in forum C Programming
    Replies: 3
    Last Post: 01-30-2005, 01:33 PM
  4. Why ^ will swap values?
    By Nutshell in forum C Programming
    Replies: 20
    Last Post: 04-24-2002, 01:12 PM
  5. Swap function using no temp variable
    By khirok in forum C Programming
    Replies: 4
    Last Post: 04-03-2002, 08:11 PM

Tags for this Thread