【发布时间】:2014-04-29 17:28:34
【问题描述】:
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <iostream>
using namespace std;
#define FILEPATH "file.txt"
#define NUMINTS (268435455)
#define FILESIZE (NUMINTS * sizeof(int))
int main()
{
int i=sizeof(int);
int fd;
double *map; //mmapped array of int's
fd = open(FILEPATH, O_RDONLY);
if (fd == -1) {
perror("Error opening file for reading");
exit(EXIT_FAILURE);
}
map = (double*)mmap(0, FILESIZE, PROT_READ, MAP_SHARED, fd, 0);
if (map == MAP_FAILED) {
close(fd);
perror("Error mmapping the file");
exit(EXIT_FAILURE);
}
for (i = 100000; i <=100100; ++i) {
cout<<map[i]<<endl;
}
if (munmap(map, FILESIZE) == -1) {
perror("Error un-mmapping the file");
}
close(fd);
return 0;
}
我收到文件大小太大的错误。
【问题讨论】:
-
复制/粘贴确切的错误消息,格式化为代码(使用编辑框左上方的
{}工具)是诊断问题的更好证据。考虑使用该信息更新您的问题。祝你好运。 -
文件在哪个FS上?
-
文件包含从 0 到 268435455 的整数
-
你是怎么编译的?你有 32 位还是 64 位操作系统?当前目录的文件系统 (FS) 是什么?
-
这是编译错误还是运行时错误?这听起来像是一个编译文件,尽管映射一个近 2 GB 的文件在运行时也可能有点挑战。您系统上 size_t 的最大范围是多少?