GCC -O3 6.1.1, 5.3.1, 4.1.2 fails to compile a correct executable (recursive merge sort in C) -


the following program not compiled correct executable when using gcc (at least 6.1.1, 5.3.1, 4.1.2) , optimization -o3 (and -o2). gcc generates correct executable -o0 (and -o1).

the output should (-o0):

sorted array: 1.000 2.000 3.000 4.000 5.000 6.000 7.000 8.000 9.00010.0009999.00012.00013.00014.00015.00016.00017.00018.0001                     9.00020.000 

instead of (-o3):

sorted array:11.00012.00013.00014.00015.00016.00017.00018.00019.00020.0009999.000 2.000 3.000 4.000 5.000 6.000 7.000 8.000                      9.00010.000 

has idea of reason? bug of gcc? thanks!


#include <stdio.h> #include <stdlib.h>  double arr[20] = {20,19,18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1};  int merge(double arr[],int l,int m,int h) {     double arr1[10],arr2[10];     int n1,n2,i,j,k;      n1=m-l+1; n2=h-m;     for(i=0; i<n1; i++) arr1[i]=arr[l+i];     for(j=0; j<n2; j++) arr2[j]=arr[m+j+1];      arr1[i]=9999; arr2[j]=9999; i=0; j=0;      for(k=l; k<=h; k++) {         if(arr1[i]<=arr2[j])             arr[k]=arr1[i++];         else             arr[k]=arr2[j++];     }  return 0; }  int merge_sort(double arr[],int low,int high) {     int mid;     if(low<high) {         mid=(low+high)/2;         merge_sort(arr,low,mid);         merge_sort(arr,mid+1,high);         merge(arr,low,mid,high);     }     return 0; }  int main() {     int i,n=20;      merge_sort(arr,0,n-1);     printf("sorted array:");     for(i=0; i<n; i++)         printf("%6.3f",arr[i]);     printf("\n");      exit(0); } 

there (at least) 1 bug.

in merge have:

double arr1[10],arr2[10]; 

the work arrays needs 1 larger fit watch value (9999), need:

double arr1[11],arr2[11]; 

Comments

Popular posts from this blog

java - Suppress Jboss version details from HTTP error response -

gridview - Yii2 DataPorivider $totalSum for a column -

Sass watch command compiles .scss files before full sftp upload -