User account creation filtered due to spam.

Bug 40366 - Array + XOR swap fails
Summary: Array + XOR swap fails
Status: RESOLVED DUPLICATE of bug 11751
Alias: None
Product: gcc
Classification: Unclassified
Component: c (show other bugs)
Version: 4.2.4
: P3 major
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2009-06-07 07:15 UTC by Ashutosh Sharma
Modified: 2014-02-16 13:14 UTC (History)
61 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Ashutosh Sharma 2009-06-07 07:15:33 UTC
Compiler Version:
gcc (GCC) 4.2.4 (Ubuntu 4.2.4-1ubuntu3)
Copyright (C) 2007 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.


Sample code:

#include <stdio.h>

int main() {
        int a[2];
        a[0]=10;
        a[1]=20;
        printf("a[0] = %d a[1] = %d\n", a[0], a[1]);
        a[0]^=a[1]^=a[0]^=a[1];
        printf("a[0] = %d a[1] = %d\n", a[0], a[1]);
        return 0;
}

gcc test.c -o test -O2

a[0] = 10 a[1] = 20
a[0] = 20 a[1] = 10

gcc test.c -o test -O0

a[0] = 10 a[1] = 20
a[0] = 0 a[1] = 10

Bug: When compiled with -O0 swap fails.
Swap only fails in case of array. Following code works fine with -O0:

#include <stdio.h>

int main() {
        int a, b;
        a=10;
        b=20;
        printf("a = %d b = %d\n", a, b);
        a^=b^=a^=b;
        printf("a = %d b = %d\n", a, b);
        return 0;
}
Comment 1 pinskia@gmail.com 2009-06-07 07:23:39 UTC
Subject: Re:   New: Array + XOR swap fails

Both of these are undefined. Look up about sequence point rules.

Sent from my iPhone

On Jun 7, 2009, at 12:15 AM, "ashutosh dot sharma dot 0204 at gmail  
dot com" <gcc-bugzilla@gcc.gnu.org> wrote:

> Compiler Version:
> gcc (GCC) 4.2.4 (Ubuntu 4.2.4-1ubuntu3)
> Copyright (C) 2007 Free Software Foundation, Inc.
> This is free software; see the source for copying conditions.  There  
> is NO
> warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR  
> PURPOSE.
>
>
> Sample code:
>
> #include <stdio.h>
>
> int main() {
>        int a[2];
>        a[0]=10;
>        a[1]=20;
>        printf("a[0] = %d a[1] = %d\n", a[0], a[1]);
>        a[0]^=a[1]^=a[0]^=a[1];
>        printf("a[0] = %d a[1] = %d\n", a[0], a[1]);
>        return 0;
> }
>
> gcc test.c -o test -O2
>
> a[0] = 10 a[1] = 20
> a[0] = 20 a[1] = 10
>
> gcc test.c -o test -O0
>
> a[0] = 10 a[1] = 20
> a[0] = 0 a[1] = 10
>
> Bug: When compiled with -O0 swap fails.
> Swap only fails in case of array. Following code works fine with -O0:
>
> #include <stdio.h>
>
> int main() {
>        int a, b;
>        a=10;
>        b=20;
>        printf("a = %d b = %d\n", a, b);
>        a^=b^=a^=b;
>        printf("a = %d b = %d\n", a, b);
>        return 0;
> }
>
>
> -- 
>           Summary: Array + XOR swap fails
>           Product: gcc
>           Version: unknown
>            Status: UNCONFIRMED
>          Severity: major
>          Priority: P3
>         Component: c
>        AssignedTo: unassigned at gcc dot gnu dot org
>        ReportedBy: ashutosh dot sharma dot 0204 at gmail dot com
>
>
> http://gcc.gnu.org/bugzilla/show_bug.cgi?id=40366
>
Comment 2 Andreas Schwab 2009-06-07 09:04:07 UTC

*** This bug has been marked as a duplicate of 11751 ***
Comment 3 Jackie Rosen 2014-02-16 13:14:47 UTC Comment hidden (spam)