Question

I have been looking around the internet and SO, and am trying to find how to detect and fix segfaults. I tend to get this error a lot, and all I could find on google and SO was it is a memory issue(This answer helped a lot: https://stackoverflow.com/a/3200536/3334282). I want to know what the poor programming is that causes it so that I can avoid it.

My most recent example was trying to learn how to use fgets().

FILE *text;
char array[100];

fopen("text.txt", "r");
fgets(array, 100, text);

printf("%s\n", array);

fclose(text);

This returns 8181 segmentation fault (core dumped).

Was it helpful?

Solution

In this case it's very obvious: Where do you initialize text?

Uninitialized local variables have an indeterminate value, and using uninitialized local variables leads to undefined behavior which can lead to crashes.


For the more generic question as phrased in the question title, that's harder because quite a few things can cause segmentation faults. Using uninitialized pointers may cause it, using NULL pointers most definitely will cause a crash. Writing outside of bounds for allocated memory (like writing out of bounds for arrays), can overwrite other data making other pointers change their values unexpectedly and that can lead to a crash as well.

In short, using pointers can lead to segmentation faults if used improperly.

However, many cases of undefined behavior, like the one in your code, can be avoided by enabling more warnings (like e.g. -Wall flag to GCC) and fixing those warnings. While warnings are technically not errors, they are often an indicator of you doing something suspicious. Again with your code as example, by adding -Wall to your GCC command line, you would have gotten a warning about using the uninitialized variable text.

OTHER TIPS

Segmentation fault occurs when you try to use the memory which is not assigned to your process by OS. some common occurrence is when you when you use pointer.

when you try to free a memory which is not assigned to you (when pointer has garbage value) like your case text is uninitialized and your trying to close it.

when you try to free a memory already freed.

Even if you had shown source code from a language that uses dynamic allocation, like Perl or Python, the variable text has not been assigned a value. So, you have the same problem across most languages. A way to prevent a problem like this is to examine variables.

You can do this by printing them with printf or by using a debugger. In your case, printf-ing text would show a numeric value that wouldn't make sense to you, and you'd get the segfault when you executed the open.

You're getting a segfault, because your program is trying to access privileged memory; at least it is privileged to your process running the program. If you assigned text to the return value from your fopen statement

text = fopen("text.txt", "r");

then text would either be NULL or would contain the file handle of text.txt. It is because you are just using text's random value that you are getting the segfault.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top