Someone asked me how I could switch two memory locations in two lines of code.... Anyone know the answer to this trivia?
Someone asked me how I could switch two memory locations in two lines of code.... Anyone know the answer to this trivia?
Most likely with the evil XOR trick (although that works with one line). Don't bother.
Of course, it depends on how you define a line.
Code:inline void swapmem(void* m1, void* m2, size_t sz) { std::vector<char> buf(sz); memcpy(&buf[0], m1, sz); memmove(m1, m2, sz); memcpy(m2, &buf[0], sz); }
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law
Is that just
int a = 1
int b = 2
a ^= b
b ^= a
Not quite -- I think you need
orCode:a ^= b; b ^= a; a ^= b;
But note CornedBee's operative word: evil.Code:x ^= y ^= x ^= y;
From http://en.wikipedia.org/wiki/XOR_swap_algorithmMost modern compilers can optimize away the temporary variable in the naive swap, in which case the naive swap uses the same amount of memory and the same number of registers as the XOR swap and is at least as fast, and often faster.[2] As a general rule, you should never use the XOR swap unless you know for a fact that the naive swap will not suit your application (which is very rare in this day and age). The XOR swap is also much less readable, and can be completely opaque to anyone who isn't already familiar with the technique.
On modern (desktop) CPUs, the XOR technique is considerably slower than using a temporary variable to do swapping. One reason is that modern CPUs strive to execute commands in parallel; see Instruction pipeline. In the XOR technique, the inputs to each operation depend on the results of the previous operation, so they must be executed in strictly sequential order. If efficiency is of tremendous concern, it is advised to test the speeds of both the XOR technique and temporary variable swapping on the target architecture.
Also see http://everything2.com/title/xor%2520swap (for the one-line version)
[edit] Really, you had it right the first time. It's just trivia. Don't use it in your code. [/edit]
Last edited by dwks; 09-19-2008 at 05:14 PM.
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
ah thats great thanks!!!
1 line:
But I'd have to think a while about how to do it in 2 lines.Code:std::swap( p1, p2 );
"I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008
"the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010
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.
I think by "lines" they're talking about statements ending with a semicolon, rather than actual lines ending with CRLF. That's how I measure "lines of code".
"I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008
"the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010
It's
which can be condensed to:Code:if(a!=b){ a ^= b; b ^= a; a ^= b; }
Which by cpjust's stated definition is two lines.Code:if(a!=b){ b ^= a ^= b; a ^= b; }
Or you can do
But now it's bad style, unlike the other examples.Code:a!=b?(b ^= a ^= b,a ^= b):b;
You do need the check for if a == b, otherwise it will zero both values out.
And you need the sequence points to keep things well defined.
Last edited by King Mir; 09-20-2008 at 12:21 PM.
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.
It must be &a != &b, because the problem exists when the two arguments alias each other, not when they have the same value.
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law
Code:namespace { template <class T> void swap (T & a, T & b) { T temp = a; a = b, b = temp; } }
hmm... but then what we are doing is swapping the values between two memory locations. Actually switching/swapping two memory locations... can only be done by changing the hardware?
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Depends on what you mean by memory locations. You could perhaps change the address mapping in the kernel.
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law