Problem with if-condition with assignment on either side of logical comparison

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Andreas Eibach

    Problem with if-condition with assignment on either side of logical comparison

    Hi,

    supposed I have this alone:
    (mentally add typedefs :))

    uchar byte1, byte2;
    ulong a1, a2, b1, b2;
    uchar getOffset (ulong, ulong);
    uchar getOtherOffset (ulong, ulong);

    byte1 = getOffset (a1, a2);
    byte2 = getOtherOffset (b1, b2);

    _Instead_of the above, to shorten it all, it did NOT work here to say

    if ( (byte1 = getOffset (a1,a2)) != (byte2 = getOtherOffset( b1,b2)))
    {
    ....
    }

    It was supposed to have both byte1 and byte2 calculated separately, then
    compared.
    No errors whatsoever when compiling; however, this always gave a wrong
    (resp. random) result for byte1 when debugging, why?

    However, calculating the left result isolatedly, this *does* work:

    byte1 = getOffset (a1,a2);
    if ( (byte1 != (byte2 = getOtherOffset( b1,b2)))
    {
    ....
    }

    Is this a known "issue"? (note the quotes)

    -Andreas

  • Ben Bacarisse

    #2
    Re: Problem with if-condition with assignment on either side of logical comparison

    "Andreas Eibach" <aeibach@mail.c omwrites:
    <snip>
    uchar byte1, byte2;
    ulong a1, a2, b1, b2;
    uchar getOffset (ulong, ulong);
    uchar getOtherOffset (ulong, ulong);
    >
    byte1 = getOffset (a1, a2);
    byte2 = getOtherOffset (b1, b2);
    >
    _Instead_of the above, to shorten it all, it did NOT work here to say
    >
    if ( (byte1 = getOffset (a1,a2)) != (byte2 = getOtherOffset( b1,b2)))
    {
    ...
    }
    It would be best to post a complete compilable example. Just making
    such an example often reveals the problem but even if it does not it
    gives us something try out.

    Since you are suggesting a compiler fault, the name and version of it
    would help.

    <snip>
    Is this a known "issue"? (note the quotes)
    Not to me, sorry. BTW, I noted the quotes but I have no idea what
    they signify. What is an "issue" as opposed to an issue?

    --
    Ben.

    Comment

    • vippstar@gmail.com

      #3
      Re: Problem with if-condition with assignment on either side oflogical comparison

      On Nov 16, 7:16 pm, "Andreas Eibach" <aeib...@mail.c omwrote:
      Hi,
      >
      supposed I have this alone:
      (mentally add typedefs :))
      >
      uchar byte1, byte2;
      ulong a1, a2, b1, b2;
      uchar getOffset (ulong, ulong);
      uchar getOtherOffset (ulong, ulong);
      When you write a usenet post, expect it to be treated the same way you
      treated it; A serious post will get serious replies (minus trolls), a
      half-arsed post will receive half-arsed replies.

      Comment

      • Keith Thompson

        #4
        Re: Problem with if-condition with assignment on either side of logical comparison

        vippstar@gmail. com writes:
        On Nov 16, 7:16 pm, "Andreas Eibach" <aeib...@mail.c omwrote:
        >supposed I have this alone:
        >(mentally add typedefs :))
        >>
        > uchar byte1, byte2;
        > ulong a1, a2, b1, b2;
        > uchar getOffset (ulong, ulong);
        > uchar getOtherOffset (ulong, ulong);
        >
        When you write a usenet post, expect it to be treated the same way you
        treated it; A serious post will get serious replies (minus trolls), a
        half-arsed post will receive half-arsed replies.
        Composing a good question, like programming in C, is a learned skill.
        Advising the OP how he can ask better questions (as several others in
        this thread have done) is likely to be more constructive than
        insulting him.

        One good resource:


        --
        Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
        Nokia
        "We must do something. This is something. Therefore, we must do this."
        -- Antony Jay and Jonathan Lynn, "Yes Minister"

        Comment

        • CBFalconer

          #5
          Re: Problem with if-condition with assignment on either side of logicalcomparis on

          Andreas Eibach wrote:
          >
          uchar byte1, byte2;
          ulong a1, a2, b1, b2;
          uchar getOffset(ulong , ulong);
          uchar getOtherOffset( ulong, ulong);
          >
          byte1 = getOffset(a1, a2);
          byte2 = getOtherOffset( b1, b2);
          >
          _Instead_of the above, to shorten it all, it did NOT work here
          >
          if ((byte1 = getOffset(a1,a2 )) != (byte2 = getOtherOffset( b1,b2)))
          ....
          >
          It was supposed to have both byte1 and byte2 calculated separately,
          then compared. No errors whatsoever when compiling; however, this
          always gave a wrong (resp. random) result for byte1 when debugging,
          why?
          I have no idea what uchar and ulong represent. If they are
          supposed to mean "unsigned char" and "unsigned long", just write
          the real meanings. Assuming so, how can the "getOffset" call ever
          work, since you are passing char addresses to a routine that wants
          unsigned long addresses?

          --
          [mail]: Chuck F (cbfalconer at maineline dot net)
          [page]: <http://cbfalconer.home .att.net>
          Try the download section.

          Comment

          • Andrey Tarasevich

            #6
            Re: Problem with if-condition with assignment on either side of logicalcomparis on

            Andreas Eibach wrote:
            Hi,
            >
            supposed I have this alone:
            (mentally add typedefs :))
            >
            uchar byte1, byte2;
            ulong a1, a2, b1, b2;
            uchar getOffset (ulong, ulong);
            uchar getOtherOffset (ulong, ulong);
            >
            byte1 = getOffset (a1, a2);
            byte2 = getOtherOffset (b1, b2);
            >
            _Instead_of the above, to shorten it all, it did NOT work here to say
            >
            if ( (byte1 = getOffset (a1,a2)) != (byte2 = getOtherOffset( b1,b2)))
            {
            ...
            }
            It was supposed to have both byte1 and byte2 calculated separately, then
            compared.
            No errors whatsoever when compiling; however, this always gave a wrong
            (resp. random) result for byte1 when debugging, why?
            This should give you the result identical to the original code (i.e.
            'byte1' and 'byte2' calculated in advance, separately, and then
            compared), unless 'getOffset' has side-effects that affect the result of
            'getOtherOffset ' or vice versa. The language specification does not
            specify whether 'getOffset (a1,a2)' or 'getOtherOffset (b1,b2)' should be
            called first in the "shortened" version.
            However, calculating the left result isolatedly, this *does* work:
            >
            byte1 = getOffset (a1,a2);
            if ( (byte1 != (byte2 = getOtherOffset( b1,b2)))
            {
            ...
            }
            .... which is a strong indication of the existence of the aforementioned
            side-effects.

            --
            Best regards,
            Andrey Tarasevich

            Comment

            • Antoninus Twink

              #7
              Re: Problem with if-condition with assignment on either side oflogical comparison

              On 16 Nov 2008 at 22:17, CBFalconer wrote:
              I have no idea what uchar and ulong represent.
              Hanlon's Razor makes you the biggest idiot on the planet.

              Comment

              • blargg

                #8
                Re: Problem with if-condition with assignment on either side of logical comparison

                CBFalconer wrote:
                Andreas Eibach wrote:

                uchar byte1, byte2;
                ulong a1, a2, b1, b2;
                uchar getOffset(ulong , ulong);
                uchar getOtherOffset( ulong, ulong);

                byte1 = getOffset(a1, a2);
                byte2 = getOtherOffset( b1, b2);
                [...]
                I have no idea what uchar and ulong represent. If they are
                supposed to mean "unsigned char" and "unsigned long", just write
                the real meanings.
                Obviously it would have taken too much time to write "unsigned char"
                instead of uchar. His time is very valuable (unlike ours). </sarcasm>
                Assuming so, how can the "getOffset" call ever
                work, since you are passing char addresses to a routine that wants
                unsigned long addresses?
                Huh? He's passing type ulong to functions accepting ulong, and assigning
                the uchar result to uchar objects. No addresses involved.

                Comment

                • CBFalconer

                  #9
                  Re: Problem with if-condition with assignment on either side of logicalcomparis on

                  blargg wrote:
                  CBFalconer wrote:
                  >Andreas Eibach wrote:
                  >>
                  >> uchar byte1, byte2;
                  >> ulong a1, a2, b1, b2;
                  >> uchar getOffset(ulong , ulong);
                  >> uchar getOtherOffset( ulong, ulong);
                  >>>
                  >> byte1 = getOffset(a1, a2);
                  >> byte2 = getOtherOffset( b1, b2);
                  [...]
                  >I have no idea what uchar and ulong represent. If they are
                  >supposed to mean "unsigned char" and "unsigned long", just write
                  >the real meanings.
                  >
                  Obviously it would have taken too much time to write "unsigned char"
                  instead of uchar. His time is very valuable (unlike ours). </sarcasm>
                  >
                  >Assuming so, how can the "getOffset" call ever
                  >work, since you are passing char addresses to a routine that wants
                  >unsigned long addresses?
                  >
                  Huh? He's passing type ulong to functions accepting ulong, and
                  assigning the uchar result to uchar objects. No addresses involved.
                  True. I saw the 'getOffset' and assumed he was passing addresses.
                  The code just doesn't make any sense.

                  --
                  [mail]: Chuck F (cbfalconer at maineline dot net)
                  [page]: <http://cbfalconer.home .att.net>
                  Try the download section.

                  Comment

                  Working...