Connecting Tech Pros Worldwide Help | Site Map

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

  #1  
Old November 16th, 2008, 05:15 PM
Andreas Eibach
Guest
 
Posts: n/a
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

  #2  
Old November 16th, 2008, 06:35 PM
Ben Bacarisse
Guest
 
Posts: n/a

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


"Andreas Eibach" <aeibach@mail.comwrites:
<snip>
Quote:
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>
Quote:
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.
  #3  
Old November 16th, 2008, 06:55 PM
vippstar@gmail.com
Guest
 
Posts: n/a

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


On Nov 16, 7:16 pm, "Andreas Eibach" <aeib...@mail.comwrote:
Quote:
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.

  #4  
Old November 16th, 2008, 09:15 PM
Keith Thompson
Guest
 
Posts: n/a

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


vippstar@gmail.com writes:
Quote:
On Nov 16, 7:16 pm, "Andreas Eibach" <aeib...@mail.comwrote:
Quote:
>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:
http://www.catb.org/~esr/faqs/smart-questions.html

--
Keith Thompson (The_Other_Keith) 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"
  #5  
Old November 16th, 2008, 10:25 PM
CBFalconer
Guest
 
Posts: n/a

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


Andreas Eibach wrote:
Quote:
>
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)))
....
Quote:
>
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.
  #6  
Old November 16th, 2008, 10:25 PM
Andrey Tarasevich
Guest
 
Posts: n/a

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


Andreas Eibach wrote:
Quote:
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.
Quote:
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
  #7  
Old November 16th, 2008, 10:35 PM
Antoninus Twink
Guest
 
Posts: n/a

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


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

  #8  
Old November 16th, 2008, 11:35 PM
blargg
Guest
 
Posts: n/a

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


CBFalconer wrote:
Quote:
Andreas Eibach wrote:
Quote:

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

byte1 = getOffset(a1, a2);
byte2 = getOtherOffset(b1, b2);
[...]
Quote:
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>
Quote:
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.
  #9  
Old November 16th, 2008, 11:55 PM
CBFalconer
Guest
 
Posts: n/a

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


blargg wrote:
Quote:
CBFalconer wrote:
Quote:
>Andreas Eibach wrote:
>>
Quote:
>> uchar byte1, byte2;
>> ulong a1, a2, b1, b2;
>> uchar getOffset(ulong, ulong);
>> uchar getOtherOffset(ulong, ulong);
>>>
>> byte1 = getOffset(a1, a2);
>> byte2 = getOtherOffset(b1, b2);
[...]
Quote:
>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>
>
Quote:
>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.
Closed Thread