473,480 Members | 1,585 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

A question about swap

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
Jul 5 '08 #1
10 1470
Chad wrote:
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.

--
Ian Collins.
Jul 5 '08 #2
Chad <cd*****@gmail.comwrites:
I hope this question doesn't appear twice.
[...]

It did. See my response to the other one.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jul 5 '08 #3
Chad wrote:
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>

Jul 5 '08 #4
santosh wrote:
Chad wrote:
.... snip ...
>
>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>
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.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.
Jul 5 '08 #5
CBFalconer said:
santosh wrote:
>Chad wrote:
... snip ...
>>
>>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>

No, don't.
Why not?
That mechanism is faulty.
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 the
result:

XCHG(a, a);

from it. Which is something that can easily arise from a loop.
Nothing to gain, and lots to lose.
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.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Jul 5 '08 #6
Richard Heathfield wrote:
CBFalconer said:
>santosh wrote:
>>Chad wrote:
... snip ...
>>>
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>

No, don't.

Why not?
>That mechanism is faulty.

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 the
result:

XCHG(a, a);

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

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?...ldid=223694901

Bye, Jojo
Jul 5 '08 #7
Joachim Schmitz wrote:
Richard Heathfield wrote:
>>
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.
Huh? Except when an uber-admin wipes it from the database.

--
Mark McIntyre

CLC FAQ <http://c-faq.com/>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>
Jul 5 '08 #8
Mark McIntyre <ma**********@TROUSERSspamcop.netwrites:
Joachim Schmitz wrote:
>Richard Heathfield wrote:
>>>
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.

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.

Jul 5 '08 #9
Richard Heathfield <rj*@see.sig.invalidwrites:
CBFalconer said:
>santosh wrote:
>>Chad wrote:
... snip ...
>>>
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>

No, don't.

Why not?
>That mechanism is faulty.

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 the
result:

XCHG(a, a);

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

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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jul 5 '08 #10
On 2008-07-05, Chad <cd*****@gmail.comwrote:
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?
In the absence of temp, you would have bad syntax:

void swap(int *x, int *y)
{
int = *x;
*x = *y;
*y =;
}

Better leave it there.
Chad
** Posted from http://www.teranews.com **
Jul 9 '08 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
1614
by: Gumby | last post by:
I want to make a two-d array of unsigned ints that I can change the size of as I need more memory. To do this I put in the h file a simple pointer to the memory and row/col variables to retain the...
34
440
by: wilson | last post by:
Hi All, I am a novice at C and just have learned pointer for a short period. Today one error had occured while executing my program below. And I cannot find the error out since it's checked "OK"...
5
1658
by: ciccio | last post by:
Hi, Again I am wondering about something. If you use the swap function of an std::vector, what does it do? Does it swap each element separately by means of copying to a tmp variable, or...
160
5469
by: raphfrk | last post by:
Is this valid? int a; void *b; b = (void *)a; // b points to a b += 5*sizeof(*a); // b points to a a = 100;
0
7044
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6908
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7045
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7087
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
6944
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5341
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
2995
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
2985
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
182
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.