Bit*_*tor 1 c++ memory-management windows-server-2008 visual-studio-2008 visual-c++
我的系统上有32 GB的RAM,并希望在内存中读取并保留15 GB的文件.当我尝试执行以下代码时,输出为:"n read = 1073741824",它恰好对应于1 GB,而它应该具有与文件大小相对应的读取字节.
我检查了甚至运行系统资源管理器,并且增加了正好1GB,这意味着malloc没有按预期分配17 GB的空间.
使用64位系统,我可以读取高达8TB(32位系统,它是2GB或更具体的1.5 GB).
我在64位机器和操作系统上,我已经按照以下步骤配置Visual C++来构建和执行64位二进制文件:Intel Link [通过检查int的大小验证它是64位 - 之前它是4字节,现在是8字节]
为了分配和使用大于1 GB的内存,我可以调整什么?
#include <stdio.h>
#include <cstdlib>
#define PATH "C:\\train"
#define SIZE 17*1024*1024*1024
int main()
{
FILE * fp = fopen(PATH, "r");
char* buff = (char*)malloc(SIZE);
int nRead = fread(buff, 1, SIZE, fp);
buff[nRead] = 0;
printf("n read = %d\n", nRead);
int a;
scanf("%d" ,&a);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
问题是它17*1024*1024*1024是一个文字类型int,int在Windows上是四个字节宽.您错误地认为int在64位Windows 中是8字节宽.不是.它是4个字节宽.显然,4字节整数不能保存您希望放入其中的值.
您需要使用64位整数文字:
17*1024*1024*1024ull
Run Code Online (Sandbox Code Playgroud)