Problem with gcc -O3 please help
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
Re: Problem with gcc -O3 please help
I also executed your program using gcc compiler only.
I got same output for both gcc, gcc -O3.
activedirections[0] = 1
activedirections[0] = 1
holder = 1
-O is for optimizing the C code. Which version you are using and Which OS you are using can you explain that.
I am using gcc in Linux OS.
Re: Problem with gcc -O3 please help
I am using 4.1.2 Gcc version.
Actually, for all the optimization level I got only the same results. you are using improvised GCC version. so that only you got some different output I think so.
But I need to work with 4.3.2 or read about that.Then only I can reply.
And If it is possible you can also try with 4.1.2 version and check the difference.