【问题标题】:How to fix segmentation fault 11 error? [duplicate]如何修复分段错误 11 错误? [复制]
【发布时间】:2017-06-04 11:05:42
【问题描述】:

我希望主要返回“dati”中“mdl”出现的位置。我设置了“模式”函数来查找每次出现的起点,但是当我从命令行运行程序时,它返回:

Segmentation fault: 11

我不知道如何解决这个问题。 代码如下:

#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;
}

我必须使用函数来查找出现,我不能使用其他方式。

【问题讨论】:

  • 专业提示一:为编译器启用警告,并注意它们(最好也将它们设置为错误处理)。
  • *soluzione = schema(dati, n, mdl, m, start); : soluzionestart 未初始化。
  • @n.m.我启用了警告,当我编译程序时没有错误或警告......
  • 你用的是什么编译器?考虑买一个更好的。对于任何现代编译器来说,像 soluzione 这样的未初始化变量都是一个简单的游戏。
  • @n.m.我正在使用 GCC。

标签: c segmentation-fault find-occurrences


【解决方案1】:

您正在取消引用指针 soluzione,但它从未使用值初始化:

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

读取一个未初始化的值,以及随后取消引用该未初始化的值,调用undefined behavior。在这种情况下,它表现为分段错误。

在这种情况下,您不需要指针。只需将变量声明为int

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

你也不初始化start。结果,您在可能超出数组边界的未知位置索引到 testo。这也会调用未定义的行为。

编辑:

看起来您实际上从 schema 返回了错误的数据类型。如果你想返回一个指向本地数组r的指针(在这种情况下很好,因为它被声明为static,函数需要返回一个int *,你应该return r

然后在main 中,您将保留soluzione 作为指针但直接分配给它。

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);

【讨论】:

  • 注意start也没有初始化。
  • @VladislavMartin 很好。编辑包括在内。
  • @dbush 谢谢。我理解并修复了代码。但是现在我遇到了一个新问题:它会产生错误:error: subscripted value is not an array, pointer, or vector printf("- risultato[%d] = %d\n", i, soluzione[i]);
  • @FranzGoogle 查看我的编辑。
  • @dbush 非常感谢!现在它完美地工作了!我还有一个问题:如果我想保留schema(而不是*schema),我与rsoluzione 有什么关系?
【解决方案2】:

我认为错误在于以下代码段:

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]) {

请注意,您将dati 传递为testo,它是一个大小为n 的整数数组,并且您将n 作为nT 的值传递。因此,testo 的大小为 nT。 但是在你的循环中,i 可能一直运行到nt-1,你访问testo[i+7],这超出了testo 的边界,对吧?

【讨论】:

  • 我认为你是对的,谢谢。但是在同一程序的更简单版本中,它没有出现问题,我不知道为什么......我不是专家,我正在学习它。
猜你喜欢
  • 2016-03-13
  • 1970-01-01
  • 1970-01-01
  • 2012-09-04
  • 1970-01-01
  • 2020-12-13
  • 2022-11-21
  • 1970-01-01
相关资源
最近更新 更多