3

I have tested the code below

#include <iostream>
using namespace std;
void swap(int *x, int *y);

int main() {
    int a, b;
    a = 5;  
    b = 10; 
    cout << "a = " << a << endl;
    cout << "b = " << b << endl;
    cout << "&a = " << &a << endl;
    cout << "&b = " << &b << endl;

    swap(a, b); 
    cout << endl;
    cout << "a = " << a << endl;
    cout << "b = " << b << endl;

    return 0;
}

void swap(int *x, int *y){
    cout << "Hello" << endl;
    cout << "x = " << x << endl;
    cout << "y = " << y << endl;
    int temp;
    temp = *x; 
    *x = *y; 
    *y =temp;
}

I know it should pass &a and &b to swap and that works as expected. However, above codes seem work as well. Results are:

a = 5
b = 10
&a = 0x7ffeea1648d8
&b = 0x7ffeea1648d4

a = 10
b = 5

Questions are:
If the swap function is implemented why there is no info printed out?
If swap function is not implemented why the values swap?

  • 8
    This is because of your `using namespace std`. You are using `std::swap`. – Matthieu Brucher Dec 13 '18 at 10:17
  • Deleted using namespace std, same result. Thanks anyway :) – Mingshan Jia Dec 13 '18 at 10:21
  • 1
    cannot be. maybe you need to make a clean build. See [here](https://ideone.com/tISbSv)). Please check again and either roll back the edit or say what compiler you are using – 463035818_is_not_an_ai Dec 13 '18 at 10:28
  • 1
    @MingshanJia If you really deleted `using namespace std;` then your code would not compile (because `cout` needs to be replaced by `std::cout`). – john Dec 13 '18 at 10:29
  • Try a different approach, rename your swap function to (myswap for instance), so it doesn't clash with the standard version. – john Dec 13 '18 at 10:30
  • Note -- do NOT change the question you posted. You are free to edit it and ADD new information below, but DON'T delete information from the original. Why? Then any answers and comments addressing your original code will no longer make sense. You are a new user, so no dings this time, but don't do it going forward. – David C. Rankin Dec 13 '18 at 10:31
  • btw no problem in this case, but in general you should not edit your code in the question after you got comments and/or answers, unless the change is definitely completely unrelated to the question (which it is not in this case, even if the code you have now would compile) – 463035818_is_not_an_ai Dec 13 '18 at 10:32
  • Sorry about that, first time asking question. Didn't know how to edit without editing yet. – Mingshan Jia Dec 13 '18 at 10:40

1 Answers1

9

Your code is not doing what you think it does. To understand, try to compile this:

void foo(int* x) {}

int main() { 
    int x = 42;
    foo(x);
}

You will get an error because you cannot pass an int to a function that expects an int*. Your code still works because of

using namespace std;

and because there is a std::swap(int&,int&) that matches your call (hence you wont see an error for the swap you wrote) and your

 swap(a, b); 

actually calls the standard function.

Take it as a lesson and try to avoid using namespace std;. This is just one problem that you can encounter when you use it.

Moreover I would suggest you to use references instead of pointers. Pointers can be null, but if one of the parameters to your function is null, it does not make sense to swap anything. With references the code looks much nicer (well subjective, but maybe you can agree):

void swap(int& x, int& y){
    int temp;
    temp = x; 
    x = y; 
    y =temp;
}

However, you shouldnt be writing a swap in the first place, but use std::swap instead.

463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185