Or just debugging in general, how do you like to go about finding bugs in code. Specifically for C/C++, but all languages in general. I\'ve been trying to find the cause of
This solution usually permit to identify the segfault and his cause.
I usually run in the debugger... generally there are enough clues to go on to track it down... call stack, current state of variables, etc. If the stack is trashed or it's heap corruption, it can be more difficult. If I have a repeatable scenario, I'll run it in valgrind (Linux) which bounds-checks everything and can often pinpoint the issue.
If you're in a fancy IDE try to familiarize yourself with breakpoints, you can use them to see how far you get into your code before the segfault happens. If you don't have that option, the simplest way is to comment out parts of your code one at a time until you find what is breaking.
I have found the commenting out method to be pretty efficient for finding segfaults in small homework sized C problems.
Use a debugger such as gdb and upon seg fault, print back trace. It would show the line number and file on which it crashed. Use this as a starting point.
To take this further, you can repeat the process to make sure it is not a random fault occuring due to something wrong else where earlier and rather a specific problem at that line number.
For static code analysis you can use tools such as klockworks or lint which would show possible issues in your code.
For dynamic analysis, use tools such as memwatch which would monitor your memory allocations at run time.
i didn't point out valgrind as it is already mentioned by others and no doubt is a great tool.
Try to push your code into bad situations.
If you're writing a parser, throw BMPs, JPGs, random text at it, and see what happens. If you're writing a RPC protocol server, overload it with plenty of concurrent requests, write garbage into it, disconnect the client in the middle of nowhere...
Don't be subtle at first, throw whatever possible, but then try to trick your code.
RMS made a good tutorial for finding a segfault using gdb.
Short version:
$ gcc -g -O0 -o program program.c # -O0 makes debugging easier
$ gdb ./program
> run # Tell gdb to start running the thing
...segfault happens
> bt # Get a stack trace from where it segfaulted