A question about swap

C

Chad

I hope this question doesn't appear twice. I tried posting it once,
however, I got a stupid 505 internal error on google. When this
happens, the post will either post 30 minutes later or won't post at
all.

Given

void swap(int *x, int *y)
{
int temp;

temp = *x;
*x = *y;
*y = temp;
}

How does having a temp variable allow for anything to safely swap with
itself? Say x = 10 and y = 10. What would happen in the absence of
temp?

Chad
 
I

Ian Collins

Chad said:
I hope this question doesn't appear twice. I tried posting it once,
however, I got a stupid 505 internal error on google. When this
happens, the post will either post 30 minutes later or won't post at
all.

Given

void swap(int *x, int *y)
{
int temp;

temp = *x;
*x = *y;
*y = temp;
}

How does having a temp variable allow for anything to safely swap with
itself? Say x = 10 and y = 10. What would happen in the absence of
temp?
Try writing the function without it and see what you get.
 
S

santosh

Chad said:
I hope this question doesn't appear twice. I tried posting it once,
however, I got a stupid 505 internal error on google. When this
happens, the post will either post 30 minutes later or won't post at
all.

Given

void swap(int *x, int *y)
{
int temp;

temp = *x;
*x = *y;
*y = temp;
}

How does having a temp variable allow for anything to safely swap with
itself? Say x = 10 and y = 10. What would happen in the absence of
temp?

Well, x's value will get irretrievably overwritten won't it? If you
really dislike the temporary variable, then look-up the so-called "XOR
swap".

<http://en.wikipedia.org/wiki/XOR_swap_algorithm>
 
C

CBFalconer

santosh said:
Chad wrote:
.... snip ...


Well, x's value will get irretrievably overwritten won't it? If you
really dislike the temporary variable, then look-up the so-called
"XOR swap".

<http://en.wikipedia.org/wiki/XOR_swap_algorithm>

No, don't. That mechanism is faulty. Just take a look at the
result:

XCHG(a, a);

from it. Which is something that can easily arise from a loop.
Nothing to gain, and lots to lose.
 
J

Joachim Schmitz

Richard said:
CBFalconer said:


Why not?


If you mean that the XOR mechanism is faulty, then whether I agree
depends on what you mean by "faulty". If you simply mean that the
mechanism for updating the Wikipedia is faulty because it means any
fool can introduce random mistakes at any time, then I certainly
agree. Nevertheless, santosh did not say "use the XOR swap" - he
merely suggested looking it up. I just checked the Wikipedia
reference he gave and, at the time that I looked, it seemed to me
adequately to cover the various drawbacks of using the XOR swap.
"Look it up" does not mean "look it up and then use it" - it merely
means "look it up".


Just take a look at this text that was present in the Wikipedia entry
at the time I saw it a few moments ago:

"The XOR swap is also complicated in practice by aliasing. As noted
above, if an attempt is made to XOR-swap the contents of some
location with itself, the result is that the location is zeroed out
and its value lost. Therefore, XOR swapping must not be used blindly
in a high-level language if aliasing is possible."

Okay, that text might have changed or vanished by the time you look,
Nothing really ever vanishes from wikipedia, you can always look it up in
the history of a document. Also wikipedia has so-called permanent links
which will never change, e.g.:
http://en.wikipedia.org/w/index.php?title=XOR_swap_algorithm&oldid=223694901

Bye, Jojo
 
R

Richard

Mark McIntyre said:
Huh? Except when an uber-admin wipes it from the database.

or a nuclear war wipes the planet? Thanks for that piece of
uber-pedantry.
 
K

Keith Thompson

Richard Heathfield said:
CBFalconer said:

Why not?


If you mean that the XOR mechanism is faulty, then whether I agree depends
on what you mean by "faulty". If you simply mean that the mechanism for
updating the Wikipedia is faulty because it means any fool can introduce
random mistakes at any time, then I certainly agree. Nevertheless, santosh
did not say "use the XOR swap" - he merely suggested looking it up. I just
checked the Wikipedia reference he gave and, at the time that I looked, it
seemed to me adequately to cover the various drawbacks of using the XOR
swap. "Look it up" does not mean "look it up and then use it" - it merely
means "look it up".

But it's reasonable to infer that "Look it up" means at least "Look it
up and consider using it". That's certainly how I took it, especially
considering the precondition: "If you really dislike the temporary
variable". If the point is to look it up and then decide, based on
the description, not to use it, then the precondition would be
pointless. I assumed that santosh didn't write point pointless words,
and therefore that he was actually implicitly suggesting that Chad
should consider *using* the xor swap. And even if he wasn't, it seems
very likely that Chad would take it that way.
Just take a look at this text that was present in the Wikipedia entry at
the time I saw it a few moments ago:

"The XOR swap is also complicated in practice by aliasing. As noted above,
if an attempt is made to XOR-swap the contents of some location with
itself, the result is that the location is zeroed out and its value lost.
Therefore, XOR swapping must not be used blindly in a high-level language
if aliasing is possible."

Okay, that text might have changed or vanished by the time you look, but it
seems to me that, in the form stated above, it covers the ground properly.

Not quite. It covers some of the problems, but it doesn't mention the
possibility of trap representations in intermediate results.

Chad: By all means read the Wikipedia article, but don't use the xor
swap trick in your own code.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,262
Messages
2,571,052
Members
48,769
Latest member
Clifft

Latest Threads

Top