Categories:
.NET (357)
C (330)
C++ (183)
CSS (84)
DBA (2)
General (7)
HTML (4)
Java (574)
JavaScript (106)
JSP (66)
Oracle (114)
Perl (46)
Perl (1)
PHP (1)
PL/SQL (1)
RSS (51)
Software QA (13)
SQL Server (1)
Windows (1)
XHTML (173)
Other Resources:
How can I write a generic macro to swap two values?
How can I write a generic macro to swap two values?
✍: Guest
There is no good answer to this question. If the values are integers, a well-known trick using exclusive-OR could perhaps be used, but it will not work for floating-point values or pointers, or if the two values are the same variable. If the macro is intended to be used on values of arbitrary type (the usual goal), any solution involving a temporary variable is problematical, because:
* It's hard to give the temporary a name that won't clash with anything. (Any name you pick might be the actual name of one of the variables being swapped. If you tried using ## to concatenate the names of the two actual arguments, to ensure that it won't match either one, it might still not be unique if the concatenated name is longer than 31 characters, and it wouldn't let you swap things like a[i] that aren't simple identifiers. You could probably get away with using a name like _tmp in the ``no man's land'' between the user and implementation namespaces;
* Either it can't be declared with the right type (because standard C does not provide a typeof operator), or (if it copies objects byte-by-byte, perhaps with memcpy, to a temporary array sized with sizeof) the macro can't be used on operands which are declared register.
The best all-around solution is probably to forget about using a macro, unless you're willing to pass in the type as a third argument. (Also, if you're trying to swap entire structures or arrays, you probably want to exchange pointers instead.) If you're worried about the use of an ugly temporary, and know that your machine provides an efficient exchange instruction, convince your compiler vendor to recognize the standard three-assignment swap idiom in the optimization phase.
If you're consumed by a passionate desire to solve this problem once and for all, please reconsider; there are better problems worthier of your energies.
2016-02-24, 1288👍, 0💬
Popular Posts:
What is effort variance? Effort Variance = (Actual effort – Estimated Effort) / Estimated Effort.
I have 5 questions please give me the answer ,explanation,suggestions if any?? what is PMP(project m...
Can we use the constructor, instead of init(), to initialize servlet? Yes , of course you can use th...
How To Divide Query Output into Groups? - MySQL FAQs - SQL SELECT Query Statements with GROUP BY You...
How is normally a project management plan document organized ? PMP document forms the bible of a pro...