31

In Ubuntu I have faced the segmentation fault error many times. What is a segmentation fault and when does it occur?

RobotHumans
  • 29,530
Tachyons
  • 17,281
  • 1
    Just to cover it up: I had a similar problem, whereas my segfaults were not reliably reproduceable and they came from (almost) random applications. Found out that most likely my memory is broken. So if quite any program causes segfaults, one might have a closer look at the RAM. –  Mar 16 '13 at 23:21

3 Answers3

29

An error saying segmentation fault (or segfault, or SIGSEGV) in Ubuntu and other Unix-like operating systems, or saying general protection fault in Windows, is when a program attempts to access a part of memory that cannot be accessed, or which the program is prohibited from accessing. A segmentation fault is a kind of program crash, that is, an abnormal termination of a program. See the Wikipedia articles on crashes, memory protection, segmentation fault, general protection fault, and SIGSEGV for more information (and a more textured understanding of the topic than is presented here).

A segmentation fault is almost always due to a bug in the program where it occurs. I am guessing most or all of your segmentation faults are happening from the same application. Please provide more details about the circumstances under which segmentation faults are happening on your machine, and what program is crashing. Please also provide the full and exact text of the error message you're receiving and any other messages that appear before it. This should make it possible for us to provide detailed advice specific to your problem (rather than just general information about what a segmentation fault is).

The best way for you to provide this information is for you to edit your question to include it. Alternatively, if you want this question to be just about segmentation faults in general, you could post a new question to ask about what specifically is causing your segmentation faults (if you do this, make sure to provide all these details in your new question).

Eliah Kagan
  • 117,780
  • fortunately all the segmentation fault are fixed by upgrading os :) . My question is about segmentation fault in general .Not for a particular app . – Tachyons May 17 '12 at 14:16
  • @I'mnotthisperson I've edited my answer accordingly. (I'm retaining the directions for providing more information, for the benefit of other users experiencing segmentation faults, even though I know you don't need specific assistance with them right now.) – Eliah Kagan May 17 '12 at 14:18
  • Well this is about a segmentation fault in an app. I wanted to ask about it occurring in a C++ program where as the same program runs perfectly fine in Windows or Turbo C++(in wine)(i use anjuta in Linux).And well these are just basic programs and not applications(Programs like TREES,GRAPHS,etc) – Nirmik May 17 '12 at 14:22
  • Thanks .But I am afraid in most of the cases asking question about segmentation fault in app is offtopic here. :) – Tachyons May 17 '12 at 14:26
  • @Nirmik : probably it is the topic for stackoverflow :) – Tachyons May 17 '12 at 14:28
  • @Nirmik Actually, I think that would be on-topic here. Normally questions about problems in programs you are writing should be asked at StackOverflow rather than AskUbuntu, but since you have something that is Ubuntu-specific, you can go ahead and post a question about it. Just make sure it's clear in your question how this is an Ubuntu-specific problem. It might end up getting migrated to StackOverflow, but probably not. You can post a link to your new question here, in a comment. BTW, pretty much any program can have a segmentation fault, not just "applications." – Eliah Kagan May 17 '12 at 14:29
  • @Eliah Kagan Thanx for considering it.Actually i know its not a question to be asked here completely i just thought you beeing an advanced user might be knowing it. Thanx..I'll post it as a new question and go the way you have suggested :) – Nirmik May 17 '12 at 14:34
  • @Nirmik I do code in C and C++, so it's possible I'd be able to help, but I'd have to see the code. Whether you post here, at StackOverflow, or somewhere else, if you leave a comment here with a link to your post I'll take a look at it. And make sure to include the code! (You should include a reasonably simple complete program that can be compiled successfully on all the platforms you're using and produces the problem as you've described it.) – Eliah Kagan May 17 '12 at 14:35
  • Okay i'll do it asap. Unfortunately i do not have that code with me right now(Lost it in the 12.04 upgrade :p) But i'll post one soon.Thanx :) – Nirmik May 17 '12 at 14:37
4

Segmentation fault is caused by a bug in the application. Technically it means that application try to read or write to part of memory that doesn't belongs to it (or doesn't exist). It's of course forbidden to read or write to somebody else's memory and when system (kernel) detects this, it will force the application to quit.

0

Gone are the days when people used to trace through assembly code and debug a problem. Abends, Dr Watson, Segmentation fault. Those green days are gone.

One of the reasons for a segmentation fault is when code that has direct access to memory fails. When a piece of code tries to access the memory segment of a different application, segmentation fault occurs. Memory allocations are some times moved around to allow larger contiguous blocks of memory to get allocated. While it tries to recover, the kernel tries hard to save all its memory information to a file, current state of all applications running on the cpu and their state (last instruction run) to a file and just die. It will also try to store as much recovery information, and close as many files as it can, so that hdd's dont get broken links.

Debug it and fix it if you can reproduce it often. If you cannot reproduce it, just join your hands, kneel down and pray like hell that you don't see it "regularly".

Siddharth
  • 331
  • Segfaults can be triggered in any program written in a language that allows direct memory access. They will not generally crash the kernel either. – Alex L. Jan 18 '13 at 06:05
  • @AlexL. that is accurate. I'll edit my answer. – Siddharth Jan 18 '13 at 08:07
  • I hope I have represented my thoughts correctly. – Siddharth Jan 18 '13 at 08:10
  • @AlexL. Can you please review it one more time. I'll delete it, if its still not accurate. – Siddharth Feb 06 '13 at 07:32
  • This is better. Still, (1) although possible it is extremely rare for a program to set the SIGSEGV handler to anything other than SIG_DFL (which is dump the core and exit for a SIGSEGV) since if you are accessing memory that you shouldn't be then something has gone very wrong and recovery is probably impossible. (2) A core will be dumped only once the process has been stopped. (3) The core dump only includes the memory and registers of the process that was dumped. (4) Other processes, the CPU and the kernal are not affected by a segfault of another process. – Alex L. Feb 06 '13 at 21:44
  • Ok :). To ensure that SO is kept clean and good. I'll delete my answer. Please be sure to add your points on a answer. Dont want to loose them. – Siddharth Feb 07 '13 at 03:02