title: Codeblocks vs GCC different output (strange behavior)?
body: <p>I was trying to answer this <a href="https://stackoverflow.com/questions/26735839/sorting-dynamic-array-in-a-structure-with-c-qsort">question</a> and after writing this code </p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;time.h&gt;
  #include &lt;math.h&gt;
  
  int M =10;
  int N =30;
  int K = 10;
  
  struct element {
      int *i;
      int *j;
      int k;
  };
  
  struct element *create_structure();
  void print_element(struct element *);
  int compare (const void *, const void * );
  void sort(struct element *); // changed the return value of sort
  // to void as the argument will be changed directly because it is a
  // pointer
  
  
  int main()
  {
      srand(time(NULL));
      struct element *lista;
      lista=create_structure();
      printf("\n--------- i ---  j  ---------\n\n");
      print_element(lista);
      printf("\n---------------------------\n");
  
      sort(lista);
      print_element(lista);
      return 0;
  
  }
  
  
  struct element *create_structure()
  {
      int aux1=0,count=0;
      struct element *structure;
      // Changed the allocation of structure pointer
      structure = (struct element *) malloc (sizeof(struct element));
      structure-&gt;k=K;
      structure-&gt;i= (int *)malloc(K*sizeof(int));
      structure-&gt;j=(int *)malloc (K*sizeof(int));
      for (count = 0; count &lt; K; count ++)
      {
          aux1=rand()%N;
          // we kept only the first aux1 and copied it in the two arrays
          (structure-&gt;i)[count]=aux1;
          (structure-&gt;j)[count]=aux1;
      }
      return (structure);
  }
  
  void print_element(struct element *lista)
  {
      int count=0;
      for(count = 0; count &lt; K; count++)
      {
          printf("row=%2d :  %2d     %2d\n",count+1,(lista-&gt;i)[count],(lista-&gt;j)[count]);
      }
  }
  
  
  int compare(const void *a, const void *b)
  {
      // compare the values of two case of array pointed by i of type int
      return *(int*)a-*(int*)b;
  }
  
  
  void sort(struct element *list)
  {
    // we will sort the array pointed by i which contains K elements
    // of type int and size sizeof(int) by using the compare function
      qsort(list-&gt;i, K , sizeof(int), compare);
  }
  </code></pre>
  
  <p>I realized that compiling it with <code>gcc</code> command from terminal gives correct output but using the codeblocks v13.12 which uses the same internal GNU GCC compiler gives wrong output !!</p>
  
  <p>The output of <code>gcc</code> command: <code>gcc -g -Wall main.c -o exec</code> </p>
  
  <p>gcc version 4.8.2 (Ubuntu 4.8.2-19ubuntu1)</p>
  
  <pre><code>--------- i ---  j  ---------
  
  row= 1 :   8      8
  row= 2 :  29     29
  row= 3 :   9      9
  row= 4 :  11     11
  row= 5 :  20     20
  row= 6 :  21     21
  row= 7 :   4      4
  row= 8 :  16     16
  row= 9 :   9      9
  row=10 :   2      2
  
  ---------------------------
  row= 1 :   2      8
  row= 2 :   4     29
  row= 3 :   8      9
  row= 4 :   9     11
  row= 5 :   9     20
  row= 6 :  11     21
  row= 7 :  16      4
  row= 8 :  20     16
  row= 9 :  21      9
  row=10 :  29      2
  </code></pre>
  
  <p>the output of codeblocks (<strong>please note the number of the starting row 3</strong>)</p>
  
  <pre><code>row= 3 :  20     20
  row= 4 :   4      4
  row= 5 :  13     13
  row= 6 :   8      8
  row= 7 :   4      4
  row= 8 :   7      7
  row= 9 :  21     21
  row=10 :  12     12
  
  ---------------------------
  row= 1 :   4     24
  row= 2 :   4     22
  row= 3 :   7     20
  row= 4 :   8      4
  row= 5 :  12     13
  row= 6 :  13      8
  row= 7 :  20      4
  row= 8 :  21      7
  row= 9 :  22     21
  row=10 :  24     12
  </code></pre>
  
  <p><img src="https://i.stack.imgur.com/nyXCh.png" alt="enter image description here"></p>
  
  <p>So I was wondering what can lead to such behavior ? </p>