File mapping error

I'm trying to map a file to memory using mmap function, but whenever I set an offset I get an error!! I tried to google it but no use! could you guys help me with this?

here's my code: it's a template for "type" by the way. In the implementation I replace it by double
1
2
3
4
5
6
7
8
9
10
11
12
13
14
    type* map;
    int fd = open(filename.c_str(), O_RDONLY);
    if (fd == -1)
    {
        throw string("FILEACCESS_EXCEPTION");
    }

    map = (type*) mmap(0, fileSize, PROT_READ, MAP_SHARED, fd, offset_param*sizeof(type));

    if (map == MAP_FAILED)
    {
        close(fd);
        throw string("MAPPING_EXCEPTION");
    }

I keep always getting this MAPPING_EXCEPTION whenever offset_param != 0... I can't understand why this happens!

Thank you in advance guys!
I found the solution. The offset has to be multiples of the system page-size which is 4096 byes. I think I'll have to include this internally in my object to support a real offset.

Thanks for reading :-)
Topic archived. No new replies allowed.