mmap from file in loop
<p>Im trying to work with mmap reading from file in loop,
Im have file contains info about 3 parts, first part is size of 3*sizeof(double),
second one also in the size of 3*sizeof(double), and the third with size of sizeof(double). At the first part of the file I have HEADER with size of 32768 bytes. The file organised:</p>

<p>HEADER||Part(1),Part(1)....Part(1)||Part(2),Part(2)....Part(2)||Part(3),Part(3)....Part(3)|</p>

<p>Each part I have 100 times.
I want to work each time with 30 parts (10 parts from each part).</p>

<p>I have tried this code:</p>

<pre><code>void readingFile(FILE *file, double *a, double *b, double *c, int start, int end, int chunksz, long total)
{
    int i = 0;
    int size = end - start + 1;
    int fd;
    fd = fileno(file);
    off_t fullsize = lseek(fd,SEEK_CUR,SEEK_END); //getting the file size
    fullsize-=1;//the lseek gives one more byte, its ok!
    unsigned long summ = (unsigned long)(start-1)*chunksz; //chunk is 56
    summ+=(unsigned long)HEADER_SIZE;//offset the header size
    unsigned long paramm=(unsigned long)((unsigned long)summ/(unsigned long)(sysconf(_SC_PAGE_SIZE)));
    unsigned long param = floor(paramm);
    void *buf=NULL;
    buf =mmap(NULL,fullsize , PROT_READ, MAP_PRIVATE , fd, param*sysconf(_SC_PAGE_SIZE));
    if(buf==MAP_FAILED)
    {
        printf("we have an error\n");
    }
    unsigned long gapp = (sysconf(_SC_PAGE_SIZE))*param;
    unsigned long gap =summ-gapp;
    buf+=gap;
    memcpy(a,buf,3*sizeof(double)*size);
    buf+=(unsigned long)((long)total-(start-1))*3*sizeof(double);
    buf+=((start-1)*3*sizeof(double));
    memcpy(b,buf,3*sizeof(double)*size);
    buf+=(unsigned long)((long)total-(start-1))*3*sizeof(double);
    buf+=((start-1)*sizeof(double));
    memcpy(c,buf,sizeof(double)*size);
    munmap(buf, fullsize);
    return;
}
</code></pre>

<p>Somewhere in the way I have Overflow and the program crashing!
Each time the function being called, A new memory is allotted properly to a,b,c.
What is worng here?
The process crashed at iteration number 14 in line:</p>

<pre><code>memcpy(c,buf,sizeof(double)*size);
</code></pre>

<p>Thanks!</p>