OK for whatever reason I’m having trouble causing a seg fault. I want to produce one so that I can use gdb to see how to debug one. I have tried both examples from the Wikipedia article yet neither work.
The first one:
char *s = "Hello World!";
*s = 'H';
And the second example:
int main(void)
{
main();
}
EDIT: I’m using Ubutnu 9.10 and g++ as my compiler. Can anyone show me some code that is guaranteed to segfault?
It impossible to try and reliable do it dereferencing pointers.
This is because how the application handles memory can vary from compiler to compiler also across the same compiler with different options (debug/release mode handled differently).
What you can do is explicitly raise the segfault using a signal: