You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Wrong C code:
#include <stdio.h>
int main(){
int i = 0;
int a[] = {10,20,30};
int r = 1 * a[i++] + 2 * a[i++] + 3 * a[i++];
printf("%d
", r);
return 0;
}
Clang gives on that code:
warning: multiple unsequenced modifications to 'i' [-Wunsequenced]
GCC 4.8.0 gives on that code:
test.c: In function 'main':
test.c:5:46: warning: operation on 'i' may be undefined [-Wsequence-point]
int r = 1 * a[i++] + 2 * a[i++] + 3 * a[i++];
^
test.c:5:46: warning: operation on 'i' may be undefined [-Wsequence-point]
This similar D code compiles and prints "140":
void main() {
import std.stdio;
int i = 0;
auto a = [10, 20, 30];
int r = 1 * a[i++] + 2 * a[i++] + 3 * a[i++];
r.writeln;
}
I suggest to statically forbid such D code, or to make it defined and deterministic on all D compilers. (From my coding experience, I find that kind of code often confusing for the programmer too, I refactor away that kind of code, so probably I'd like it to be forbidden).
More info:
http://www.open-std.org/jtc1/sc22/wg14/www/docs/n925.htmhttp://www.open-std.org/jtc1/sc22/wg14/www/docs/n926.htmhttp://www.open-std.org/jtc1/sc22/wg14/www/docs/n927.htm
The text was updated successfully, but these errors were encountered:
bearophile_hugs reported this on 2013-11-30T04:11:14Z
Transferred from https://issues.dlang.org/show_bug.cgi?id=11647
CC List
Description
Wrong C code: #include <stdio.h> int main(){ int i = 0; int a[] = {10,20,30}; int r = 1 * a[i++] + 2 * a[i++] + 3 * a[i++]; printf("%d ", r); return 0; } Clang gives on that code: warning: multiple unsequenced modifications to 'i' [-Wunsequenced] GCC 4.8.0 gives on that code: test.c: In function 'main': test.c:5:46: warning: operation on 'i' may be undefined [-Wsequence-point] int r = 1 * a[i++] + 2 * a[i++] + 3 * a[i++]; ^ test.c:5:46: warning: operation on 'i' may be undefined [-Wsequence-point] This similar D code compiles and prints "140": void main() { import std.stdio; int i = 0; auto a = [10, 20, 30]; int r = 1 * a[i++] + 2 * a[i++] + 3 * a[i++]; r.writeln; } I suggest to statically forbid such D code, or to make it defined and deterministic on all D compilers. (From my coding experience, I find that kind of code often confusing for the programmer too, I refactor away that kind of code, so probably I'd like it to be forbidden). More info: http://www.open-std.org/jtc1/sc22/wg14/www/docs/n925.htm http://www.open-std.org/jtc1/sc22/wg14/www/docs/n926.htm http://www.open-std.org/jtc1/sc22/wg14/www/docs/n927.htmThe text was updated successfully, but these errors were encountered: