我正在尝试使用以下代码(test.c)"mmap"二进制文件(~8Gb).
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#define handle_error(msg) \
do { perror(msg); exit(EXIT_FAILURE); } while (0)
int main(int argc, char *argv[])
{
const char *memblock;
int fd;
struct stat sb;
fd = open(argv[1], O_RDONLY);
fstat(fd, &sb);
printf("Size: %lu\n", (uint64_t)sb.st_size);
memblock = mmap(NULL, sb.st_size, PROT_WRITE, MAP_PRIVATE, fd, 0);
if (memblock == MAP_FAILED) handle_error("mmap");
for(uint64_t i = 0; i < 10; i++)
{
printf("[%lu]=%X ", i, memblock[i]);
}
printf("\n");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
test.c是使用 …
" C编程语言 "一书在第8.7节" 实例 - 存储分配器 "中讨论了"限制性最强的类型" :
虽然机器各不相同,但每台机器都有一个限制性最强的类型:如果限制性最强的类型可以存储在特定地址,则所有其他类型也可以.在某些机器上,限制最多的类型是a
double; 在别人身上,int或是long足够的.
在他们的代码中,union header使用类型对齐long.
限制性最强的是什么意思?它可能是最大的类型(例如double),还是有另一种方法?