Thread: Problem with gcc -O3 please help

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    6

    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
    Last edited by forceael; 03-01-2010 at 05:03 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. weird problem in constant definition
    By psx-c in forum C Programming
    Replies: 2
    Last Post: 10-06-2009, 01:25 PM
  2. Replies: 4
    Last Post: 09-02-2007, 08:47 PM
  3. Compiles on gcc 3.3 but not on gcc 4.0.3
    By cunnus88 in forum C++ Programming
    Replies: 5
    Last Post: 03-29-2007, 12:24 PM
  4. Bin packing problem....
    By 81N4RY_DR460N in forum C++ Programming
    Replies: 0
    Last Post: 08-01-2005, 05:20 AM
  5. gcc
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 21
    Last Post: 10-22-2003, 03:46 PM