{ }codelper

Error Messages › C

C Segmentation fault (core dumped)

Segmentation fault (core dumped)

It means your program tried to read or write memory that is not its own, so the operating system forced it to stop. A Linux terminal shows Segmentation fault (core dumped) and this site shows [SIGSEGV] process killed by signal SIGSEGV, and they are the same thing (SIGSEGV is the name of the signal the operating system sends when this happens). It is not a compile error but one that happens while the program runs, so it does not tell you which line. That is why it is quickest to go through the lines that use scanf and pointers, one by one.

Cause 1. You left out & in scanf

This is the most common cause. scanf takes the address of the place to store the value it reads. If you pass n without &, it treats the value in n (0 here) as an address, tries to write there, and stops.

The compiler tells you about this mistake in advance. It is the warning above the [SIGSEGV] line in the output below. A warning does not stop compilation, so it is easy to skip past, but if you get a segfault, read the warnings first.

Wrong code

#include <stdio.h> int main(void) {    int n = 0;    scanf("%d", n);    printf("%d\n", n * 2);    return 0;}
Input
21
When you run it
main.c: In function ‘main’:
main.c:5:13: warning: format ‘%d’ expects argument of type ‘int *’, but argument 2 has type ‘int’ [-Wformat=]
    5 |     scanf("%d", n);
      |            ~^   ~
      |             |   |
      |             |   int
      |             int *
[SIGSEGV] process killed by signal SIGSEGV

Edit and run it →

Fixed code

Put & in front of the variable name to pass its address.

#include <stdio.h> int main(void) {    int n = 0;    scanf("%d", &n);    printf("%d\n", n * 2);    return 0;}
Input
21
Output
42

Edit and run it →

If you had not given n a starting value, the garbage value in it would be used as the address. In that case the program may stop, or it may keep going and damage some unrelated place.

When you read into an array such as char name[20]; with %s, you don't add &. The array name is already the address of its first element.

Cause 2. You used * on a NULL pointer

NULL is a mark that means "points to nothing". If you read or write a value through such a pointer with *p, the operating system blocks it. Either you set the pointer to NULL and forgot to give it something to point to, or a function failed and returned NULL and you used it without checking.

Wrong code

#include <stdio.h> int main(void) {    int *p = NULL;    *p = 5;    printf("%d\n", *p);    return 0;}
When you run it
[SIGSEGV] process killed by signal SIGSEGV

Edit and run it →

Fixed code

Store the address of a real variable in the pointer before you use it. For a pointer that might be NULL, check with if (p != NULL) before using it.

#include <stdio.h> int main(void) {    int x = 0;    int *p = &x;    if (p != NULL) {        *p = 5;    }    printf("%d\n", x);    return 0;}
Output
5

Edit and run it →

Functions that return a pointer, such as malloc, fopen and strchr, return NULL when they fail or find nothing. Don't use the returned value right away. Check it first.

Cause 3. You tried to change a string literal

The "hello" in char *s = "hello"; is placed in a read-only area. s only points to that area, so if you try to change a character, as in s[0] = 'H', the program stops. There is no warning at compile time either, so it is hard to find.

Wrong code

#include <stdio.h> int main(void) {    char *s = "hello";    s[0] = 'H';    printf("%s\n", s);    return 0;}
When you run it
[SIGSEGV] process killed by signal SIGSEGV

Edit and run it →

Fixed code

Make a string you want to change an array. char s[] = "hello"; copies the characters into your own array, so you can change them freely.

#include <stdio.h> int main(void) {    char s[] = "hello";    s[0] = 'H';    printf("%s\n", s);    return 0;}
Output
Hello

Edit and run it →

What to check when you see this error

  1. First see whether there were any compile warnings. A missing & in scanf is reported in advance by the format ... expects argument of type warning.
  2. Check that every scanf has & in front of the variable (leave it off only when reading a character array with %s).
  3. For every line that uses * or [ ] on a pointer, answer where that pointer points right now. If the answer is "nowhere yet" or "it might be NULL", that line is the culprit.
  4. Check that you are not reading a string into a pointer that has no room for characters, as in char *name; scanf("%s", name);. You need an array such as char name[20];.
  5. Check that each array index is between 0 and size - 1. If it goes out of range, the program may stop with this error, or it may keep running and just give wrong values.
  6. When you add a printf to see how far the program got, put fflush(stdout); right after it. Without it, text printed before the crash may be lost and never reach the screen.
  7. A recursive function with no ending condition also causes this error by using up the stack. If you used recursion, check that there is a stopping condition (if (n == 0) return ...).

Practice problems

See also