0
我有一個函數,我以前在另一個叫做findVertexNumber的程序中使用過。功能不能正常工作,GDB沒有發現錯誤
在我的新程序中,它不適用於我。
也許我一直在盯着代碼太久才發現錯誤,但是當我比較這兩個程序時,它在執行過程中看起來與我完全相同。
希望有人能發現我的錯誤....
#include <stdio.h>
#include <stdlib.h>
int findVertexNumber(FILE *fp);
int main(int argc, char *argv[])
{
if(argc != 2) //insure 2 arguments given, one for a.out and one for the test file
{
printf("Requires 2 arguemnts. Be sure to include test file location\n"); //result if request fails
return 0;
}
FILE *fp; //variable declartion
int numberVertices = 0;
int i, j;
fp = fopen(argv[1], "r"); //open the file provided
numberVertices = findVertexNumber(fp); //find the number of vertices in the graph
fclose(fp);
printf("Max vertex number is: %d\n", numberVertices);
}
int findVertexNumber(FILE *fp) //function finds the largest number in the file, assumed to be number of
{ //vertices
int max = 0;
int e1, e2;
while(fscanf(fp, "%d %d", &e1, &e2) != EOF) //continue to end of file
{
if(e1 > max) //check if e1 is greater than max
{
max = e1; //if true, update max
}
if(e2 > max) //check if e2 is greater than max
{
max = e2; //if true update max
}
}
fclose(fp); //close file
return max; //return max number, number of vertices
}
文件其讀數是一個文本文件,也許有一些錯誤在它的格式?
這就是:
1 3
1 4
2 3
2 4
5 7
5 8
6 8
6 9
錯誤來自while
聲明,據我可以通過評論告訴調試是:
*** glibc detected *** ./a.out: double free or corruption (top): 0x0000000000ee3010 ***