David
David

Reputation: 3105

get N-th byte from mmaped file

I'm new at C and now I'm learning about mmap. I want to get N-th byte from mmaped file, but I get this error Segmentation Fault (core dumped) When I test my program with gdb I get that something is wrong with this line printf("%d\n", (int) data[sk]); and then I print data and I get

(gdb) print data[sk]
Cannot access memory at address 0xfe5f07d0
(gdb) print data
$1 = 0xfe5f0000 <Address 0xfe5f0000 out of bounds>

I have no idea why I get this error. Here is my code

int main( int argc, char * argv[] ){
    int sk;
    int d;
    char *data;
    size_t s;
    if(argc == 3){
        sk = atoi(argv[2]);
        d = da_open(argv[1]);
        s = da_fileSize(d);
        data = (char*)da_mmap(d, s);
        printf("File Size: %d\n", (int) s);
        printf("%d\n", (int) data[sk]); // this line is bad. But why?
        close(d);
    }
    return 0;
}

Also here is my full code

#include <stdio.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/time.h>
#include <string.h>

int da_open(const char *name);
void *da_mmap(int d, size_t size);
size_t da_fileSize();

int da_open(const char *name){
   int dskr;
   dskr = open( name, O_RDWR );
   if( dskr == -1 ){
      perror( name );
      exit(1);
   }
   printf( "dskr1 = %d\n", dskr );
   return dskr;
}

void *da_mmap(int d, size_t size){
     void *a = NULL;
     a = mmap(NULL, size, PROT_WRITE, MAP_SHARED, d, 0);
     if( a == MAP_FAILED ){
          perror( "mmap failed" );
          abort();
     }
     return a;
}

size_t da_fileSize(int d){
    struct stat info;
    if(fstat(d, &info) == -1) {
        perror("fstat failed");
        exit(1);
    }
    return (size_t)info.st_size;
}

int main( int argc, char * argv[] ){
    int sk;
    int d;
    char *data;
    size_t s;
    if(argc == 3){
        sk = atoi(argv[2]);
        d = da_open(argv[1]);
        s = da_fileSize(d);
        data = (char*)da_mmap(d, s);
        printf("File Size: %d\n", (int) s);
        printf("%d\n", (int) data[sk]);
        close(d);
    }
    return 0;
}

Upvotes: 1

Views: 238

Answers (2)

Th&#233;o Richard
Th&#233;o Richard

Reputation: 247

A segmentation fault is when you try to access a memory space that you don't own. It usually happens (like in your case) with arrays. If you have an array that is 5 elements long and you try to access the 6th, you'll get a segmentation fault and your program will stop.

So in the line printf("%d\n", (int) data[sk]); your error is probably that the value in sk is too big, you should try to print it to check it.

also you should allways check the return of system functions (functions that are not yours), for exemple your open(), or atoi(). if open fails and you dont stop your program, you are gonna have a bad time :)

Upvotes: 2

Frederik Deweerdt
Frederik Deweerdt

Reputation: 5281

I'm guessing you also need read permissions in your mmap: PROT_WRITE | PROT_READ

Upvotes: 5

Related Questions