EDIT: Clarifying.
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:
//...other code
printf("finished \n");
fclose(fout);
printf("after fclose \n");
return 0;
finished
after fclose
Segmentation fault
I'm compiling with GCC, -std=c99.
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 return is going back to is causing the fault. If this code snippet is in main(), then the code has inflicted damage to the stack, most likely by exceeding the bounds of a variable. For example
int main ()
{
int a [3];
int j;
for (j = 0; j < 10; ++j)
a [j] = 0;
return 0;
}
This sort of thing could cause any of a number of inexplicable symptoms, including a segfault.
Since it's probably a stack corruption related problem, you could also use a memory debugger to locate the source of the corruption, like valgrind.
Just compile using gcc -g and then run valgrind yourprog args.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With