Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

mmap gives segmentation fault

Posted on 2010-09-24
23
Medium Priority
?
560 Views
Last Modified: 2012-05-10
hi,

i have a file that i am trying to mmap.
and i get the error in valgrind

 Invalid read of size 4
==31586==    at 0x8048F06: filec(char*) (test.c:65)
==31586==    by 0x80489D4: main (c1.c:33)
==31586==  Address 0xFFFFFFFF is not stack'd, malloc'd or (recently) free'd
==31586==
==31586== Process terminating with default action of signal 11 (SIGSEGV)
==31586==  General Protection Fault

the reason is because , i am trying to access a value on of the mapped file. it works fine if the file is so much smaller but it shoudl work with large files too..thanks.

0
Comment
Question by:zizi21
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 13
  • 9
23 Comments
 
LVL 40

Expert Comment

by:evilrix
ID: 33753215
Please post the code that goes with this.
0
 
LVL 46

Expert Comment

by:Kent Olsen
ID: 33753270
Hi zizi,

The error message indicates that you're trying to read address 0xFFFFFFFF.  Coincidentally, that is -1, the value returned my mmap when the mapping is unsuccessful.

After calling mmap, you should check that the mapping is successful.  ERRNO should contain the reason that the mapping failed.


Good Luck,
Kent
0
 

Author Comment

by:zizi21
ID: 33753303
     this is the code

        struct stat sts;
        fp = open(f,0|O_RDWR);
        if(fp == -1)
        {
                fprintf(stderr," No open.");
                exit(1);
        }

        if(fstat (fp, &st) == -1)
        {
                fprintf(stderr, " No status.");
                exit(1);
        }
       
        s = sts.st_size;
        x = (unsigned int*) mmap(NULL, s, PROT_WRITE|PROT_READ, MAP_SHARED, fp, 0);
        if(x == 0)
        {
                fprintf(stderr," No map");
                exit(1);
        }
       
        j = s/sizeof(unsigned int);
       
        printf("x[0] %d\n",x[0]);

this is the code
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 46

Expert Comment

by:Kent Olsen
ID: 33753387
I think that you want to test:

        x = (unsigned int*) mmap(NULL, s, PROT_WRITE|PROT_READ, MAP_SHARED, fp, 0);
        if(x == -1)
        {
                fprintf(stderr," No map");
                exit(1);
        }
0
 

Author Comment

by:zizi21
ID: 33753503
i tried testing that but i got this error

 error: ISO C++ forbids comparison between pointer and integer

and when i did
*x == -1,

i got segmentation fault.
0
 

Author Comment

by:zizi21
ID: 33753510
how do i check errno to find the reason ?
0
 
LVL 46

Accepted Solution

by:
Kent Olsen earned 2000 total points
ID: 33753564
You need to test x, not the dereferenced pointer.

        x = (unsigned int*) mmap(NULL, s, PROT_WRITE|PROT_READ, MAP_SHARED, fp, 0);
        if((int)x == -1)
        {
                fprintf(stderr," No map");
                exit(1);
        }


Sorry -- I missed the casting the first time around.


Kent
0
 
LVL 46

Expert Comment

by:Kent Olsen
ID: 33753574

in the globals block (outside of any function) declare errno.

  extern int errno.


Then print the value of errno if the mapping fails.  The reason codes are in errno.h, which should be in your standard includes directory.


Kent
0
 

Author Comment

by:zizi21
ID: 33753590
i checked the errno and it says errno 12 Cannot allocate memory

0
 

Author Comment

by:zizi21
ID: 33753611
the mapping failed...how is this possible,...i mean,  i though with mmap , that i could mmap file bigger than the existing ram...
0
 

Author Comment

by:zizi21
ID: 33753646
thanks for all the help.
0
 
LVL 46

Expert Comment

by:Kent Olsen
ID: 33753653
Is there enough swap space for it?

How big is the file?  It looks like you're trying to map all of it, so it might not hurt to print the key values (like length) as you debug this.


Kent

0
 

Author Comment

by:zizi21
ID: 33753743
when i tried printing the value for sts.size, i got a negative value.

but when i did ls -lh, the size of the file 3.2 gb
0
 
LVL 46

Expert Comment

by:Kent Olsen
ID: 33753821

Ok.  When you store 3.2GB in an integer, all 32 bits are used.  This overflows into the sign bit so the value that you see printed is negative.

Display the value as an unsigned integer and it should display just fine.

You may be trying to allocate more virtual memory than you have defined.  


Kent
0
 

Author Comment

by:zizi21
ID: 33753860
i tried using %ld and i still got negative number
0
 

Author Comment

by:zizi21
ID: 33753874
i get the value of the size from struct stat sts.
0
 
LVL 46

Expert Comment

by:Kent Olsen
ID: 33753877

%ld formats a long integer.  use %u.

0
 

Author Comment

by:zizi21
ID: 33753894
also, the size of the file is larger than the existing ram.
0
 

Author Comment

by:zizi21
ID: 33753921
yes, i got a positive number now with %u
0
 
LVL 46

Expert Comment

by:Kent Olsen
ID: 33755534

How large is the swap file?
0
 

Author Comment

by:zizi21
ID: 33759113
its 1 gb
0
 

Author Comment

by:zizi21
ID: 33759114
thanks for all the help.
0
 
LVL 46

Expert Comment

by:Kent Olsen
ID: 33760857
Hi zizi,

Did you get this resolved?

The swap file should probably be at least as large as RAM.


Kent
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

660 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question