Hi everyone,
I ran across a really strange problem with the -O3 optimization of gcc, and I would appreciate any help because I really have a hard time understanding the problem. I have boiled down my considerably longer code to a simple example that shows the behaviour. What it does is simply allocating an array, setting its 0-element to 0 and then setting it to one in a loop that is executed only once. The code
Code:
#include <stdlib.h>
#include <stdio.h>
int main() {
int i;
double R4 = 0;
int *activedirections;
int nactive=0;
int holder;
activedirections = malloc((2*3+1) *sizeof(int));
activedirections[0] = 0;
for (i=1; i<=1; i++) {
activedirections[nactive] = i;
nactive++;
}
holder = activedirections[0];
printf("activedirections[0] = %i\n",activedirections[0]);
printf("activedirections[0] = %i\n",activedirections[0]);
printf("holder = %i\n",holder);
free(activedirections);
}
when compiled with gcc and no options produces:
Code:
activedirections[0] = 1
activedirections[0] = 1
holder = 1
as expected. Compiling with -O3 however yields:
Code:
activedirections[0] = 0
activedirections[0] = 1
holder = 0
I have no idea how a value can change in between two identical printf's
Any help would be great.
Thanks