≡ Menu

Why Does The Segmentation Fault Occur on Linux / UNIX Systems?

According to wikipedia:

A segmentation fault occurs when a program attempts to access a memory location that it is not allowed to access, or attempts to access a memory location in a way that is not allowed (for example, attempting to write to a read-only location, or to overwrite part of the operating system).

Usually signal #11 (SIGSEGV) set, which is defined in the header file signal.h file. The default action for a program upon receiving SIGSEGV is abnormal termination. This action will end the process, but may generate a core file (also known as core dump) to aid debugging, or perform some other platform-dependent action. A core dump is the recorded state of the working memory of a computer program at a specific time, generally when the program has terminated abnormally.

Segmentation fault can also occur under following circumstances:

a) A buggy program / command, which can be only fixed by applying patch.

b) It can also appear when you try to access an array beyond the end of an array under C programming.

c) Inside a chrooted jail this can occur when critical shared libs, config file or /dev/ entry missing.

d) Sometime hardware or faulty memory or driver can also create problem.

e) Maintain suggested environment for all computer equipment (overheating can also generate this problem).

Suggestions to debug Segmentation Fault errors

To debug this kind of error try one or all of the following techniques :

  • Use gdb to track exact source of problem.
  • Make sure correct hardware installed and configured.
  • Always apply all patches and use updated system.
  • Make sure all dependencies installed inside jail.
  • Turn on core dumping for supported services such as Apache.
  • Use strace which is a useful diagnostic, instructional, and debugging tool.
  • Google and find out if there is a solution to problem.
  • Fix your C program for logical errors such as pointer, null pointer, arrays and so on.
  • Analyze core dump file generated by your system using gdb

Further readings:

Please add your suggestions and debugging techniques in the comment below.

Tweet itFacebook itGoogle+ itPDF itFound an error/typo on this page?

Comments on this entry are closed.

  • Anthonus de Silva April 4, 2009, 4:50 pm

    Suddenly my software started giving a Segmentation Fault.

    Language C, O/S: Linux.

    Following is a (pseudo) code snippet:

    1. x=strtok(y,”:”);
    2. if(strlen(x)>0) { do_something; }

    Line 2 would cause a Seg Fault. I would guess it should at least give me a false result.

    Hence I changed the code to:

    2. if(x != NULL) if(strlen(x) > 0) { do_something; }

    Note: you cant do if(x != NULL && strlen(x) > 0) as the strlen woud cause a SegFault (GP fault in Windows, i guess).

    Regards.

  • Felix Mwango Mutale April 14, 2009, 8:09 am

    Hello All Forum members,

    I am in dire need of help please. Can some one help me solve this segmentation problem ?

    I have part of the code like this. I have tried to put displays around to track down the flow as follows:

    LET GetInput = TRUE
    WHILE GetInput
    display ” Inside infield manno 1 ”
    display “GetInput = “, GetInput
    sleep 5
    INPUT w_manno,
    w_surname
    WITHOUT DEFAULTS
    FROM manno,
    surname

    AFTER FIELD manno
    display ” Inside infield manno 2 ”
    sleep 5
    IF w_manno ” ”
    AND w_manno IS NOT NULL
    THEN
    SELECT COUNT(*)
    INTO w_cnt

    The second display is not coming. I can’t put a display just after the INPUT command or as I am getting a syntax error.

    I have also tried the following:

    LET GetInput = TRUE
    WHILE GetInput
    display ” Inside infield manno 1 ”
    display “GetInput = “, GetInput
    sleep 5
    INPUT BY NAME manno,
    surname
    WITHOUT DEFAULTS

    But still I don’t seem to get what is going on. How do control the cursor to placed in the input field ?

    Thanking you in advance.

    Felix Mwango Mutale

  • Anup July 20, 2009, 1:15 pm

    Segmentation Fault may also occur if disk is running out of free space!

  • Ruwinda Fernando November 15, 2009, 3:48 pm

    Invoking “ps gave me “segmentation fault”. Managed to got rid-off the problem by upgrading “procps”.
    More info nixtaste

    Good Luck !!

  • mannex November 30, 2009, 6:54 pm

    I’m having a strange behavior with an application, it segfaulted before every time while running it after the first error which happened after I closed it (after first use)
    But then I by accident missed the – while trying to run it in debugging mode. So it kinda looks like this “./nameapp d” and that puzzles me, any insight or guess what is going on there? What difference does that make?

    Cause after doing this it suddently worked again without error where as it segfaulted every single time before that. Could be random behaviour but others with the same problem seem to say it fixed it for them too.

  • satya January 2, 2010, 10:00 am

    hiii ……..my program works fine in turboc2 but it gives segmentation fualt in linux through putty………what should i do

  • Yuri March 30, 2010, 9:55 pm

    9 out of 10:

    You screwed up your printf like statement: the number of your print format parameters is greater than the number of arguments you pass. Like this

    printf (” %d %s %s \n”, string1, string2); Oops, you forgot to pass in your number which was supposed to be the first.

  • Narendra March 3, 2011, 7:37 am

    hi,
    while make function i got following errors
    ./common/ptypes2tcl > gen/ptypes.cc
    /bin/sh: line 1: 15769 Segmentation fault ./common/ptypes2tcl > gen/ptypes.cc
    make: *** [gen/ptypes.cc] Error 139
    [root@localhost ns-2.33]#
    can you tell me how to solve this

    thanks in advance

  • sayali March 21, 2012, 8:16 am

    hello,
    while doing make for ns-2.34
    ./common/ptypes2tcl > gen/ptypes.cc
    /bin/sh: line 1: 15769 Segmentation fault ./common/ptypes2tcl > gen/ptypes.cc
    make: *** [gen/ptypes.cc] Error 139
    [root@localhost ns-2.33]#
    can you tell me how to solve this…

    thanks

  • wwo March 19, 2013, 10:21 am

    hello,
    while doing make for ns-2.34
    ./common/ptypes2tcl > gen/ptypes.cc
    /bin/sh: line 1: 15769 Segmentation fault ./common/ptypes2tcl > gen/ptypes.cc
    make: *** [gen/ptypes.cc] Error 139
    [root@localhost ns-2.33]#
    can you tell me how to solve this…

    thanks

  • sathyaraj May 28, 2013, 1:13 pm

    Hi All,
    Today I have faced the segmentation fault in the Linux servers
    I have tried to remove the mod_nss rpm from the server once it started to ask the dependency library from the server I have removed the dependency library from the server once the depency library removed I was unable to run any of the command and getting the error as “segmentation fault”
    Once rebooted the server with the Linux iso I have upgraded the installation once it has been done the server comes to normal one
    Regards,
    Sathyaraj.S

  • Gaurav Garg June 18, 2013, 7:12 am

    Hey i am getting this error >> Segmentation fault (core dumped)
    but this error comes at the time of runing the program not at the time of compilation.. all dependencies are installed so can you guide me how can fix it…?