Learn how easy it is to sync an existing GitHub or Google Code repo to a SourceForge project! See Demo

Close

std::ofstream::write() hangs on large blocks

yhw
2013-02-18
2013-06-06
  • yhw
    yhw
    2013-02-18

    Hi all,

    I'm using x86_64-w64-mingw32-gcc-4.7.2-release-win64_rubenvb.7z on Windows 7 x64. I need to work with large arrays (>2GB) and read/write them to a disk. When I try to write an array using std::ofstream::write(), it never returns if the block size (the second argument) is more than 2GB. Here's a test program to reproduce the bug. The file is created, but the assertion is never executed:

    #include <cstddef>
    #include <cassert>
    #include <fstream>
    int main() {
        const std::size_t size = 3000000000ULL; // ~3GB
        char *data = new char[size];
        std::ofstream ofs("test.tmp", std::ios_base::out | std::ios_base::binary);
        ofs.write(data, size);
        assert(false);
        return 0;
    }
    

    Any suggestions on how to fix it? Maybe I should try another version of mingw? I read the following on the main page:

    Version 3.0 (Currently in trunk and considered unstable) has some LFS64 support that allow traditional Unix programs to use 64-bit large file support if they so choose.

    Is that related to my problem?

     
  • yhw
    yhw
    2013-02-18

    Stupid forum eated some charactes from the code…

    #include <cstddef>
    #include <cassert>
    #include <fstream>
    int main() {
        const std::size_t size = 3000000000ULL; // ~3GB
        char *data = new char\[size\];
        std::ofstream ofs("test.tmp", std::ios_base::out | std::ios_base::binary);
        ofs.write(data, size);
        assert(false);
        return 0;
    }
    
     
  • yhw
    yhw
    2013-02-18

    #include <cstddef>
    #include <cassert>
    #include <fstream>
    int main() {
        const std::size_t size = 3000000000ULL; // ~3GB
        char *data = new char{size}; // replace } with ] and { with [
        std::ofstream ofs("test.tmp", std::ios_base::out | std::ios_base::binary);
        ofs.write(data, size);
        assert(false);
        return 0;
    }