Why only codeblocks give me return error(0xc00000FD)?

0

When I compile the program with Codeblocks (GNU gcc compiler) it gives me this error: Process returned -1073741571 (0xC00000FD) I tried it without the binary search and it works, but I can't understand why when I insert the binary search it gives me this error. But if I compile it with another compiler (I tried on mine university online compiler) it works very well.

#include <stdio.h>
#include <stdlib.h>

int main()
{   
    int n;
    int v[n];
    int i;
    int aux;
    int j;

    printf("Quanti numeri vuoi inserire? \n");
    scanf("%d",&n);
    for(i=0;i<n;i++){
        printf("%d: ",i);
        scanf("%d",&v[i]);
    }
    for(i=0;i<n;i++) {
        printf("%d,",v[i]);
    }
    printf("\n");

    for(j=0;j<n-1;j++) {
        for(i=0;i<n-1;i++) {
            if(v[i]>v[i+1]) {
                aux = v[i];
                v[i] = v[i+1];
                v[i+1] = aux;
            }
        }
    }
    for(i=0;i<n;i++)
    printf("%d ", v[i]);

    //Binary search
    int alto, basso, pos, ele, k;
    alto = 0;
    basso = n-1;
    pos = -1;

    printf("Mi dia un elemento da cercare:\n");
    scanf("%d",&ele);

    while(alto<=basso && pos==-1){
        k=(alto+basso)/2;
            if(v[k]==ele)
                pos=k;
            else
                if(v[k]<ele)
                    alto=k+1;
            else
                basso=k-1;
    }
    if(pos==-1)
        printf("Il numero inserito non e' presente");
    else
        printf("L'elemento e' presente in posizione %d del vettore", pos);
    return 0;
}
c
return
return-value
asked on Stack Overflow Dec 12, 2019 by Vicez • edited Dec 12, 2019 by dragosht

1 Answer

0
int n;
int v[n];

you have undefined behaviour(UB) here.

you declare a VLA with the size set by not initialized variable n.

So answering the question

Why only codeblocks give me return error(0xc00000FD)

the answer is : because it does. It is not predictable.

answered on Stack Overflow Dec 12, 2019 by 0___________

User contributions licensed under CC BY-SA 3.0