EDIT: Clarifying.
- fout is is a FILE*. (I thought this was irrelevant since that line clearly compiles)
- there is A LOT of code above these last few lines; I guess I could dump them all but I imagine you’re not overly interested in debugging my stuff. I’m more interested, generally, in what could possibly occur that would segfault at return 0 but not before.
Warning: My C is terrible.
I’ve got a C program which, from the likes of it, just wants to segfault. I’ll spare you the other, irrelevant details, but here’s the big picture:
My code:
//...other code
printf("finished \n");
fclose(fout);
printf("after fclose \n");
return 0;
The output:
finished
after fclose
Segmentation fault
I’m compiling with GCC, -std=c99.
My question:
How the heck is this even possible? What should I be looking at, that may be causing this (seemingly random) segfault? Any ideas?
Much thanks!
Whatever the
returnis going back to is causing the fault. If this code snippet is inmain(), then the code has inflicted damage to the stack, most likely by exceeding the bounds of a variable. For exampleThis sort of thing could cause any of a number of inexplicable symptoms, including a segfault.