c - C中的段错误,似乎无法找到原因

标签 c segmentation-fault

我似乎无法在下面的代码中找到段错误的原因。从evaluate() 跳回main() 后,代码在注释行中崩溃。我用 gdb 查看了这段代码,在评估函数中似乎一切正常 - 输出已正确填写,但是在我返回 main 后,内存中的任何内容都无法访问。 gdb 中的“p j”返回内存不可访问错误,尝试打印任何其他变量也是如此。我检查了堆栈是否可能有很多值,但增加堆栈大小没有帮助。

疯狂的是我可以解决这个错误,但我不知道为什么它会改变任何东西。如果我在evaluate()中的任何地方添加一个int声明(例如int iAmNotUsedEver;),那么代码突然不会导致段错误并且在gdb中完美工作。 编辑:在评估中动态分配节点[116] (int *node = malloc(116*sizeof(int));) 也解决了这个问题,但我又不知道为什么?

评估函数:(我删除了部分代码,否则会太长)

void evaluate(int *parameter, int output[][16]) {
    int node[116];
    node[0] = 0;
    output[0][0] = !node[0];
    node[1] = parameter[0];
    output[0][1] = !node[1];
    output[0][2] = !node[0];
    ...
    node[34] = !node[114] && !node[45];
    node[45] = !node[34] && !node[105];
    output[11][15] = node[45];
}

主要功能:

int main(int argc, char *argv[]) {
    int i;
    int j;
    int k;
    int ret;
    int parameter[8];
    int output[12][16];
    FILE *paramFile;
    FILE *outFile;

    paramFile = fopen(argv[1], "r");
    if (paramFile == NULL) {
        printf("I couldn't open the parameter file.\n");
        exit(0);
    }
    outFile = fopen(argv[2], "ab");
    if (outFile == NULL) {
        printf("Something went wrong with the output file.\n");
        exit(0);
    }
    while(1){
        for(i=0;i<8;i++){
            ret=fscanf(paramFile, "%d", &parameter[i]);
            printf("parameter: %d\n", parameter[i]);
        }
        if(ret!=1){
            break;
        }
        for(j=0;j<12;j++){
            for(k=0;k<16;k++){
                output[j][k] = 2;
            }
        }
        evaluate(parameter,output);
        printf("Evaluation is done. \t %d\n",i);
        for(j=0;j<12;j++){ //SEG FAULT HERE
            for(k=0;k<16;k++){
                fprintf(outFile, "%d", output[j][k]);
            }
            fprintf(outFile,"\n");
        }
        fprintf(outFile,"\n\n\n");
    }
    printf("Closing files\n");  
    fclose(paramFile);
    fclose(outFile);
    return 0;
}

最佳答案

首先你应该检查是否至少有两个参数。这也可能引起段错误。

if (argc < 3)
    return -1;

然后,在评估时,因为它们是固定数组,所以您应该传入参数

void evaluate(int parameter[8], int output[12][16]) {
    ...
}

你也可以这样做

void evaluate(int *parameter, int **output) {
    ...
}

我认为在这两个声明上使用一个可以避免段错误。

关于c - C中的段错误,似乎无法找到原因,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15542896/

相关文章:

c++ - 如何在Qt C++中使用C函数

c - 尝试进入 for 循环时出现段错误(核心转储)

c - 为什么编译器会被 unsigned int 混淆成一个 SIGSEGV?

c++ - 尽管有 pop(),如何避免删除 priority_queue::top()?

c - 双向链表删除函数中的段错误和异常输出

c++ - 来自带有分隔符的 std::getline 的段错误

ios - NSString 到带有希腊字符的 char*

我可以使用 getchar() 清除 scanf 和 fgets 之间的缓冲区吗?

c - IA-64 上的段错误,但 IA-32 上没有

c - 头文件未正确包含