Output does not print without fflush (stdout)

I don’t understand why sometimes I need to use fflush() , and sometimes not.

My program is currently interrupted and I am debugging it using print statements. When the program segfaults, stdout does not automatically unload its buffer?

+4
source share
3 answers

I do not understand why sometimes I need to use fflush (), and sometimes not.

Sometimes stdio buffers are sometimes flushed, but it is not. For example, just including "\ n" in printed materials, as a rule, it will hide it (since stdout is attached to the string by default when connected to the terminal).

When the program segfaults, stdout does not clear the buffer automatically?

Stdio buffers are flushed to exit . When a signal (e.g. SIGSEGV ) kills a process, exit not called . Another way to exit the process without flushing stdio buffers is to use the special _exit call for Unix.

+8
source

No, why. The operation is started by the program. If segfault occurs, the program is no longer in a meaningful state, so nothing can happen at this point other than immediate termination.

(And no one is trying to register a signal handler for SIGSEGV .)

+1
source

"I can't understand why fflush (stdout) is called here in this code. I'm trying to comment on this line, and the behavior was the same."

Because you cannot see the previous output of printf () if this output does not end on a new line.

Basically, you only need this if you show a hint without a new line, and you want to make sure that the user can see it.

See this site.

0
source

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


All Articles