I am trying to receive and save file over 5gb size using c++. But during the course of the process, memory used by the application is increasing and sometimes the application crashes. Is there something I am doing wrong ? Or is there a better way to do this?
    char * buffer = channel->cread(&len);
    __int64_t lengthFile = *(__int64_t * ) buffer;
    __int64_t received = 0;
    __int64_t offset = 0;
    __int64_t length;
    string filename = "received/testFile";
    ofstream ifs;
    ifs.open(filename.c_str(),ios::binary | ios::out);
    int len = 0;
    while(1){
        length = 256;
        if(offset + MAX_MESSAGE >= lengthFile){
            length = lengthFile - offset;
            breakCondition = true;
        }
        file = new filemsg(offset,length);
        channel->cwrite((char *)file,sizeof (*file));
        buffer = channel->cread(&len);
        ifs.write(buffer,length);
        received = received + length;
        offset = offset + MAX_MESSAGE;
        if(breakCondition)
            break;
    }
