How to debug why my C code causes a segmentation fault? [closed]

1) You check feof() first, then do a fscanf(), and then don’t check its return value (or re-check feof() at the very least. (You might have been right at the end of the file prior to the fscanf() call, or the configuration file might be malformed, but you wouldn’t detect this with your code.)

2) Your index range checks (and assorted realloc()s) look dodgy. But there is absolutely no chance I’ll be doing a runtime analysis of your code, especially since I don’t have an input file example to go with.

Do some Machete Debugging

Edit: After joop’s comment pointed me toward the fine print of your realloc() (and the if statement around it), in absence of a comment explaining how exactly you expect this to work out, I’ll say that you are invoking undefined behaviour there.

Leave a Comment