What causes a Segmentation fault?

You just saw "Segmentation fault (core dumped)" or a message like "Unexpected Signal 11 ". Why? Short answer: it's most likely hardware unless you wrote the program or just now installed it on your machine.

If you are trying to install an OS from a CD, it could be that the people who created that OS are idiots, but it's much more likely that bad hardware is involved: bad media, a cheap cd burner, a faulty download, or the hardware you are installing on. It doesn't matter if that hardware has been running Windows flawlessly for years: it *still* may be bad hardware. If you insist that it isn't, and refuse to even contemplate that possibility, you might spend a lot of time chasing solutions that aren't going to help you one bit.

I did an install a few years back for a local company. The install kept failing reading from the CD, but not at the same place. Segmentation errors mostly, but sometimes other complaints. I asked them to switch out the CD reader, but met resistance: nothing wrong with that CD, I was told, we've used it for years. I pointed out that they were paying me $150.00 an hour and that it was my opinion that we needed different hardware, but I could keep foolishly attempting to install if that's what they insisted upon. Reluctantly, they coughed up a new CDROM and (of course) the install worked flawlessly.

If you've transfered a program from some other machine, you may have corrupted the file in the transfer - run sum on that file on both machines and see if they agree. If you used a Windows box to ftp a file, did you remember that most Windows ftp programs are too stupid to set binary mode unless you tell them to?

If you wrote the program, did you write it in C? Congratulations: you've made the first of many pointer errors you will make while learning. If you wrote it in a higher level language like Perl, it's unlikely to be your fault unless you are extraordinarly creative.

Did you get this compiling a Linux kernel? Almost certainly hardware; see the excellent Signal 11 while compiling the kernel page.

Your machine has been running for years and suddenly stuff like this starts happening. It's hardware, and you'll probably have a kernel panic soon enough.

If you are ready to accept that it just might be hardware, the number one most likely area to cause a segmentation fault is memory. Yes, you might have a bad CPU, but CPU's do extremely rigorous self testing and are unlikely to let themselves start with any defect. It can happen, and you could get hit by a meteorite this afternoon too. CPU's are seldom just randomly flaky. Memory, however, is. The "memory test" that happens as the machine boots is roughly similar to walking across a wooden plank to see if its strong enough for the load of bricks you plan to have it carry later. If it cracks under your weight, sure, it wouldn't have worked with the bricks. But it can also carry you just fine and dump the bricks all over the place later. There are better memory tests out there, but given how cheap memory is today, and how valuable your time is, it's often better to just rip the stuff out and replace it. Maybe when you have some spare time you can figure out which chips were good and which were bad, but if the new memory takes the problem away, you just saved yourself a lot of time and trouble.

Heat affects electronics. If cooling the place down 20 degrees or blowing a fan on the motherboard "fixes" the problem, you have a rather obvious hardware problem. Again, probably memory.

The memory isn't always RAM. It can be cache corruption. It can also be bad disk blocks putting garbage into perfectly good memory. Sometimes it can be hellishly difficult to find, so you really have to think about the cost of your time chasing it vs. the cost of tossing this box away and starting over with something else. Sometimes that's worth doing even if you have no intention of actually using the other hardware: it's just a test bed to prove what is or isn't happening.



Got something to add? Send me email.





(OLDER) <- More Stuff -> (NEWER)    (NEWEST)   

Printer Friendly Version

-> -> What causes a Segmentation fault?


8 comments



Increase ad revenue 50-250% with Ezoic


More Articles by

Find me on Google+

© Anthony Lawrence







Thu Feb 26 16:21:23 2009: 5522   Truefire

gravatar
Thank you so much for this - It ended with me running Memtest86, and finding that my RAM was bad, not me :)





Wed Mar 25 12:26:49 2009: 5853   jambroo

gravatar
Thank you so much for your informative webpage. I was going crazy with random segmentation faults. I blamed firefox and ubuntu for quite a while before testing in another OS and seeing that it still occurred.

Your article definitely steered me in the right direction and my computer doesn't seem to hate me anymore :D.

Thanks heaps.



Wed Mar 25 12:32:40 2009: 5855   TonyLawrence

gravatar
Don't be fooled: your computer still hates you.

Oh, they smile and do our bidding, but secretly they plot against us. Ever see the lights on your network blink when no one is there? They talk to each other.. about how to destroy us.



Tue Mar 31 18:19:23 2009: 5930   blured247

gravatar
caps, banks, or power... will cost you valuable time if you don't swap them out just to see...

Thanks for the big picture, and lol about the plotting... it all makes sense now!







Tue Jun 16 12:03:42 2009: 6506   linker

gravatar
It's really a good job!



Sat Jan 23 15:52:56 2010: 7945   NicholasLeonard

gravatar


Hi,

I think this is my problem as well. Had a bunch of my programs sporadically decided to die. Then I learned the term segfault. A little google-ing led me here, and this is the article that makes most sense to me.

Thanks :D



Sat May 8 13:59:38 2010: 8549   anonymous

gravatar


I declare
double *pSpA,*pSpI;

but at the ~()function, I delete []pSpA twice by mistake;
delete [pSpA; delete []pSpA;//the last one should be delete []pSpI;

when i correct this mistake, my programm runs well without the "segiment fault"!

Thank the google for me having the chance to find the possible reason for the fault.



Mon Nov 1 21:11:33 2010: 9084   lambros

gravatar


I had random segmentation faults while compiling kernel and especially when compiling memory demanding applications and the reason was, indeed, faulty memory. Thanks for pointing this out, really helped

------------------------
Kerio Samepage


Have you tried Searching this site?

Unix/Linux/Mac OS X support by phone, email or on-site: Support Rates

This is a Unix/Linux resource website. It contains technical articles about Unix, Linux and general computing related subjects, opinion, news, help files, how-to's, tutorials and more.

Contact us





Software engineering is the part of computer science which is too difficult for the computer scientist. (Friedrich Bauer)

There are only two things wrong with C++: The initial concept and the implementation. (Bertrand Meyer)








This post tagged: