https://social.msdn.microsoft.com/Forum ... =vcgeneral
I think nobody ever tested fwrite with data more than 4GB as Microsoft code loops forever.
MSVC 2008, 64-bit project:
fwrite( p, sizeof(int), num, fout );
num is 1024*1024*1024
sizeof(int) is 4
fwrite locks the stream and calls
size_t __cdecl _fwrite_nolock
there is nice loop there, where (bufsize is 4096)
nbytes = ( bufsize ? (unsigned)(count - count % bufsize) : (unsigned)count );
count at this point is 4*1024*1024*1024
so
nbytes = (unsigned)(4*1024*1024*1024) =0
it tries to write 0 bytes, subtracts it from count (no change of course) so infinite tight loop.
if I'm trying to write not 4GB of data but say 5GB then
nbytes = ( bufsize ? (unsigned)(count - count % bufsize) : (unsigned)count );
nbytes now 1GB, it writes 1GB, then count is 5GB-1GB = 4GB, (unsigned)4GB is 0 - infinite loop.
So no matter what the size is if it's above 4GB (i've tried to write 10GB originally) it writes out whatever is above closest multiple of 4GB (in the case of 10GB - it writes what is above 8GB hence 2GB) then it gets count as something in multiple of 4GB units (say 8GB) and does
nbytes = (unsigned)(8*1024*1024*1024) =0
and tight loop forever.
I suspect that fread may have the same issue....
Monday, November 30, 2009 5:02 AM
grigdn