This is a followup to How can we improve "segmentation fault" questions?.
We are having a lot of questions about segmentation fault in C and C++. Some examples within 12 hours of this post are:
Why do I get a segmentation fault in this code with a signal handler?, BMP transformation seg fault - alloc or something else, and Segmentation fault in function that reads file and with threads. I am quite tired of seeing all these somewhat duplicate, but not so quite duplicate, posts again and again, as most problems can be solved by the following procedure:
- Read the code carefully (98% of problems ends here)
- Run Valgrind (or whatever memory checkers you like) (99.8% of problems ends here)
- Get a core dump and do a backtrace
It appears that people either do not know how to debug or just don't want to; and just dump whatever code that is segfaulting onto Stack Overflow. I suggest that we get a megathread about these segmentation fault questions, how to solve them, and restrict the dumping of code to Stack Overflow to use the community as an AI debugger, for the following reasons:
- A question about the issue of a specific piece of code is generally not useful for others, as they are too specific (this is especially true for segfault type of issues)
- Stack Overflow is a not free debugging service or AI debugger
- There is really no sense to waste everyone's time reading 200+ lines of code only to find out one missed a
*
when operating a pointer - The answer to most of these questions are going to be the same (read, Valgrind, core dump)
- Otherwise there will be one day when the error message of segmentation fault becomes
Segmentation fault (code dumped to Stack Overflow)
.
Note: in case one does not notice, the default segfault error message is Segmentation fault (core dumped)
.