Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Printf() before line causing segmentation fault does not execute

Tags:

c

gcc

When a segmentation fault occurs, the printf() before it does not execute.

main()
{
 printf( "something" );
 statement;  //this statement causes a segmentation fault
}

In the situation above, why does the printf() not execute?

So do I need to use valgrind in such a case(which prints all printf() before the faulty statement).

like image 775
Jeegar Patel Avatar asked Aug 09 '11 18:08

Jeegar Patel


1 Answers

Make sure you include a newline "\n" in your printf statement. Normally, at least in UNIX systems, stdout is line-buffered so newline character makes the line to appear immediately. You probably omitted "\n" (or your output is not flushed for other reason) and that's why you can't see the printed string.

Another option is to flush the output yourself using fflush(stdout) after calling printf.

like image 197
ecik Avatar answered Sep 30 '22 04:09

ecik