※スタイルシート未対応ブラウザではレイアウトを正確に再現できません。
  < The returning  contents  advance >   
                   < [modosusu] > Color magazine monochrome light and shade   font predetermined, Gothic Ming-style type longhand   size Konaka large   standard  


  Column: Finished type of SWAP macro   

SWAP is a macro and a function that exchanges the values of two variables.
Therefore, this function : oppositely though it is very easy.
The person who thinks whether achieve it only by the macro also is getting into the news considerably a lot.

First of all, start explaining from mounting by the most basic function.
Naturally, use the argument of the pointer type because it is necessary to change the value of two variable.
Notes here : though it only has to exchange two variables on the side of the function now.

 void swap(int *a,int *b)  
{
    *a = *b;  
    *b = *a;  
    return;
}
It is useless in the method.
Because contents of *a become it as well as contents of *b when *b is substituted for *a
It is because of meaningless even if *a is substituted for *b afterwards.
Therefore, it is necessary to declare another one local variable, and to save *a in that.
The following program is an example of mounting the most standard swap function.
 
#include <stdio.h> 

void swap(int *a,int *b);  

int main(void)  
{
    int a = 10,b = 100;  
    printf("a = %3d : b = %3d\n",a,b);     &
nbsp;swap(&a,&b);  
    printf("a = %3d : b = %3d\n",a,b);  
    return 0;  
}

void swap(int *a,int *b)  
{
    int temp;  
    temp = *a;  
    *a = *b;  
    *b = temp;  
    return;
}
The execution result of this program is as follows.

a = 10 : b = 100
a = 100 : b = 10
Though this of each type only has to be prepared now
Because the function such as in-line, overloads, and templates cannot be used by C language
It is necessary to make it naming a separate name of each type one by one.
Then, it is not easy to use, and because the overhead of the call is anxious
There seem to manage considerably to be people that it wants to achieve it only by the macro.

It is mounting by the following addition and subtraction to be thought first of all.

 
#define SWAP(a,b) (a += b,b = a - b,a -= b)
The principle is easy. b is added first, and the way that preserves the value of b.
Though the overflow by addition is anxious on the face of things
Because an additional amount loops even when overflowing, it operates without trouble.

It began to devise the method using another one and the exclusive-OR.

 
#define SWAP(a,b) (a ^= b,b = a ^ b,a ^= b)
There is fundamentally no great difference though it omits when retreating not fixing the explanation of the exclusive-OR.
It seems that here might be computational more high-speed.
However, because it is not possible to use it for the real number value, generality will be lacked.

Though these two macros operate without trouble in case of almost
Actually, there is a case that doesn't operate well, and it becomes the maximum problem.
It happens when the same variable is specified.
Will actually try from the theory because it is evidence.

 
#include <stdio.h> 

#define SWAP(a,b) (a += b,b = a - b,a -= b)  

int main(void)  
{
    int a = 98;  
    printf("a = %3d\n",a);  
    SWAP(a,a);  
    printf("a = %3d\n",a);  
    return 0;
}
The execution result of this program is as follows.

a = 98
a = 0
If you exchanged the values of a that is the same variable though the result is [nahazu] naturally as it is
It becomes 0 in this method.
In this method, if it subtracts it because a and b of the macro are always the same values to 0 natural.
By the way, the method by the exclusive-OR still becomes 0.

Though it might not be thought that there is what of the exchange of the same variables
Actually, with the sort program with array[i] When you exchanged array[j] It was i == j.
Then, be and get it. Moreover, most of the usage of SWAP is a sort program.

Generally though it doesn't depress any further because this problem is helpless
The author wants to solve this even forcibly, and is using the method by & operator.
In & operator, there is a character that the following expression is not executed if the expression executed immediately before is an imitation.
The following macro is SWAP macro using the character of & operator.

 #define SWAP(a,b) ((a != b) && (a += b,b = a - b,a -= b))
Execute the exchange in this macro only when the value of a and b is different.
In a word, it remains as it is without being exchanged when a and b are the same variables.
However, it might be able to be said that it is easier to make it to the function than to do these kind of things.

Or, clause 3 operator can be used so as not to depend on the character of the operator.

 #define SWAP(a,b) ((a != b)? (a += b,b = a - b,a -= b) : 0 )
Space cork of 0 in the latter half to dummy value for fixing up grammar.
However, it is likely to become warning as a meaningless expression according to the compiler.
Though it is possible to exchange even by the variable of what type (Even the real number :) if they are these macros
Only, variables of the pointer type not to be able to add and subtract alone cannot be exchanged.

Though the SWAP macro can be made if the pointer variable is not used
Only the exchange of pointer variables has only mounting by the function shown first by all means.
And, pointers might be exchanged with the sort program unfortunately.

In addition, there is a possibility that drop out happens when the real number values are exchanged.
For instance, when the value of variable a is terribly huge, and the value of b is terribly minimum
The result is rounded even if a+b is calculated, and there is a possibility of becoming it as well as a.
Therefore, when accuracy is requested, it is not possible to use it.

After all, to exchange the values surely, the variable is temporarily needed by all means.
It is necessary to declare a variable in the macro for that.

If the type is specified, it is possible to declare a variable because the macro is only a mere replacement.
Moreover, the collision of the variable identifier can be avoided if it encloses and it blocks it.
Concretely, it becomes the following.

 #define SWAP(type,a,b) { type temp = a; a = b; b = temp; } 
Though convenience is somewhat inferior to the extent that this macro should specify the type for the first argument
Any variables can be exchanged as long as the type is specified.
Of course, the exchange of the real number values and the exchange of pointer values are also possible.

The following program is an example of exchanging variables by using this macro.

 
#include <stdio.h> 

#define SWAP(type,a,b) { type temp = a; a = b; b = temp; } 

int main(void)  
{
	int a = 10,b = 100;  
	printf("a = %3d : b = %3d\n",a,b);  
	SWAP(int,a,b) printf("a = %3d : 
	b = %3d\n",a,b);  
	return 0;
}
The execution result of this program is as follows.

a = 10 : b = 100
a = 100 : b = 10
Of course, when the same variable is specified, it operates without trouble.
Because it ..[de].. incloses it in this method for the variable declaration
;It is concluded as [wotsukenakutemo] sentence.
At the end;will ..[hatsukenai]..
At the end;The person of yucky : [wotsukerarenaino].
 #define swap(type,a,b) do{type _c;_c=a;a=b;b=_c;}while(0)
Because it is not concluded only by the macro as a sentence if it makes it
At the end;[Wotsukete] comes to operate.


< - Advanced -> to the returning following. the first  that returns