Using mmap to assign space for a struct

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Seisouhen
    New Member
    • Mar 2007
    • 7

    Using mmap to assign space for a struct

    Hi all,
    I am using mmap to obtain some space(mapped anonymously) and am giving the address of the assigned space to a struct pointer. Then I want to access a member of the struct that the pointer points to. The code is:

    //----begin
    struct foo{
    int test;
    }foo;

    struct foo one,*one_ptr;
    one_ptr=mmap(NU LL,sizeof(one), PROT_READ|PROT_ WRITE,MAP_ANON,-1,0);
    *one_ptr = one;
    one_ptr->test =0;
    //---end

    but I get a SIGSEGV at "*one_ptr = one". If I remove the line, then I get a SIGSEGV at the following line "one_ptr->test =0"

    The reason for using mmap is I have to obtain some memory that is to be shared by a bunch of "processes" . The processes should use the same memory and struct is being used to utilize the memory.

    Thanks for your help
    Last edited by Seisouhen; Apr 19 '08, 11:49 PM. Reason: incomplete post
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    Originally posted by siesouhen
    struct foo{
    int test;
    }foo;

    struct foo one,*one_ptr;
    one_ptr=mmap(NU LL,sizeof(one), PROT_READ|PROT_ WRITE ,MAP_ANON,-1,0);
    I think you want sizeof(foo) rather than sizeof(one) in the mmap call. one is a pointer and will be the same size for any struct.

    Also, you are not checking the return from mmap to see if MAP_FAILED.

    Comment

    Working...