for swap function we have two choice: ref-style:
void swap (int &a, int &b)
{
int temp;
temp = b;
b = a;
a = temp;
}
and pointer-style:
void swap (int *a, int *b)
{
int temp;
temp = *b;
*b = *a;
*a = temp;
}
ref-style absolutely legal but pointer-style have some issue, when we try to use this function variables pass by value not by reference-even they are pointers- in fact we try to use memory of a local variable outside its function, and may by in some day in some machine we have undefined behavior ,also the code works for examples: In this code:
main()
{
//
{
int i=12;
int *j=&i;
}
//in this area, there is not variables i and j, but phisically threre is
// unsafe-relationship between this address: &i and what point to (12) ,
//any more logic according to this assumtion may be work
//but not safe -in the scene of undefined behavior-