简体   繁体   English

C ++:将大的二进制文件(2GB至4GB)保存到char *中的RAM中吗?

[英]C++: Saving a large binary file (2GB to 4GB) to RAM in a char*?

I am working on a file transfer program in C++ which converts a file to binary, saves the bytes in a char*, then sends that char* through a TCP connection to another computer. 我正在使用C ++中的文件传输程序 ,它将文件转换为二进制文件,将字节保存在char *中,然后通过TCP连接将该char *发送到另一台计算机。 The other computer then recreates the file locally. 然后,另一台计算机在本地重新创建文件。 The program does work, but I run into a big problem with large files! 该程序可以运行,但是我遇到了一个大文件的大问题! I cannot allocate enough array indexes to contain the bytes! 我无法分配足够的数组索引来包含字节! For example, if I want to send a 600MB file, I need a char* with 600 MILLION indexes. 例如,如果我要发送600MB的文件,则需要具有6亿个索引的char *。 This works. 这可行。 But once I go any higher, the program simply cannot allocate the memory and I get errors. 但是,一旦我再提高一点,该程序就无法分配内存,并且会出现错误。

A friend of mine suggested that I split the file into chunks and do the transfer chunk by chunk, however this creates a plethora of other challenges and would require me to basically rewrite the entire program. 我的一个朋友建议我将文件拆分为多个块,然后逐个块地进行传输,但这会带来很多其他挑战,并且需要我基本上重写整个程序。

Is there any way to get around this? 有什么办法可以解决这个问题?

A friend of mine suggested that I split the file into chunks and do the transfer chunk by chunk, however this creates a plethora of other challenges and would require me to basically rewrite the entire program. 我的一个朋友建议我将文件拆分为多个块,然后逐个块地进行传输,但这会带来很多其他挑战,并且需要我基本上重写整个程序。

This is why it's called computer science, and why once you have mastered these challenges, you can head to the city and earn the big bucks. 这就是为什么它被称为计算机科学的原因,而且一旦您掌握了这些挑战,便可以前往城市并赚大钱了。

I don't know what you mean with converting to binary , but you shouldn't have to allocate 600MB+ of memory, but work with buffering instead. 我不知道转换为binary的意思,但是您不必分配600MB +的内存,而应使用缓冲。

For example, to send a file from disk: 例如,要从磁盘发送文件:

  • open file 打开文件
  • read part of file in buffer 读取缓冲区中文件的一部分
  • send buffer over TCP connection (repeat until done) 通过TCP连接发送缓冲区(重复直到完成)

You could also use memory mapping (or TransmitFile() in Windows). 您也可以使用内存映射(或Windows中的TransmitFile())。

In case your data needs to be converted: 如果您的数据需要转换:

  • open file 打开文件
  • read part in buffer 读取缓冲区中的部分
  • convert buffer 转换缓冲区
  • send buffer (repeat) 发送缓冲区(重复)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM