可能重复:
suggest us on mmap system call operation that is able to access memory locations
我的问题是“如何实现一个程序,使用mmap(比如说1GB)分配大块内存然后尝试访问随机内存位置来读取/写入它”这是我试过的示例程序:
#include <stdio.h>
#include <stdlib.h>
#include <sys/mman.h>
int main() {
int fd,len,mem_to_alloc
char *p;
void *mem;
while(1)
{
printf(“memory to allocate”);
scanf(“%d”,&mem_to_alloc);
mem= mmap(0, len, PROT_READ, MAP_SHARED, fd, 0);
if(mem==NULL)
printf(“out of memory”);
else
printf(“memory allocated through mmap”);
}
return 0;
}
分配此内存时,如何访问此位置? 有人能帮助我吗?