in reply to Re^2: [OT]: Flushing XS buffers portably
in thread [OT]: Flushing XS buffers portably

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...)

Replies are listed 'Best First'.
Re^4: [OT]: Flushing XS buffers portably
by syphilis (Archbishop) on Aug 31, 2007 at 14:18 UTC
    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

      The '3' is the manual page section ("library calls") where the function is documented... :)   (an old unix convention — typically, I avoid using it for just that reason... sorry I didn't in this case).

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

      Yes I'd think so, it's a pretty standard library call (ANSI C - X3.159-1989, the man page says).

        Yes I'd think so, it's a pretty standard library call

        Yep ... thanks again almut.

        Still, it's something that should be tested in an extension's test script ... so ... one has a module that contains an xsub that doesn't return anything:
        void foo(mpfr_t * x) { mpfr_out_str(*x, .......); fflush(stdout); }
        How does one verify (in a .t test script) that foo is producing the desired output ?

        Heavens forbid ... I'm now straying into on-topic areas, aren't I :-)

        Actually ... I'm about to answer the question I just posed, but I'll send this post anyway. As it turns out mpfr_out_str() is not a void function, but returns the number of bytes written (as a size_t). So, to make foo testable, I guess one could just rewrite it as:
        SV * foo(mpfr_t * x) { size_t s = mpfr_out_str(*x, .......); fflush(stdout); return newSViv(s); }
        And then the test script would simply check that foo($var) returned the expected number of bytes.

        If there are any comments on that, please don't be shy :-)

        Cheers,
        Rob