Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Operation undefined

Reply
Thread Tools

Operation undefined

 
 
Christian Christmann
Guest
Posts: n/a
 
      07-31-2006
Hi,

for this code

int main( void )
{
unsigned short a= 10;
a = (a <<= 1) ^ 4129; //warning concerns this line
return 0;
}

gcc issues the warning:

warning: operation on `a' may be undefined


The problem is "(a <<=1 )" before the XOR operator, but
why might this expression lead to undefined behavior?

Regards,
Chris
 
Reply With Quote
 
 
 
 
Robert Gamble
Guest
Posts: n/a
 
      07-31-2006
Christian Christmann wrote:
> Hi,
>
> for this code
>
> int main( void )
> {
> unsigned short a= 10;
> a = (a <<= 1) ^ 4129; //warning concerns this line
> return 0;
> }
>
> gcc issues the warning:
>
> warning: operation on `a' may be undefined
>
>
> The problem is "(a <<=1 )" before the XOR operator, but
> why might this expression lead to undefined behavior?


The operation is undefined because a is modified twice without an
intervening sequence point. Why not just use "a = (a << 1) ^ 4129" ?

Robert Gamble

 
Reply With Quote
 
 
 
 
Martin Ambuhl
Guest
Posts: n/a
 
      07-31-2006
Christian Christmann wrote:
> Hi,
>
> for this code
>
> int main( void )
> {
> unsigned short a= 10;
> a = (a <<= 1) ^ 4129; //warning concerns this line
> return 0;
> }


#include <stdio.h>

int main(void)
{
unsigned short a = 10;
#if 0
/* the following is a variation on 'i = i++;' covered by the FAQ */
a = (a <<= 1) ^ 4129;
#endif
/* but this should work */
a = (a << 1) ^ 4129;
printf("a was 10, and is now %u\n", a);
/* and this should work */
a = 10;
a = (a << 1);
a ^= 4129;
printf("a was 10, and is now %u\n", a);
return 0;
}

 
Reply With Quote
 
Andrew Poelstra
Guest
Posts: n/a
 
      07-31-2006
On 2006-07-31, Christian Christmann <(E-Mail Removed)> wrote:
> Hi,
>
> for this code
>
> int main( void )
> {
> unsigned short a= 10;
> a = (a <<= 1) ^ 4129; //warning concerns this line
> return 0;
> }
>
> gcc issues the warning:
>
> warning: operation on `a' may be undefined
>
>
> The problem is "(a <<=1 )" before the XOR operator, but
> why might this expression lead to undefined behavior?
>


I'm pretty sure this is a (potential?) sequence-point error, and
that is what gcc is warning you about. It appears to me that using
(a <<= 1) is logically equivilant to (a << 1) in this context:

int main (void)
{
unsigned short a = 10;
a = (a << 1) ^ 4129;
return 0;
}

--
Andrew Poelstra <website down>
To reach my email, use <email also down>
New server ETA: 42
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Why does left shift operation invoke Undefined Behaviour when theleft side operand has negative value? Prasoon Saurav C++ 1 09-24-2010 06:07 PM
Boolean operation and arithmetic operation Buzz Lightyear C++ 10 08-12-2009 01:27 PM
I/O operation, file operation behaviou raan C++ 2 08-16-2007 07:13 PM
Does bit operation always work more efficiently than math operation? david ullua C Programming 13 03-01-2006 11:02 PM
operation on `x' may be undefined? jimjim C Programming 30 08-11-2005 05:59 PM



Advertisments