in reply to [OT]: Flushing XS buffers portably

Check out perlapio ... you shouldn't be using stdio for printing but perl's IO abstraction interface:

use warnings; use Inline C => <<'EOC'; void foo() { PerlIO_printf(PerlIO_stdout(),"HELLO"); } EOC for(1..3) { foo(); print "\n"; }

-derby

Replies are listed 'Best First'.
Re^2: [OT]: Flushing XS buffers portably
by syphilis (Archbishop) on Aug 31, 2007 at 13:46 UTC
    Dammit, derby, you found the escape clause :-)

    In real life, foo calls a third party C library function that does use stdio for printing ... something like:
    use Inline C => <<'EOC'; #include <mpfr.h> void foo(mpfr_t * x) { mpfr_out_str(*x, .......); } EOC
    The Inline::C script that I presented was the closest thing I could think of that would actually depict the problem generically ... but, as I can now see, I stuffed up.

    So ... although your response answers the question I asked, it doesn't really address the question I was hoping to be answered ... my fault, not yours.

    I guess I'm really after a way of flushing the stdio printf buffer - sort of an stdio way of doing $| = 1; (if such exists).

    Btw, I had long wondered about the advantages of using the PerlIO_* routines as recommended in perldoc perlclib (and other places). Your reply provides an excellent demonstration of those "advantages". Thank you.

    Cheers,
    Rob
      I guess I'm really after a way of flushing the stdio printf buffer - sort of an stdio way of doing $| = 1; (if such exists).

      Have you tried fflush(3)?

      (It's not autoflushing, though; you have to call it whenever you want to have the buffers flushed...)

        Have you tried fflush(3)?

        If I literally fflush(3); it segfaults on both winders and linux ... but fflush(stdout); works fine on both platforms. Thanks muchly.

        I guess that's likely to work on other operating systems as well ?

        Cheers,
        Rob