Is there a built in swap function in C?

前端 未结 11 2097
北恋
北恋 2020-12-15 16:01

Is there any built in swap function in C which works without using a third variable?

相关标签:
11条回答
  • 2020-12-15 16:20

    Since you may copy any object representation into an unsigned char array in C, the following macro allows you to swap any two objects:

    #define SWAP(X,Y) \
        do { \
            unsigned char _buf[sizeof(*(X))]; \
            memmove(_buf, (X), sizeof(_buf)); \
            memmove((X),  (Y), sizeof(_buf)); \
            memmove((Y), _buf, sizeof(_buf)); \
        } while (0)
    

    GCC will even generate optimal code for this in some cases. You might not keep your job though...

    0 讨论(0)
  • 2020-12-15 16:35

    there is std::swap since in general it depends on your processor, whether it supports swaping. there is an instruction called "compare and swap", but it only works on types that fit into a register and is guaranteed to be atomic. There is a built-in implementation of compare and swap (CAS) from gcc it's used for synchronization of thread and mutex implementations and probably way out of scope for your purpose so it's best to stick with just using a temporary variable or if you are really stuck to C you can always use a macro like this:

    #define swap(a,b) a=a^b; \
                      b=a^b; \
                      a=b^a;
    
    0 讨论(0)
  • 2020-12-15 16:38

    Why do you not want to use a third variable? It's the fastest way on the vast majority of architectures.

    The XOR swap algorithm works without a third variable, but it is problematic in two ways:

    1. The variables must be distinct i.e. swap(&a, &a) will not work.
    2. It is slower in general.

    It may sometimes be preferable to use the XOR swap if using a third variable would cause the stack to spill, but generally you aren't in such a position to make that call.

    To answer your question directly, no there is no swap function in standard C, although it would be trivial to write.

    0 讨论(0)
  • There is is a C++ library function. It swaps the values of two integer variables. For example, swap(x, y); will swap the values of variables x and y. Similarly, swap(mat[i][j], mat[j][i]); will swap two values in matrix mat, namely the value in row i column j and the value in row j column i.

    0 讨论(0)
  • 2020-12-15 16:42

    There is no standard function in C to swap two variables.

    A macro can be written this way:

    #define SWAP(T, a, b) do { T tmp = a; a = b; b = tmp; } while (0)
    

    and the macro can be called this way:

    int a = 42;
    int b = 2718;
    
    SWAP(int, a, b);
    

    Some solutions for a writing a SWAP macro should be avoided:

    #define SWAP(a, b) do { a = b + a; b = a - b; a = a - b; } while (0)
    

    when operands are of signed types an overflow can occur and signed overflow are undefined behavior.

    Also a solution trying to optimize the XOR solution like this should be avoid:

    #define SWAP(a, b) (a ^= b ^= a ^=b)
    

    a is modified twice between the previous and the next sequence point, so it violates the sequence points rules and is undefined behavior.

    0 讨论(0)
提交回复
热议问题