3

I have a simple little program that converts one filetype to another. There are quite a few calls to fprintf() (roughly linearly dependent to the size of the file to convert). When I started, there were no calls to fflush(). On small files (<10 Kb) there was no problem whatsoever. On a larger file, (>40 Kb) the whole thing crashed when the call to fclose() was reached.

So, I thought maybe I was letting the buffer fill up too much or something, so I put in calls to fflush() after roughly* 512 calls to fprintf (where each call prints out between 8 and 10 characters). The program still crashes on the call to fclose().

*Because I'm not actually counting the calls to fprintf and I'm using another count that's already in the program, it is possible for this number to be less than 512.

This brings me to my question. When should fflush() be called? Should it be called after a certain amount of data has been fprintf'd? Or is there something I'm missing?

Thanks

By the way, in case it's pertinent, I'm on Windows 7 (64bit) and I've fopen'd the output file in "a+" mode

Marco Merlini
  • 875
  • 7
  • 29
  • It could actually be a case of undefined behavior, as `fprintf` *should* auto-flush the buffer when it's full. – Drew McGowen Jul 31 '14 at 18:53
  • 2
    fflush only needs to be called if there is some external entity that wants the data from the file. You don't need it for your program. There is no need to flush and a lack of flushing has nothing to do with your problem. – tdelaney Jul 31 '14 at 18:54
  • Weird... My only clue was that on large files the program crashed on the call to fclose(). It might not be a buffer issue at all. Unfortunately, I really don't know what else it could be – Marco Merlini Jul 31 '14 at 18:55
  • `valgrind` is your friend. I don't know if Windows has anything equivalent to `strace` or `truss`. If your code is leaking memory for every iteration, then it makes sense it will crash when working on large files and survive the leaks when working on small files. But this is just a guess as I am clueless what's in your code. – alvits Jul 31 '14 at 18:58
  • 1
    Try to create http://stackoverflow.com/help/mcve , because otherwise the issue can be speculated on for a long time without any useful result. – Eugene Podskal Jul 31 '14 at 19:01
  • I'll do my best to make an MVCE. And I'll look for memory leaks as well. Could be a while but I'll edit the post once I've done all that. Thanks for all the help, btw – Marco Merlini Jul 31 '14 at 19:07
  • I've narrowed down the problem to a realloc call and a bad heap pointer. I might make a new question out of it if I really can't solve it myself. However, since this question is about fflush() and fprintf(), should I leave it up or delete it since it doesn't pertain to the problem described in the post? – Marco Merlini Jul 31 '14 at 19:50

1 Answers1

6

It is completely legitimate to call fprintf as many times as one needs without a single call to fflush. Crashes are caused by something else in your program, most likely some invalid memory access, and adding fflush calls will not fix them.

Wojtek Surowka
  • 20,535
  • 4
  • 44
  • 51