【问题标题】:My C program runs in Dev C++ but gets a segmentation fault in another compiler [closed]我的 C 程序在 Dev C++ 中运行,但在另一个编译器中出现分段错误 [关闭]
【发布时间】:2016-07-24 18:15:08
【问题描述】:

所以我昨晚在 Dev C++ 中完成了我的最新任务,这是我们创建它所需要的。让它完美地工作,编译得很好,运行得很好,一切都很好。但是,我们必须使用他们用来评分的我们大学的系统,通过“MobaXTerm”和 VPN 运行我们的程序。我尝试运行它,但出现错误,“分段错误”。那是我第一次听说这个错误,所以我研究了它。在大多数情况下,它会将错误的变量放在“for 循环”中,但我检查了我的循环 50 次,它可以工作。我认为它与我的 2D 数组有关,可能超出范围,但我进行了调试,据我所知,它并没有逃脱我为它分配的大小。

我不只是想把我的整个程序粘贴在这里,但我会给出一些 sn-ps,也许有人可以尝试告诉我可能是什么问题。我只是不明白它是如何在一个而不是另一个中运行良好的。

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

#define ROWS 12
#define COLS 8

void makeArray(FILE*, int [][COLS]);
int getScore(int [][COLS], int, int);
int getMonthMax(int [][COLS], int);
int getYearMax(int [][COLS]);
float getMonthAvg(int [][COLS], int);
float getYearAvg(int [][COLS]);
int toursMissed(int [][COLS]);
void displayMenu();
int processRequest(int [][COLS], int);
void printArray(int [][COLS]);

int main(){
int scoresArray[ROWS][COLS];
int choice, constant = 0;
FILE *scoreFileptr;
scoreFileptr = fopen("scores.txt", "r");
if (scoreFileptr == NULL)
    printf("The file isn't opening");
makeArray(scoreFileptr, scoresArray);

while(constant == 0){
  displayMenu();
  scanf("%d", &choice);
  processRequest(scoresArray, choice);
}

fclose(scoreFileptr);
}

这是一个函数:

void makeArray(FILE *scoreFileptr, int scoresArray[][COLS]){
int i, j, filler = 0;

for(i = 0; i < ROWS; i++){
    for(j = 0; j < COLS; j++){
        fscanf(scoreFileptr, "%d", &filler);
        if(filler == 999){
            break;
        }
        scoresArray[i][j] = filler;

    }
  }
}

这是另一个函数:

int processRequest(int scoresArray[][COLS], int choice){
int month = 0, tourNum = 0;

switch(choice){

    case 1: 
        printf("Please enter the month and the game\n");
        scanf("%d %d", &month, &tourNum);
        printf("The score for tournament %d is %d\n\n",tourNum, getScore(scoresArray, month, tourNum));

    break;

    case 2:     
        printf("Please enter the month\n");
        scanf("%d", &month);
        printf("The max score in month %d is %d\n\n", month, getMonthMax(scoresArray, month));
    break;

    case 3:
        printf("Please enter the month\n");
        scanf("%d", &month);
        printf("The average score for month %d is %.2f\n\n", month, getMonthAvg(scoresArray, month));
    break;

    case 4:
        printf("The max score for the year is %d\n\n", getYearMax(scoresArray));
    break;

    case 5:
        printf("The average score for the year is %.2f\n\n", getYearAvg(scoresArray));
    break;

    case 6:
        printf("The number of tournaments missed for the year is %d\n\n", toursMissed(scoresArray));
    break;

    case 7:
        printf("The scores for the year are:\n");
        printArray(scoresArray);
    break;

    case 0: printf("Thank you! Goodbye");
    exit(0);
    break;

    default:
        printf("Please enter one of the options listed\n\n");

}
return 0;

}

你认为这是我将数组传递给函数的方式吗?这是我能想到的唯一方法。但是为什么它会在 Dev C++ 编译器中工作呢?我被难住了。

编辑:问题似乎出在“makeArray”函数中。我在 main 中取出了它的函数调用,它现在运行。只需要弄清楚是什么原因造成的。

编辑 2:在逐行执行函数后,他们的问题似乎是 makeArray 函数中的“fscanf”行。谢谢大家为我指明正确的方向!

【问题讨论】:

  • 哪一行导致分段错误?
  • @SurvivalMachine 我不知道。它没有经过任何代码,只是说明存在分段错误。我在 main 的开头放了一个 printf ,它甚至没有打印出来。
  • 你试过调试器吗?
  • @melpomene 我在 Dev C++ 中尝试过这个,但它并没有指出问题所在。

标签: c arrays segmentation-fault out-of-memory


【解决方案1】:

由于没有完整的代码,我会告诉你原因和寻找什么,但我无法提供确切的答案。

在 C 语言中有一种叫做未定义行为的东西。这意味着该标准不保证在这种情况下会发生什么。它可能工作,它可能在运行时失败,基本上任何事情都可能发生。

遇到的最常见的未定义行为错误是与内存相关的错误。例如,假设以下代码 sn -p:

int a[10];
a[10] = 2;

我访问了数组 a 的分配区域之外的一个元素。根据标准,这是未定义的行为,因此没有暗示会发生什么。在实践中,通常有两种结果:它可以运行得几乎没有问题,或者它会产生分段错误,具体取决于编译器。

为什么它依赖于编译器?好吧,这种代码进入堆栈内存区域,由编译器管理。在某些情况下,覆盖数组之后的内存位置可能不会覆盖程序的重要内存区域,并且程序可以完成执行。在其他情况下,编译器决定将一些重要数据放在数组之后(如堆栈指针),如果被覆盖,则会导致程序崩溃。此外,该数组之后可能有一些受保护的内存区域,或者您可能会破坏您计划使用的另一个变量的值。

(我只描述了一些常见的可能结果。这种类型的错误可能会导致其他问题)。

碰巧用 Dev C++ 编译的代码运行时会出现这个错误。请注意,这绝不是一件好事 - 应不惜一切代价避免未定义的行为。

正如我所说,很难说问题出在哪里。只是一些调试提示:尝试删除代码的某些部分直到它工作,然后逐个添加并尝试找到发生错误的行。

附:另外,请确保您有可用的输入文件。

【讨论】:

  • 感谢您的帮助。我对主要注释中的“makeArray”进行了函数调用,并且代码运行了。因此,我将通过该功能并查看问题。我认为这与我使用我在 main 中定义的二维数组作为函数中的参数的方式有关。再次感谢!
【解决方案2】:
#include <stdio.h>
#include <stdlib.h>

#define ROWS 12
#define COLS 8

void makeArray(FILE* scoreFileptr,int (*scoresArray)[COLS]);

int main(){
  int scoresArray[ROWS][COLS];
  int choice, constant = 0;

  FILE* scoreFileptr = fopen("scores.txt", "r");
  if (scoreFileptr == NULL){
    printf("The file isn't opening\n");
  }else{ printf("opened\n");}   
  makeArray(scoreFileptr,scoresArray);
  //printf("%d\n",scoresArray[0][3]); test
  fclose(scoreFileptr);

}


void makeArray(FILE* scoreFileptr,int (*scoresArray)[COLS]){
  int i, j, filler = 0;

  for(i = 0; i < ROWS; i++){
    for(j = 0; j < COLS; j++){
      fscanf(scoreFileptr, "%d", &filler);
      if(filler == 999){
         break;
      }
      scoresArray[i][j] = filler;

    }
  }
}

这是你的一段代码,我已经修改了一些,它正在工作(用 gcc 编译)。希望它可以提供帮助。如您所见,由于您的函数是无效的,因此我传递了 2D 的地址。

【讨论】:

    猜你喜欢
    • 2021-07-18
    • 2013-04-24
    • 1970-01-01
    • 1970-01-01
    • 2021-07-03
    • 2011-07-18
    • 1970-01-01
    • 2015-06-19
    • 2013-02-22
    相关资源
    最近更新 更多