What is a segmentation fault in C?
A segmentation fault (segfault) happens when a program reads or writes memory that it should not touch. The operating system stops the program.
Common causes:
- Dereferencing a NULL or uninitialized pointer.
- Accessing an array outside its limits.
- Using memory after
free(). - Very deep recursion (stack overflow).
- Writing to a string literal, like
char *s = "abc"; s[0] = 'x';.
To find it, compile with -g and run with a debugger like GDB, or use -fsanitize=address to see the exact line.
Learn more about the similar topics:
| Tutorials |
|---|
| No Content Found. |
| Exercises & Assignments |
|---|
| No Content Found. |
| Interview Questions & Answers |
|---|
| No Content Found. |