stumping in memory mapped
I think some of this is GNU specific. Anyway, the following program creates a new one page (4k) file with nothing in it, maps it to memory, and then my intent was, of course, to write different things to different parts of the map/file and see if I could use them.
Which is as far as I've gotten with memory mapping, because even though the file is really 4096 bytes and everything, attempting to write past byte 1024 causes a segfault. WHY OH WHY?
Code:
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <errno.h>
int main () {
int *map, i;
char buffer[4096];
const char file[]="/tmp/maptest";
int FD=open(file,O_RDWR|O_CREAT);
write(FD,buffer,4096);
map=mmap(NULL,4096,PROT_READ|PROT_WRITE,MAP_SHARED,FD,0);
close(FD); // if "i" goes to 1024, segfault
for (i=800;i<1024;i++) map[i]=54;
printf("%c",map[1800]); // is a 0 (null terminator)
}