c++swaptemp

Swap with temp variable is not giving any output but swap() is giving an desired output output. c++


Swap with temp variable is not giving any output but swap() is giving an desired output. I am trying to swap arr[i] with arr[arr[i]+1].

This code is giving the desired output

#include <iostream>

using namespace std;

int main()
{
    int n;
    cin>>n;
    int arr[n],i,temp;
    for(i=0;i<n;i++) {
        cin>>arr[i];
    }
    for(i=0;i<n;i++) {
        swap(arr[i],arr[arr[i]+1]);
    }
    for(i=0;i<n;i++) {
        cout<<arr[i]<<" ";
    }
}

But with temp variable there's no output. Code is given below

#include <iostream>

using namespace std;

int main()
{
    int n;
    cin>>n;
    int arr[n],i,temp;
    for(i=0;i<n;i++) {
        cin>>arr[i];
    }
    for(i=0;i<n;i++) {
        temp=arr[i];
        arr[i]=arr[arr[i]+1];
        arr[arr[i]+1]=temp;
    }
    for(i=0;i<n;i++) {
        cout<<arr[i]<<" ";
    }
}

I don't know what's the issue. I hope will get an response soon. Thank You!


Solution

  • The different results is because std::swap does not use a temporary variable this way, it works slightly differently. Hence the different results.

    To understand the different results consider the most simple case when the array has two values:

          +---+---+
    arr:  | 0 | 1 |
          +---+---+
    

    arr[0] is 0 and arr[1] is 1. This can't be any more simpler, but let's just consider the only the initial swap, when i is 0:

    std::swap(arr[i],arr[arr[i]+1]);
    

    Let's evaluate both parameters when i is 0.

    The first parameter to std::swap is, therefore arr[0].

    The second parameter to std::swap is arr[arr[0]+1]. arr[0] is 0, so this becomes arr[0+1], or arr[1].

    Conclusion: the first parameter to std::swap is arr[0], the second parameter is arr[1].

    std::swap then proceeds and swaps arr[0] with arr[1]. The exact way it does this is not important, it's only important to understand that the two values get swapped as expected.

    Now, let's walk through what happens, with the same arr, when a temporary variable is used. Let's do this, one step at a time:

            temp=arr[i];
    

    Since i is 0, this becomes temp=arr[0], so temp becomes 0.

            arr[i]=arr[arr[i]+1];
    

    i is still 0, so this becomes arr[0]=arr[arr[0]+1]. arr[0] is still 0, so this becomes arr[0]=arr[0+1], or arr[0]=arr[1].

    The current contents of the array are now, at this point:

          +---+---+
    arr:  | 1 | 1 |
          +---+---+
    

    And now, the final statement gets executed:

            arr[arr[i]+1]=temp;
    

    i is still 0. So, this becomes arr[arr[0]+1]=temp.

    What it arr[0]? It's value is 1. So this becomes arr[1+1]=temp, or arr[2]=temp.

    There is no arr[2]. Instead of swapping anything, the end result of the shown code is undefined behavior, memory corruption, and demons flying out of your nose.

    Now, it's also possible to have undefined behavior using the std::swap version as well, depending on what's actually in the arr. This is only an explanation of why using a temporary variable is not the same as using std::swap, with all other things being equal.