-
Seg fault problem
When I run this program I get a seg fault when ever I run it and it happen on the last pass through for loop. I put in print statements at one point just to see where the errors were happening and it would go all the way through the for loop and when i=25 it would seg fault at the first assignment. The main thing that confuses me is that it does this no matter what I make the second argument for the for loop.
Code:
int main(int argc, char* argv[])
{
struct LIST map[26];
int i;
for(i=0;i<26;i++){
map[i].startingPoint=' ';
map[i].first=NULL;
}
if(argc>1){
fillStruct(argv[1],map);
printList();
depthSearch();
breadth();
topoSort();
}
else
printf("No file to print");
return 0;
}
int main(int argc, char* argv[])
{
struct LIST map[26];
int i;
for(i=0;i<10;i++){
printf("%d \n",i);
map[i].startingPoint=' ';
printf("%d set to blank",i);
map[i].first=NULL;
printf("%d set to NULL",i);
}
if(argc>1){
printf("Send to fillstruct");
fillStruct(argv[1],map);
printList();
depthSearch();
breadth();
topoSort();
}
else
printf("No file to print");
return 0;
}
Any help will be greatly appreciated.
-
You won't know how far you get unless you fflush(stdout) after every call to printf. I believe it to be far more likely that it's dying on one of the calls inside the if(argc>1) block.
-
So use a debugger.
gdb a.out
// some output from GDB
run filename.txt
// some output from program, plus a segfault
bt
// find out where exactly you got to.
From that point, you can examine variables to see what is wrong (typically a pointer containing NULL or garbage).