Swapping logic

Discussion in 'C Programming' started by ZUBAIR TRALI, Sep 23, 2012.



    /*new logic for swapping*/
    void main()
    int x=20,y=30;
    printf("nos before swapping are %d and %d\n",x,y);
    printf("nos after swapping are %d and %d\n",x,y);
    ZUBAIR TRALI, Sep 23, 2012
    1. Advertisements

  2. This invokes undefined behavior, as you set the value of y, and also use
    the value of y (for a purpose other than getting the value to set y to).
    The language does not promise that the sub expression x*y will be
    executed before the sub expression y=x.

    Also, x*y needs to fit withing the range of an int, so it will only work
    for "small" numbers, and it fails if y == 0 (as 0/0 again invokes
    undefined behavior).
    Richard Damon, Sep 23, 2012
    1. Advertisements


    Eric Sosman Guest

    Wonderful![*] Splendid![*] You're a genius![*]

    Unfortunately, my stupid computer misbehaves when I use
    your brilliant technique with x=0. It even misbehaves when
    both x and y are 50000 to start with (which ought to be
    trivial to "swap"). Also, my compiler complains

    foo.c:6: warning: operation on 'y' may be undefined

    What am I[*] doing wrong?

    [*] <http://dictionary.reference.com/browse/irony>
    Eric Sosman, Sep 23, 2012

    Casey Carter Guest

    I think you're looking for:
    x ^= y;
    y ^= x;
    x ^= y;
    Casey Carter, Sep 23, 2012

    Kaz Kylheku Guest

    The problem here is that you're accessing y in an expression where y is
    modified. That is undefined behavior.

    More precisely, your accessing y for a purpose other than determining a new
    value to be stored into y.

    The following also accesses y in an expression where it is modified:

    y = y + 1;

    However, the access in "y + 1" is a necessary operand for computing the value
    stored in the assignment. This imposes an order, which makes it well defined.

    This one is not defined:

    y + (y = 3);

    The y on the left of the + is accessed, but that access is not necessary for
    the y = 3 assignment on the right. Because it is not necessary, it does not
    have to be ordered relative to the assignment, and so we do not know when that
    access occurs: before, during or after the completion of assignment to y?

    If you write such ambiguous expressions in your C programs, they will be
    nonportable between compilers and possibly unreliable even on one compiler.
    For instance, if you change some code-generation options such as optimization
    settings, the behavior might change.
    Kaz Kylheku, Sep 23, 2012

    Eric Sosman Guest

    Don't do this. See Question 20.15c on the comp.lang.c
    Frequently Asked Questions (FAQ) page, <http://www.c-faq.com/>.
    Eric Sosman, Sep 23, 2012

    Nobody Guest

    And especially don't do this (or equivalent):

    *p ^= *q;
    *q ^= *p;
    *p ^= *q;

    as it's equivalent to *p=0 if p==q.
    Nobody, Sep 24, 2012

    Noob Guest

    Noob, Sep 24, 2012
  9. Which is of course complete nonsense. It's the kind of thing that
    happens when somone's _opinion_ that is implictly based on one
    specific use case gets treated as undisputable and universal fact.
    The smallest system I've ever used C on had 53 bytes of RAM. You
    try making the argument that the correct solution is _always_ a
    temporary variable in that kind of environment.
    Andrew Smallshaw, Sep 26, 2012

    Eric Sosman Guest

    It's people like you who make me glad there are people
    like Steve Summit.
    Eric Sosman, Sep 26, 2012
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.