Someone asked me how I could switch two memory locations in two lines of code.... Anyone know the answer to this trivia?
Printable View
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); }
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_algorithmQuote:
Most 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]
ah thats great thanks!!!
1 line:
But I'd have to think a while about how to do it in 2 lines. :pCode:std::swap( p1, p2 );
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".
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.
It must be &a != &b, because the problem exists when the two arguments alias each other, not when they have the same value.
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? :)
Depends on what you mean by memory locations. You could perhaps change the address mapping in the kernel.