Dan said:
Since you seem to be unable to understand plain English:
#include <stdio.h>
void swap(int *p, int *q) { ... }
int main()
{
int i = 10;
swap(&i, &i);
printf("%d\n", i);
return 0;
}
Try this code for different implementations of the swap function, using
a temp var and using in-place swapping. Compare the results.
This example is trivial, but the situation can realistically arise in more
complex algorithms.
No, I do not understand your version of plain English.
Just because you have two pointers that _point_ to the same address, this
doesn't mean that they (the variables here which are still the **pointers**)
share the same memory location.
Your definition of swap doesn't take to variables, it takes two addresses.
Even in your example in main, you are passing in the same pointer.
Show me a case of two separate variables that share the same memory location,
without using placement new. You can even use your version of 'plain English'.
Here is the point in my version of 'plain English': you can't have two
variables that share the same memory address (excluding placement new). So, if
the precondition for swap is that it operates on two variables, then it will
always work provided the precondition is met.