Home > Archive > Unix Programming > November 2004 > help with mmap
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
|
|
| Nils Grimsmo 2004-11-23, 8:57 pm |
| why does mmap return -1 here?
the file is readable.
i have read the mmap manpage, but do not understand what i do wrong.
#include <stdio.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <unistd.h>
int main()
{
FILE *fp = fopen("data_1M", "r");
fs (fp, 0, SEEK_END);
size_t size = ftell(fp);
rewind(fp);
printf("%d\n", size);
char *data = (char*)mmap(0, size, PROT_READ, MAP_PRIVATE, (int)fp,
(off_t)0);
printf("%d\n", (int)data);
fclose(fp);
}
thanks!
klem fra nils
| |
| Mr. Uh Clem 2004-11-23, 8:57 pm |
| Nils Grimsmo wrote:
> why does mmap return -1 here?
>
> the file is readable.
>
> i have read the mmap manpage, but do not understand what i do wrong.
>
>
> #include <stdio.h>
> #include <stdlib.h>
> #include <sys/mman.h>
> #include <unistd.h>
>
> int main()
> {
> FILE *fp = fopen("data_1M", "r");
You need a file descriptor here, not a file pointer. Use open().
> fs (fp, 0, SEEK_END);
Not needed.
> size_t size = ftell(fp);
stat() the file.
> rewind(fp);
Noooo.
> printf("%d\n", size);
> char *data = (char*)mmap(0, size, PROT_READ, MAP_PRIVATE, (int)fp,
> (off_t)0);
> printf("%d\n", (int)data);
> fclose(fp);
close()
> }
>
>
> thanks!
>
> klem fra nils
--
Clem
"If you push something hard enough, it will fall over."
- Fudd's first law of opposition
| |
| Eric Sosman 2004-11-23, 8:57 pm |
| Nils Grimsmo wrote:
> why does mmap return -1 here?
>
> the file is readable.
>
> i have read the mmap manpage, but do not understand what i do wrong.
>
>
> #include <stdio.h>
> #include <stdlib.h>
> #include <sys/mman.h>
> #include <unistd.h>
>
> int main()
> {
> FILE *fp = fopen("data_1M", "r");
> fs (fp, 0, SEEK_END);
> size_t size = ftell(fp);
> rewind(fp);
> printf("%d\n", size);
> char *data = (char*)mmap(0, size, PROT_READ, MAP_PRIVATE, (int)fp,
> (off_t)0);
> printf("%d\n", (int)data);
> fclose(fp);
> }
When a system call fails, `errno' usually provides
additional information about the failure. You can use
that information to help figure out the cause.
In this case, `errno' probably has the value EBADF,
meaning "invalid file descriptor." That's because you
don't understand the difference between a file descriptor
and a FILE pointer, and you've tried to use the latter
where the former is required. It appears that the compiler
complained when you did so, and you responded by adding
a cast to silence the complaint -- but silencing the
complaint didn't cure the error, so all you accomplished
was to mislead yourself.
--
Eric.Sosman@sun.com
| |
| William Ahern 2004-11-23, 8:57 pm |
| Nils Grimsmo <nils.grimsmo@idi.ntnu.no> wrote:
> why does mmap return -1 here?
>
> the file is readable.
>
> i have read the mmap manpage, but do not understand what i do wrong.
>
> #include <stdio.h>
> #include <stdlib.h>
> #include <sys/mman.h>
> #include <unistd.h>
> int main()
> {
> FILE *fp = fopen("data_1M", "r");
> fs (fp, 0, SEEK_END);
> size_t size = ftell(fp);
> rewind(fp);
> printf("%d\n", size);
> char *data = (char*)mmap(0, size, PROT_READ, MAP_PRIVATE, (int)fp,
> (off_t)0);
> printf("%d\n", (int)data);
> fclose(fp);
> }
Stop casting! If you stop casting--and maybe turn up the warning level on
your compiler--you'll see many errors. The times when you need to cast
should be few and far between.
Others have explained the code problems. But your real problem is you're
casting too much. Just say no.
| |
| Nils Grimsmo 2004-11-25, 3:56 pm |
| Mr. Uh Clem wrote:
> Nils Grimsmo wrote:
>
[snip][color=darkred]
> Noooo.
:)
[snip]
thank you for the help, all of you.
i will try to relax my casting. you can try to guess what programming
language i originally am used to :)
klem fra nils
|
|
|
|
|