40
tarun93
7y

makes me laugh every time 😂

Comments
  • 4
    std::swap( a, b );
  • 3
    C not C++ 😏
  • 2
    @tarun93 why not both?

    And java, c#, js, any c-style language?
  • 0
    @tarun93
    void swap(int* a, int* b) {
    int tmp = *a;
    *a = *b;
    *b = tmp;
    }

    int a = 3, b = 5;
    swap(&a, &b);
  • 4
    xor a,b
    xor b,a
    xor a,b
  • 5
    I have seen this multiple times on devRant now and every time it makes me. Cringe, because of the implementation of python it actually uses an extra nameless variable. It creates an tuple En destroys that tuple again. So yeah you don't have to define an extra variable, but it actually uses more memory and uses more operations. So it doesn't have to be beter than in any other language.
  • 0
    @micfort so? It's about readability and ease of writing the code.
    I don't care about a few instructions or a few bytes of memory.

    It matters when I'm working on embedded stuff.. but then I'm rather forced to use c (wich I also like actually).
  • 0
    The right way in C is to use a temporary variable.
  • 1
    @PRein no one's denying python's readability. But when a problem like this asks you to swap two variables without using a 3rd, the lesson you're learning is to respect memory. That statement in python does not. The C code does.

    Obviously in most real life situations this does not matter. But it does in some.
  • 0
    @burmesepornstar specifically for swap, in none.
  • 0
    @PRein sure, I can agree on that. But in any language that support reference parameters or in out parameters you can simple define a function that swaps 2 values. That is not difficult, and even c++ has a function in the standard library named swap. So the whole argument is kinda ridiculous.

    Meanwhile, how often do you actually swap variables? Seriously??
    I do it in for example implementing quick sort, and it ends very fast there.
  • 2
    @qookie my favourite so far.

    I’d rather have the register names than A and B but I’m sure your assembler is handling that.
Add Comment