How to fix segmentation fault 11 error? [duplicate]

回眸只為那壹抹淺笑 提交于 2019-12-22 01:09:15

问题


I want the main returns the position of the occurrences of "mdl" in "dati". I set up the "schema" function to find the starting point of each occurrence, but when i run the program from the command line, it returns:

Segmentation fault: 11

I don't know how to fix the problem. Here's the code:

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

int schema(int testo[], int nT, int modello[], int nM, int primo) {

    int i, j, k;
    static int r[12];

    j=0;
    for(i=primo; i<nT; i++) {
        if(testo[i] == modello[0] && testo[i+1] == modello[1] && testo[i+2] == modello[2] && testo[i+3] == modello[3] && testo[i+4] == modello[4] && testo[i+5] == modello[5] && testo[i+6] == modello[6] && testo[i+7] == modello[7]) {
        r[j] = i+1;
        j++;
        }
    }

    return *r;
}



int main(int argc, char** argv) {

    FILE *in;
    FILE *out;

    int i, m;
    const int n = 100;
    int dati[n];
    int *soluzione;
    int start;

    if ((in=fopen("dati.txt", "r"))==NULL){
        return -1;
    }

    for(i=0; i<n; i++) {
        if (fscanf(in, "%d", &dati[i]) < 0){
            fclose(in);
            return i;
        }
    }

    int mdl[] = {0,0,0,1,1,1,0,1};
    m = sizeof(mdl)/sizeof(mdl[0]);

    *soluzione = schema(dati, n, mdl, m, start);

    for(i=0; i<12; i++) {
        printf("- risultato[%d] = %d\n", i, soluzione[i]);
    }

    //out = fopen("risultati.txt", "w");
    //...

    fclose(in);

    return 1;
}

I have to use the function to find the occurrences, I cannot use other ways.


回答1:


You're dereferencing the pointer soluzione, but it was never initialized with a value:

int *soluzione;
...
*soluzione = schema(dati, n, mdl, m, start);

Reading an uninitialized value, as well as subsequently dereferencing that uninitialized value, invokes undefined behavior. In this case, it manifests in a segmentation fault.

You don't need a pointer in this case. Just declare the variable as an int.

int soluzione;
...
soluzione = schema(dati, n, mdl, m, start);

You also don't initialize start. As a result, you index into testo at an unknown location which could be outside the bounds of the array. This also invokes undefined behavior.

EDIT:

It looks like you're actually returning the wrong datatype from schema. If you want to return a pointer to the local array r (which in this case is fine since it's declared as static, the function needs to return an int * and you should return r.

Then in main you would keep soluzione as a pointer but assign to it directly.

int *schema(int testo[], int nT, int modello[], int nM, int primo) {
    ...
    return r;
}

int main(int argc, char** argv) {
    ...
    int *soluzione;
    ...
    soluzione = schema(dati, n, mdl, m, start);



回答2:


I suppose the error lies in the following code segment:

for(i=primo; i<nT; i++) {
    if(testo[i] == modello[0] && testo[i+1] == modello[1] && testo[i+2] == modello[2] && testo[i+3] == modello[3] && testo[i+4] == modello[4] && testo[i+5] == modello[5] && testo[i+6] == modello[6] && testo[i+7] == modello[7]) {

Note that you pass dati, which is an integer array of size n, as testo and you pass n as the value for nT. Hence, testo is of size nT. But in you loop, where i potentially runs until nt-1, you access testo[i+7], which exceeds the boundaries of testo, right?



来源:https://stackoverflow.com/questions/41747046/how-to-fix-segmentation-fault-11-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!