Click here to Skip to main content
15,672,471 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
I wrote a code in linux and after removing all the errors, I meet the segmentation fault message, I don't understand what it says. How can I remove this error?
please suggest a quick and effective way.
Updated 16-May-11 22:28pm
Richard MacCutchan 17-May-11 4:30am    
I guess you didn't remove all the errors.
Stefan_Lang 17-May-11 5:53am    
If you can't make sense ogf the message, and you don't post it, we cannot either.

Please give us the text of the message. Maybe we can help you interpret it.
Sergey Alexandrovich Kryukov 17-May-11 16:37pm    
Not until the code with the bug is also shown :-)
Stefan_Lang 18-May-11 4:06am    
True, but often the message hints at where to look for the faulty code. He (or she?) can't possibly show the faulty code as long as it's unclear what is causing it - it could be anywhere. So the first step is take a sharp look at the message and see if we can narrow down the scope of the code that may cause the error.
Legor 17-May-11 7:08am    
Your accessing invalid memory during the runtime of your program.
Without looking at the code nobody could ever give you a hint about the solution.

A quick and effective way:
Run your code through a debugger.
Share this answer
Sergey Alexandrovich Kryukov 17-May-11 16:38pm    
Agree, my 5.
Niklas L 18-May-11 3:09am    
Thanks. Sometimes I get the impression that they don't cover debugging techniques at programming courses at all. Some might learn programming on their own, but there are clearly a lot of students asking basic questions here, which could have been answered by a debugger. I'm not saying this one is such though.
Sergey Alexandrovich Kryukov 18-May-11 3:20am    
Who cares about courses. I never had one, and debugged from my first days. I knew companies who has an unwritten rule about not considering candidates with "programming" or "software diploma" -- for a good reason.
Sergey Alexandrovich Kryukov 18-May-11 3:23am    
"I'm not saying this one is such though."
Hm. It's something... well, I would say looking at this problem without debugger would be stupid, at least when the first inspection of the code did not give a clue...
Niklas L 18-May-11 3:51am    
I was referring to the OP taking a course or not. And yes, it would be stupid, but only if you knew there was something called a debugger.
Both Niklas and Harish are correct. The former named the tool to narrow down where the error could be, the latter gives you a hunch what to look for once you've located a potentially problematic piece of code.

The error message you got may also be helpful in narrowing down the whereabouts of the error, but that is harder, and often not that helpful. And of course, you didn't yet tell us what it said.

What I usually do in such a case, is
1. look at the error
2. realize it isn't helping (most of the time)
3. Consider the steps I've performed that led to the error
4. Find a piece of code that I know has been executed shortly before the error occured
5. Set a breakpoint there
6. Run the program in debug mode, performing the same steps, until I hit the breakpoint
7. From there, step through the code until I trigger the instruction or function call that causes the error.
8.a) if a function call, and that functions code is not trivial, set a breakpoint here, and repeat from 6, this time making sure I step into that function rather than over it
8.b) else, repeat from 6, but stop right before the error occurs, and investigate the state of all variables, and see if the values are what I expect them to be
9. If after all this I still can't make heads or tails of this, ask someone else.

This all assumes that you know how to build a program in debug mode, and the error does occur in debug mode as well. If the latter isn't true, then it gets a lot harder (but more interesting too, IMHO ;) )
Share this answer
hakz.code 18-May-11 5:45am    
I think this process pretty much explains how to debug any error,I think in step 2 we can try to take a hint from the error!
Check if you are trying to do any of the following:
-> dereferencing NULL,
-> dereferencing an uninitialized pointer,
-> dereferencing a pointer that has been freed (or deleted, ) or that has gone out of scope (in the case of arrays declared in functions),
-> writing off the end of an array.
-> recursive function that uses all of the stack space.
Share this answer
Albert Holguin 17-May-11 9:34am    
good feedback
hakz.code 18-May-11 0:12am    
Thank you
Sergey Alexandrovich Kryukov 17-May-11 16:38pm    
All correct except recursion which would show stack overflow. My 4.
hakz.code 18-May-11 0:16am    
Ok,Thanks for correcting ..

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900