0

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

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

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

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

1 回答 1

0

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

if (argc < 3)
    return -1;

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

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

你也可以这样做

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

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

于 2013-03-21T09:17:05.340 回答