Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Buffered printfs????

Reply
Thread Tools

Buffered printfs????

 
 
evan
Guest
Posts: n/a
 
      02-25-2005
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

 
Reply With Quote
 
 
 
 
Richard Cavell
Guest
Posts: n/a
 
      02-25-2005
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");
 
Reply With Quote
 
 
 
 
Thomas Maier-Komor
Guest
Posts: n/a
 
      02-25-2005
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
 
Reply With Quote
 
Ioannis Vranos
Guest
Posts: n/a
 
      02-25-2005
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
 
Reply With Quote
 
Aslan Kral
Guest
Posts: n/a
 
      02-25-2005

"evan" <(E-Mail Removed)>, haber iletisinde sunlari
yazdi:(E-Mail Removed) roups.com...
> 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;
}


 
Reply With Quote
 
Dietmar Kuehl
Guest
Posts: n/a
 
      02-25-2005
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.
--
<(E-Mail Removed)> <http://www.dietmar-kuehl.de/>
<http://www.contendix.com> - Software Development & Consulting

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
logging buffered vs. logging history Christian Roos Cisco 4 02-05-2006 10:55 PM
HttpWebRequest.GetResponse() buffered read - possible ? microdevsolutions@yahoo.com.au ASP .Net 2 12-06-2005 10:05 AM
whether is the standard input stream full buffered or line buffered after calling function setbuf()? kernelxu@hotmail.com C Programming 9 08-23-2005 02:24 PM
Response Filter losing data when response is buffered Brad ASP .Net 3 02-19-2004 01:13 AM
sending web response wihout buffered =?Utf-8?B?TWVobWV0?= ASP .Net 1 01-13-2004 11:55 AM



Advertisments