我的C程序在Dev C ++中运行但在另一个编译器

时间:2016-07-24 18:15:09

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

所以我昨晚在Dev C ++中完成了我的最新任务,这就是我们创建它所需要的。让它完美无缺,编译精彩,运行良好,一切都很好。但是,我们必须通过' MobaXTerm'并通过VPN,使用我们用于评分的大学系统。我试图运行它,我收到一个错误,'分段错误'。这是我第一次听到这个错误,所以我研究了它。在大多数情况下,它会将错误的变量放入“循环”中。但我检查了我的循环50次,它的工作原理。我认为它与我的2D数组有关,可能超出范围,但我调试了,从我所知道的,它并没有逃脱我为它分配的大小。

我并不只是想将我的整个程序粘贴到这里,但我会给出几个片段,也许有人可能会试着告诉我可能是什么问题。我真的不明白它是如何在一个而不是另一个中运行良好的。

#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 ++编译器中运行?我很难过。

编辑:问题似乎出现在&#39; makeArray&#39;功能。我在main中取出了它的函数调用,它现在运行。只是想弄清楚导致它的原因。

编辑2:逐行完成功能后,他们的问题似乎是&#39; fscanf&#39; makeArray函数中的行。谢谢大家指出我正确的方向!

2 个答案:

答案 0 :(得分:2)

由于整个代码不可用,我会告诉你原因和要找的内容,但我无法提供确切的答案。

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

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

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

我访问数组a的已分配区域之外的元素。根据标准,这是未定义的行为,因此没有关于可能发生的事情的暗示。在实践中,通常有2个结果:它可以运行得很好,或者它可以产生分段错误,具体取决于编译器。

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

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

使用Dev C ++编译的代码恰好可以运行此错误。请注意,这绝不是一件好事 - 应该不惜一切代价避免未定义的行为。

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

P.S。另外,请确保输入文件可用。

答案 1 :(得分:0)

#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的地址。