help with mmap
Web Server forum
Back To The Forum Home!Search!Private Messaging System

Web Server Talk Web Server Talk > Unix and Linux reviews > Free Unix support > Unix Programming > help with mmap




  Last Thread   Next Thread Next
  Show Printable Version Email this Page Subscribe to this Thread      Post New Thread    Post A Reply      

    help with mmap  
Nils Grimsmo


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
11-23-04 11:08 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");
fseek(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





[ Post a follow-up to this message ]



    Re: help with mmap  
Mr. Uh Clem


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
11-23-04 11:08 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().

>     fseek(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





[ Post a follow-up to this message ]



    Re: help with mmap  
Eric Sosman


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
11-23-04 11:08 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");
>      fseek(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







[ Post a follow-up to this message ]



    Re: help with mmap  
William Ahern


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
11-23-04 11:08 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");
>      fseek(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.






[ Post a follow-up to this message ]



    Re: help with mmap  
Nils Grimsmo


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
11-25-04 12:50 PM

Mr. Uh Clem wrote:
> Nils Grimsmo wrote:
> 
[snip][vbcol=seagreen]
> 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





[ Post a follow-up to this message ]



    Sponsored Links  




 





   All times are GMT. The time now is 09:29 PM.      Post New Thread    Post A Reply      
  Last Thread   Next Thread Next


Most Popular forums 

Forum Jump:
Rate This Thread:

Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is OFF
 
Medical and Health forum | Computer Games Reviews | Graphics design forum

Back To The Top
Home | Usercp | Faq | Register