Confused about fflush(NULL)

Tim Cussins
Fri Sep 30 16:54:00 GMT 2011

Hi all,

I'm a bit confused about what's supposed to happen when I call fflush(NULL).

I should be using fflush(stdout) and this works fine. But I felt that 
fflush(NULL) should work too and it doesn't.

Here's why (from newlib/stdio/fflush.c):

_DEFUN(fflush, (fp),
        register FILE * fp)
   if (fp == NULL)
     return _fwalk_reent (_GLOBAL_REENT, _fflush_r);

   return _fflush_r (_REENT, fp);

Which agrees with:

I presume that, in my setup, _GLOBAL_REENT has no knowledge of the 
FILE*s in _REENT, as far as I can tell. Can someone please clarify the 
contract between _REENT and _GLOBAL_REENT?

In particular, should I be expecting fflush(NULL) to flush my thread's 
stdout? If so, what do I need to wire up?

I'm working with FreeRTOS and the latest RTEMS toolchain (4.11) for ppc405.

When I create a thread:
	reent = malloc(struct _reent));
	if ( reent == NULL )
		return -1;
	_REENT_INIT_PTR(((struct _reent* )reent));

and on thread switch:
	_impure_ptr = reent;

Any advice, tips and abuse would be warmly, warmly and grudgingly 
welcomed, respectively.

Thanks in advance :)


More information about the Newlib mailing list