Thread: swap of variables without tmp

  1. #1
    Lost in the C ZaC's Avatar
    Join Date
    Jun 2008
    Location
    Italy
    Posts
    47

    swap of variables without tmp

    Is it possible?
    Some months ago, a programmer told me it is, but he didn't tell me how. I'm still thinking about it.
    Thanks in advance
    Sorry for my bad English
    and also for my bad programming style...

    ZaC'ZaCoder (?!)

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Try searching the forum or internet for it. You know, with key words that are similar to your title...


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by ZaC View Post
    Is it possible?
    Yes. You need a phenomenal sense of balance tho*, so we try to encourage people to just go pedestrian and use the "tmp".

    *because, logically, the variables must pass each other in mid air
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    Apprentice
    Join Date
    Oct 2006
    Location
    LA
    Posts
    53
    Here's one way to swap 2 integer variables a and b.

    Code:
    a= a+b
    b= a-b
    a= a-b

  5. #5
    Registered User
    Join Date
    Jun 2009
    Location
    undisclosed
    Posts
    5
    Problem w/ the above code is that it might cause overflows.

    Code:
    a = a^b;
    b = a^b;
    a = a^b;

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > Problem w/ the above code is that it might cause overflows.
    Problem with the above code, it's only guaranteed on unsigned ints.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Now that you know, treat it like a piece of trivia.

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Or they could have just searched, learned ALL of this, because it's been posted a hundred times here, and saved each one of us the trouble of typing it all yet again!


    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Give it time, we haven't seen the super-compressed (and even more broken) 1-liner XOR swap
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  10. #10
    Lost in the C ZaC's Avatar
    Join Date
    Jun 2008
    Location
    Italy
    Posts
    47
    Quote Originally Posted by quzah View Post
    Try searching the forum or internet for it. You know, with key words that are similar to your title...


    Quzah.
    You are right, it was very late and sleep made me to write without searching (I use it, don't you worry)

    Quote Originally Posted by quzah View Post
    Or they could have just searched, learned ALL of this, because it's been posted a hundred times here, and saved each one of us the trouble of typing it all yet again!


    Quzah.
    what a big trouble... if you are busy just don't care

    Quote Originally Posted by Salem View Post
    Give it time, we haven't seen the super-compressed (and even more broken) 1-liner XOR swap
    it's what I'm looking for, I already know the lazyme method, but becouse of my poor english and of my tiredness I forgot to write you about it.
    So is not good the 1-linear xor swap?
    Sorry for my bad English
    and also for my bad programming style...

    ZaC'ZaCoder (?!)

  11. #11
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    I never knew poor English makes people forget things...

    Because of my poor German I forgot to feed my dog.

  12. #12
    Lost in the C ZaC's Avatar
    Join Date
    Jun 2008
    Location
    Italy
    Posts
    47
    Code:
    if ((enlgish == poor) && tired) printf("%s",few);
    else printf("%s",more);
    Sorry for my bad English
    and also for my bad programming style...

    ZaC'ZaCoder (?!)

  13. #13
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by ZaC View Post
    So is not good the 1-linear xor swap?
    You are only allowed to change a variable once in a sequence point [1], and the one-liner xor swap does modify the same variable more than once (because of the way that it swaps x with y, it changes x twice). This is not forbidded to write in the compiler, it is what is called "undefined behaviour" - the compiler may treat do "what it feels like" in this sequence of operations - it may decide to break it up in ways you wouldn't expect. Undefined behaviour means "anything can happen": nuclear missiles launch, incorrect result, or even correct result. It's a bit like driving across a junction when the light is red in your direction. If you are lucky, nothing bad happpens. If you have a bit of luck, you have a small collision, and if you are really unlucky, there is a BIG lorry crossing in the other direction, and your car is crushed.

    [1] Sequence point is normally a statement in C, but there are some exceptions. Basically, a sequence point is the point at which the preceding code is completed, so if you say:
    Code:
    a = b + c + d;
    the semicolon indicates the sequence point. If you were to look at the place where a is stored somewhere WITHIN the code generated for that line, it may contain the old value before that line, some partial value, or the whole sum of b + c + d, depending on where you look at it [and it may contain some spurious intermediate results at some point too, such as c + d or b + c, or even b + d, depending on how the compiler composes the code].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  14. #14
    Registered User
    Join Date
    Jun 2009
    Location
    undisclosed
    Posts
    5
    Quote Originally Posted by Salem View Post
    Give it time, we haven't seen the super-compressed (and even more broken) 1-liner XOR swap
    Code:
    b = (a ^= (b ^= a)) ^ b;  
    a ^= b ^= a ^= b; // O_O
    *read matsp post above for problems...

  15. #15
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    You can do xor swap just fine in two lines:
    Code:
    b ^= a ^= b;
    a ^= b;
    And then of course there is this:
    Code:
    b ^= a ^= b, a ^= b;
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Best way to avoid using global variables
    By Canadian0469 in forum C++ Programming
    Replies: 7
    Last Post: 12-18-2008, 12:02 PM
  2. Declaring an variable number of variables
    By Decrypt in forum C++ Programming
    Replies: 8
    Last Post: 02-27-2005, 04:46 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. hwnd and variables in them
    By underthesun in forum Windows Programming
    Replies: 6
    Last Post: 01-16-2005, 06:39 PM
  5. functions to return 2 variables?
    By tim in forum C Programming
    Replies: 5
    Last Post: 02-18-2002, 02:39 PM