public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/39121]  New: strange behavior of a chain of operations.
@ 2009-02-06 19:40 nospam at pamies dot cat
  2009-02-06 20:10 ` [Bug c/39121] strange behavior in chained operations pinskia at gcc dot gnu dot org
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: nospam at pamies dot cat @ 2009-02-06 19:40 UTC (permalink / raw)
  To: gcc-bugs

#include <stdio.h>

/*
  Why the first swap operation works as expected but
  it does not happen the same with the second one ?
  I guess that it can be due operations within temp values,
  but IMHO swap operation should work in both cases.

  Thanks !
 */

void swap(int *a, int *b) {
    *a ^= *b ^= *a ^= *b;
}

int main() {
    int a = 5;
    int b = 8;
    printf("%d, %d\n", a, b);
    a ^= b ^= a ^= b;
    printf("%d, %d\n", a, b);
    swap(&a, &b);
    printf("%d, %d\n", a, b);
}


-- 
           Summary: strange behavior of a chain of operations.
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: major
          Priority: P3
         Component: c
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: nospam at pamies dot cat


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=39121


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [Bug c/39121] strange behavior in chained operations
  2009-02-06 19:40 [Bug c/39121] New: strange behavior of a chain of operations nospam at pamies dot cat
@ 2009-02-06 20:10 ` pinskia at gcc dot gnu dot org
  2009-02-06 21:07 ` nospam at pamies dot cat
  2009-02-06 21:22 ` rguenth at gcc dot gnu dot org
  2 siblings, 0 replies; 4+ messages in thread
From: pinskia at gcc dot gnu dot org @ 2009-02-06 20:10 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #1 from pinskia at gcc dot gnu dot org  2009-02-06 20:09 -------
This is undefined code as you are modifying *a twice without a sequence point
inbetween the modifies.

*** This bug has been marked as a duplicate of 15145 ***


-- 

pinskia at gcc dot gnu dot org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
         Resolution|                            |DUPLICATE


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=39121


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [Bug c/39121] strange behavior in chained operations
  2009-02-06 19:40 [Bug c/39121] New: strange behavior of a chain of operations nospam at pamies dot cat
  2009-02-06 20:10 ` [Bug c/39121] strange behavior in chained operations pinskia at gcc dot gnu dot org
@ 2009-02-06 21:07 ` nospam at pamies dot cat
  2009-02-06 21:22 ` rguenth at gcc dot gnu dot org
  2 siblings, 0 replies; 4+ messages in thread
From: nospam at pamies dot cat @ 2009-02-06 21:07 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #2 from nospam at pamies dot cat  2009-02-06 21:07 -------
Is not the same bug as #15145. I agree with you that there is just one sequence
point, but the operation is not undefined.

void swap(int *a, int *b) {
    *a ^= *b ^= *a ^= *b;
}

This code should be compiled to:

*a = *a ^ *b;
*b = *b ^ *a;
*a = *a ^ *b;

And not to something like (I think that is what happens):

int tmp;
tmp = *a ^ *b;
*b = *b ^ tmp;
//On that point *a should contain 5^8 instead of the original value 5.
//This happens because the temp variable generated by the compiler.
*a = *a ^ *b;  

I think that the compiler is not translating properly what was written in the
source code. Summarizing, I think that in:

y = 1;
x = (y += 1);

The execution order should be:

volatile_register <--- y + 1
y                 <--- volatile_register
x                 <--- volatile_register

instead of:

volatile_register <--- y + 1
x                 <--- volatile_register
y                 <--- volatile_register


-- 

nospam at pamies dot cat changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|RESOLVED                    |UNCONFIRMED
         Resolution|DUPLICATE                   |


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=39121


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [Bug c/39121] strange behavior in chained operations
  2009-02-06 19:40 [Bug c/39121] New: strange behavior of a chain of operations nospam at pamies dot cat
  2009-02-06 20:10 ` [Bug c/39121] strange behavior in chained operations pinskia at gcc dot gnu dot org
  2009-02-06 21:07 ` nospam at pamies dot cat
@ 2009-02-06 21:22 ` rguenth at gcc dot gnu dot org
  2 siblings, 0 replies; 4+ messages in thread
From: rguenth at gcc dot gnu dot org @ 2009-02-06 21:22 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #3 from rguenth at gcc dot gnu dot org  2009-02-06 21:21 -------
Evaluation order is undefined if there is no sequence point.


-- 

rguenth at gcc dot gnu dot org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
         Resolution|                            |INVALID


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=39121


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2009-02-06 21:22 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-02-06 19:40 [Bug c/39121] New: strange behavior of a chain of operations nospam at pamies dot cat
2009-02-06 20:10 ` [Bug c/39121] strange behavior in chained operations pinskia at gcc dot gnu dot org
2009-02-06 21:07 ` nospam at pamies dot cat
2009-02-06 21:22 ` rguenth at gcc dot gnu dot org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).