Xor trick?
Adam D. Ruppe
destructionator at gmail.com
Sat Jan 25 16:08:37 PST 2014
On Sunday, 26 January 2014 at 00:04:08 UTC, bearophile wrote:
> Do you know how to perform the xor trick
> (http://en.wikipedia.org/wiki/XOR_swap_algorithm ) on two
> pointers in D?
You don't; it is undefined behavior and could lead to crashes.
Suppose another thread triggers a GC run right after the first
xor. Then there may be no valid pointers to *x and the GC frees
it, so then after the swap, *y points to something entirely
different.
> test.d(2): Error: 'x' is not of integral type, it is a int*
You could cast it to size_t, then the compiler will let you do
the operation, but casting pointers to and from integers means
you take matters of correctness into your own hands.
More information about the Digitalmars-d-learn
mailing list