Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Output not printing without fflush(stdout)

Tags:

c

io

stdout

fflush

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

My program is segfaulting at the moment and I am debugging it with print statements. When a program segfaults, does stdout not flush its buffer automatically?

like image 411
darksky Avatar asked Nov 29 '11 19:11

darksky


1 Answers

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

Sometimes the stdio buffers are flushed sometimes they aren't. For example simply including a "\n" in the printed stuff will typically flush it (because stdout is by default line-buffered when attached to a terminal).

When a program segfaults, does stdout not flush its buffer automatically ?

Stdio buffers are flushed by exit. When a signal (such as SIGSEGV) kills a process, exit is not called. Another way to exit a process without flushing the stdio buffers is to use the Unix-specific call _exit.

like image 110
cnicutar Avatar answered Oct 05 '22 23:10

cnicutar