hi! I have a problem with multiply assigment at i386-portbld-freebsd5.4 gcc version 4.0.1 20050609 (prerelease) [FreeBSD] and the same problem in DJGPP with gcc 4.0.0 I've compiled my cpp file with this command line: g++40 new.cpp ---new.cpp---- #include<cstdio> #define swap(a,b) a^=b^=a^=b const int N = 2; int a[N]; int main() { for(int i=0;i<N;i++) a[i] = i; for(int i=0;i<N/2;i++) swap(a[i],a[N-i-1]); for(int i=0;i<N;i++) printf("%d ",a[i]); printf("\n"); return 0; } ------- This program simply makes 2-element array: a[0] = 0 a[1] = 1; After that it uses macros swap(a[0],a[1]) to swap these elements. After swaping program makes a[0] = 0 a[1] = 0 that's not correct. Actually, if you compile this program with -O2 key: g++40 -O2 new.cpp program will work fine and make a[0] = 1 a[1] = 0; The same situation(correct work) will be if you use older gcc 3.x compiler. I've done several experements and I guess that something wrong causes after last assigment.
Subject: Re: New: Incorrect work with multiple assigment. "algorithmus at gmail dot com" <gcc-bugzilla@gcc.gnu.org> writes: | ---new.cpp---- | #include<cstdio> | | #define swap(a,b) a^=b^=a^=b You're modifying objects more than once without intervening sequence points. All bets are off. There is a standard utility std::swap() from <utility> that works all the time. PR invalid. -- Gaby
There is no sequence points between the assignments of a[i] so the loading of a[i] can be evaluated in any order. You want to define swap as: #define swap(a,b) do{ a^=b;b^=a;a^=b;}while(0) This is a dup of bug 11751. *** This bug has been marked as a duplicate of 11751 ***
no. I guess there is a problem. just try this progam: ------------ #include<cstdio> #define swap(a,b) (a^=(b^=(a^=b))) const int N = 2; int a[N]; int main() { for(int i=0;i<N;i++) a[i] = i; for(int i=0;i<N/2;i++) swap(a[i],a[N-i-1]); for(int i=0;i<N;i++) printf("%d ",a[i]); printf("\n"); return 0; } -------- the same result... 0 0
Parentheses are not seqeunce points. The comma operator is though. *** This bug has been marked as a duplicate of 11751 ***
Also you could simply use std::swap as Gaby suggested.
thanks for your suggestions. I usually use STL. But I held training contest for Ukrainian team before International Olympiad in Informatics and one guy's used this swap macros that doesn't work at my compiler but works fine with his. I guess, it'll be useful if the developers add some warning message for similar- to-this cases.
It's there: -Wsequence-point, which is also enabled by -Wall.
(In reply to comment #7) > It's there: -Wsequence-point, which is also enabled by -Wall. But it will not warn about this testcase as we have array[index] but that is filed as PR 16202.
Subject: Re: Incorrect work with multiple assigment. "algorithmus at gmail dot com" <gcc-bugzilla@gcc.gnu.org> writes: | I guess, it'll be useful if the developers add some warning message | for similar-to-this cases. Did you try -Wsequence-points (or similar)? -- Gaby
(In reply to comment #9) > > Did you try -Wsequence-points (or similar)? yeah. both -Wall and -Wsequence-points return nothing.
Subject: Re: Incorrect work with multiple assigment. "algorithmus at gmail dot com" <gcc-bugzilla@gcc.gnu.org> writes: | (In reply to comment #9) | > | > Did you try -Wsequence-points (or similar)? | | yeah. both -Wall and -Wsequence-points return nothing. then reclassify the bug as missed warning, (not incorrect code). -- Gaby
(In reply to comment #11) > then reclassify the bug as missed warning, (not incorrect code). Did you read my comment #8, which I referenced the bug about the warning: > But it will not warn about this testcase as we have array[index] but that is filed as PR 16202.
Subject: Re: Incorrect work with multiple assigment. "pinskia at gcc dot gnu dot org" <gcc-bugzilla@gcc.gnu.org> writes: | (In reply to comment #11) | > then reclassify the bug as missed warning, (not incorrect code). | Did you read my comment #8, I read it. I was responding to the bug submitter. Do assume, people are not blind. | which I referenced the bug about the warning: | > But it will not warn about this testcase as we have array[index] but that is filed as PR 16202. I don't think you're explaining that we cannot warn. Are you? -- Gaby
> | > But it will not warn about this testcase as we have array[index] but that is filed as PR 16202. > I don't think you're explaining that we cannot warn. Are you? I can't understand what Andrew means. I agree that we should reclassify this bug as missing warning. Because sometimes programmers could get into this problem.
(In reply to comment #14) > > | > But it will not warn about this testcase as we have array[index] but that > is filed as PR 16202. > > I don't think you're explaining that we cannot warn. Are you? I am explaining why we currently don't warn and that the diagnostic problem is already filed as PR 16202. *** This bug has been marked as a duplicate of 11751 ***