Zoltorn
Zoltorn

Reputation: 171

Use of mmap causes invalid argument error

I am messing with mmap in c and I have encountered a very strange error. When I run the following block of code (which is sample code from this website)

/* The file descriptor. */
int fd;
/* Information about the file. */
struct stat s;
int status;
size_t size;
/* The file name to open. */
const char * file_name = "myfile.txt";
/* The memory-mapped thing itself. */
const void * mapped;
int i;

/* Open the file for reading. */
fd = open ("myfile.txt", O_RDONLY);
check (fd < 0, "open %s failed: %s", file_name, strerror (errno));

/* Get the size of the file. */
status = fstat (fd, & s);
check (status < 0, "stat %s failed: %s", file_name, strerror (errno));
size = s.st_size;

/* Memory-map the file. */
mapped = mmap (0, size, PROT_READ, MAP_SHARED, fd, 0);
check (mapped == MAP_FAILED, "mmap %s failed: %s",
       file_name, strerror (errno));

I am greeted with an Invalid Argument error.

My research has led me to conclude that this is an offset issue, but I am totally lost as to what I can do to fix it. Any advice would be greatly appreciated.

Thank you

Upvotes: 2

Views: 2819

Answers (1)

Zoltorn
Zoltorn

Reputation: 171

I decided to try running this code on another machine and it worked fine, it appears to be a problem machine-side rather than a problem in the code. At least now I know the code isn't broken :)

Upvotes: 1

Related Questions