#include <stdio.h>
#include <stdlib.h>
int main(int argc, char** argv) {
int svc = 0;
int dsvc = 0;
//int clc = 0;
int buffer[] = {0, 1, 2, 2, 2, 3, 4, 6, 1};
for(int i = 0; i < 9; i++) {
int sv, dsv;
sv = buffer[i];
dsv = sv - svc;
int ddsv = dsv - dsvc;
//printf(" %d %d %d \n", svc, sv, ddsv);
if(ddsv > -1 && ddsv < 1) {
printf(" %d\n", ddsv);
//clc ++;
} /*else {
if(clc > 0) {
printf("[%d]\n", clc);
}
clc = 0;
}*/
svc = sv;
dsvc = dsv;
}
return 0;
}
-std:C11 -Tx86-coff -Ob1 -fp:precise -W1 -Gd
output:
0
0
0
0
-std:C11 -Tx86-coff -Ot -Ob1 -fp:precise -W1 -Gd
output:
0
0
-1
0
0
Pelles C 7.00.350 win64
RSRC0009.DLL: Version 7.00.1
SUPPORT64.DLL: Version 7.00.0
CFORMAT64.DLL: Version 7.00.12
FDIFF64.DLL: Version 7.00.2
PORC64.DLL: Version 7.00.15
POBR64.DLL: Version 7.00.1
SQLITE364.DLL: Version 3071100
POCC.EXE: Version 7.00.18
POASM.EXE: Version 7.00.3
POLINK.EXE: Version 7.00.3
IDESPAWN64.EXE: Version 7.00.1
I get the first result (0,0,0,0) for any optimisation .
Pelles C 7.00.355 , Windows 7 32 bit
RSRC0009.DLL: Version 7.00.1
SUPPORT.DLL: Version 7.00.0
CFORMAT.DLL: Version 7.00.12
FDIFF.DLL: Version 7.00.3
PORC.DLL: Version 7.00.15
POBR.DLL: Version 7.00.1
SQLITE3.DLL: Version 3071100
POCC.EXE: Version 7.00.25
POASM.EXE: Version 7.00.3
POLINK.EXE: Version 7.00.3
IDESPAWN.EXE: Version 7.00.1
Interestingly i got a similar compiler bug as well today. It only appears when using any kind of optimization and is similar to a bug i previously posted, this time it is a global pointer to a struct.
Unfortunately my code is too complex to replicate, and smaller examples didn't show the bug.
Does anyone have any suggestion what should i do in this case? I really like this IDE and I have learned a Lot with it, but i really don't think coding around compiler bugs is optimal. Is it time to switch PellesC for something else?
It turns out i had a bug in MY code. (⌒_⌒;) My code was fixed, bug is still here; now I'm 100% it's the optimization bug.
Yes it is definetly an optimization bug.
For some reason the compiler simplifies the comparison on line:
if((ddsv > -1) && (ddsv < 1)) {
generating a single and simple jump above (ja) statement :'(.
As a workaround declare volatile the variable ddsv
int volatile ddsv = dsv - dsvc;
As general rule always try to compile your code without any optimization, if it works there could be an optimization bug.
You can then try to remove it disabling optimization for a specific function. You can do this by enclosing function in #pragmas
#pragma optimize(none) //Disable optimization
int myfunc(....) //function
#pragma optimize() //Restore command line optimizations