Buffered printfs????

Discussion in 'C++' started by evan, Feb 25, 2005.

  1. evan

    evan Guest

    I am calling a C function from a C++ method as follows:

    int mem_if::read(void) {
    int i;
    for(i = 0; i < 5; i++) {
    printf("A");
    E2Read(i);
    }
    return size;
    }

    , where the C function is:

    void E2Read(int i) {
    printf("B");
    .... (no function calls)
    }

    I would expect the output to be something along the lines of:
    ABABABABAB

    , but I am getting something more like:
    BBBBBAAAAA

    What is going on????? It is like the C++ printfs are getting buffered
    and are printed out way after they should be.

    /evan
    evan, Feb 25, 2005
    #1
    1. Advertising

  2. On 25/2/05 12:51 PM, evan wrote:

    > What is going on????? It is like the C++ printfs are getting buffered
    > and are printed out way after they should be.


    I can't explain what is going on, but I can tell you that the
    implementation of XCode/gcc/Mac OSX developed by Apple definitely
    buffers printf. Things might not print at all if your program exits
    before a printf("\n");
    Richard Cavell, Feb 25, 2005
    #2
    1. Advertising

  3. evan wrote:
    > I am calling a C function from a C++ method as follows:
    >
    > int mem_if::read(void) {
    > int i;
    > for(i = 0; i < 5; i++) {
    > printf("A");
    > E2Read(i);
    > }
    > return size;
    > }
    >
    > , where the C function is:
    >
    > void E2Read(int i) {
    > printf("B");
    > .... (no function calls)
    > }
    >
    > I would expect the output to be something along the lines of:
    > ABABABABAB
    >
    > , but I am getting something more like:
    > BBBBBAAAAA
    >
    > What is going on????? It is like the C++ printfs are getting buffered
    > and are printed out way after they should be.
    >
    > /evan
    >


    are you using gcc? IIRC gcc does not sync stdio of the various
    output streams that are different in C and C++... It's a known
    bug.

    My suggestion: use a different compiler.


    Tom

    --
    ________________________________________________________________________
    Dipl.-Ing. Thomas Maier-Komor http://www.rcs.ei.tum.de
    Institute for Real-Time Computer Systems (RCS) fon +49-89-289-23578
    Technische Universitaet Muenchen, D-80290 Muenchen fax +49-89-289-23555
    Thomas Maier-Komor, Feb 25, 2005
    #3
  4. evan wrote:

    > I am calling a C function from a C++ method as follows:
    >
    > int mem_if::read(void) {
    > int i;
    > for(i = 0; i < 5; i++) {
    > printf("A");
    > E2Read(i);
    > }
    > return size;
    > }
    >
    > , where the C function is:
    >
    > void E2Read(int i) {
    > printf("B");
    > .... (no function calls)
    > }
    >
    > I would expect the output to be something along the lines of:
    > ABABABABAB
    >
    > , but I am getting something more like:
    > BBBBBAAAAA
    >
    > What is going on????? It is like the C++ printfs are getting buffered
    > and are printed out way after they should be.



    May you post a complete code with main() that produces the behaviour you
    are mentioning?




    --
    Ioannis Vranos

    http://www23.brinkster.com/noicys
    Ioannis Vranos, Feb 25, 2005
    #4
  5. evan

    Aslan Kral Guest

    "evan" <>, haber iletisinde sunlari
    yazdi:...
    > I am calling a C function from a C++ method as follows:
    >
    > int mem_if::read(void) {
    > int i;
    > for(i = 0; i < 5; i++) {
    > printf("A");
    > E2Read(i);
    > }
    > return size;
    > }
    >
    > , where the C function is:
    >
    > void E2Read(int i) {
    > printf("B");
    > .... (no function calls)
    > }
    >
    > I would expect the output to be something along the lines of:
    > ABABABABAB
    >
    > , but I am getting something more like:
    > BBBBBAAAAA
    >
    > What is going on????? It is like the C++ printfs are getting buffered
    > and are printed out way after they should be.
    >
    > /evan
    >


    The following code is OK on my pc with MSVC6 and cygwin b20 and minGW. Can
    you test it and see if it makes any difference?

    #include <stdio.h>

    void E2Read(int i)
    {
    printf("B");
    }
    int read(void)
    {
    int i;
    for(i = 0; i < 5; i++)
    {
    printf("A");
    E2Read(i);
    }
    return i;
    }

    int main()
    {
    read();
    return 0;
    }
    Aslan Kral, Feb 25, 2005
    #5
  6. Thomas Maier-Komor wrote:
    > > int mem_if::read(void) {
    > > int i;
    > > for(i = 0; i < 5; i++) {
    > > printf("A");
    > > E2Read(i);
    > > }
    > > return size;
    > > }


    > > void E2Read(int i) {
    > > printf("B");
    > > .... (no function calls)
    > > }


    > are you using gcc? IIRC gcc does not sync stdio of the various
    > output streams that are different in C and C++... It's a known
    > bug.


    He actually does not use any streams in C++! He only uses 'printf()'.
    I can imagine that different DLLs don't share a common 'stdout'
    object but this is unlikely to be a compiler issue but rather related
    to idiosyncrasies of the underlying system.
    --
    <mailto:> <http://www.dietmar-kuehl.de/>
    <http://www.contendix.com> - Software Development & Consulting
    Dietmar Kuehl, Feb 25, 2005
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?TWVobWV0?=

    sending web response wihout buffered

    =?Utf-8?B?TWVobWV0?=, Jan 13, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    322
    Hermit Dave
    Jan 13, 2004
  2. Brad
    Replies:
    3
    Views:
    1,866
    Steven Cheng[MSFT]
    Feb 19, 2004
  3. Replies:
    9
    Views:
    676
    Michael Wojcik
    Aug 23, 2005
  4. Replies:
    2
    Views:
    350
    =?utf-8?B?SGFyYWxkIHZhbiBExLNr?=
    Feb 7, 2007
  5. karthikbalaguru

    Strange printfs !!

    karthikbalaguru, Mar 3, 2009, in forum: C Programming
    Replies:
    11
    Views:
    614
    Kojak
    Mar 3, 2009
Loading...

Share This Page