Buffered printfs????

E

evan

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
 
R

Richard Cavell

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");
 
T

Thomas Maier-Komor

evan said:
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
 
I

Ioannis Vranos

evan said:
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?
 
A

Aslan Kral

haber iletisinde sunlari said:
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;
}
 
D

Dietmar Kuehl

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top