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

by Vivek Gite · 6 comments

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.

Featured Articles:

Want to read Linux tips and tricks, but don't have time to check our blog everyday? Subscribe to our daily email newsletter to make sure you don't miss a single tip/tricks. Subscribe to our weekly newsletter here!

{ 6 comments… read them below or add one }

1 Anthonus de Silva 04.04.09 at 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.

2 Felix Mwango Mutale 04.14.09 at 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

3 Anup 07.20.09 at 1:15 pm

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

4 Ruwinda Fernando 11.15.09 at 3:48 pm

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

Good Luck !!

5 mannex 11.30.09 at 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.

6 satya 01.02.10 at 10:00 am

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

Leave a Comment

You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Previous post:

Next post: