Segmentation failure: Recorded core image

Asked

Viewed 5,464 times

0

I made a code in C to find the smallest value and position of it in a vector x[N]. Every time I compile the program by terminal (Linux), it accuses the following message:

Segmentation failure (recorded core image)

I used the same code on Windows and the program ran successfully.

#include <stdio.h>

int main(){
int N, i, menor, posicao,  x[N];

scanf("%d",&N);

i=0;

for(i;i<N;i++) scanf("%d",&x[i]);

menor = x[0];
posicao = 0;

for(i;i<N;i++){ 
    if(menor > x[i]){
        menor = x[i];
        posicao = i;
    } 
}   
printf("Menor valor: %d", menor);
printf("Posicao: %d", posicao);

return 0;
}
  • Did any of the answers solve your question? Do you think you can accept one of them? Check out the [tour] how to do this, if you haven’t already. You would help the community by identifying what was the best solution for you. You can accept only one of them. But you can vote on any question or answer you find useful on the entire site (when you have enough score).

2 answers

2

The main reason is that it is creating a vector with a random amount of positions, is whatever is in the memory at that moment, and this will burst the memory most of the time, but not at all.

What you probably wanted is to declare the vector after asking how many positions it would be (as I did in the code), then you reserve in memory the space for the quantity typed, no longer take an arbitrary value.

It has nothing to do with the operating system but with the memory junk that you have at that time of execution in the location that was allocated to your application. It may even repeat the same value, but it’s just a coincidence.

This mistake probably has to do with people’s mania to teach C how it was in the 1970s, or how it is in Pascal. So I’m critical of almost every programming course you have out there, they don’t teach you how to program in real life.

The code doesn’t take any precautions yet, but for exercise it’s fine. I have improved the organization to be more readable and more efficient (a loop solves the question, you don’t need to ask for the data and then sweep the entire array to find the desired information).

#include <stdio.h>
#include <limits.h>

int main() {
    int n;
    scanf("%d", &n);
    int x[n];
    int menor = INT_MAX;
    int posicao = 0;
    for (int i = 0; i < n; i++) { 
        scanf("%d", &x[i]);
        if (x[i] < menor) {
            menor = x[i];
            posicao = i;
        } 
    }   
    printf("Menor valor: %d Posicao: %d", menor, posicao);
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

  • Maniero, what’s the problem with my code?? Once it ran on Windows and did not run on Linux!!!!

  • I just explained, it has nothing to do with operating system, it’s virtually random, can even repeat the same situation over and over again.

  • Sorry!! I paid attention to your code and not your explanation above. I’m new to this community!!!

1

Short answer: you are using N to create the matrix x[N] before the value of N is set, so it is random (but usually it will be zero).

Declare x[N] after scanf(). In C99 it is allowed to declare variables after the start of the function.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.