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

Posted on in Categories C Programming, CentOS, Debian Linux, fedora linux, GNU/Open source, Hardware, Linux, Linux distribution, RedHat/Fedora Linux, Sys admin, Troubleshooting, Ubuntu Linux, UNIX last updated September 12, 2008

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.

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin and a trainer for the Linux operating system/Unix shell scripting. He has worked with global clients and in various industries, including IT, education, defense and space research, and the nonprofit sector. Follow him on Twitter, Facebook, Google+.

13 comment

  1. 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.

  2. 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

  3. 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.

  4. 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.

  5. 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
    [[email protected] ns-2.33]#
    can you tell me how to solve this

    thanks in advance

  6. 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

  7. 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…?

  8. when i run the command ” apt-get update” in kali linux 2.0 and it gives the error
    “Segmentation fault
    Reading package lists… Done ”

    how can i solve this error
    please tell me

Leave a Comment