Printf () before the line causing the segmentation error fails

When a segmentation error occurs, printf () before it fails.

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

In the situation above, why printf () is not executing?

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

+6
source share
3 answers

The output stream may not be output before the program crashes, but you can force the bytes to be output by dropping them with fflush ().

I usually do this with something like this:

 if (trace) { fflush(stdout); } 
+9
source

Make sure you add the new line "\n" to your printf statement. Usually, at least on UNIX systems, stdout buffered in a line, so a newline character causes the line to appear immediately. You probably omitted "\n" (or your output did not blush for another reason), and therefore you cannot see the printed line.

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

+10
source

Output through printf() and any other standard input / output function are buffered in the standard C library.

You need to call fflush() to ensure that the output will be sent to tty before the program crashes.

+2
source

Source: https://habr.com/ru/post/894667/


All Articles