468,512 Members | 1,532 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,512 developers. It's quick & easy.

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(NULL,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
Apr 19 '08 #1
1 5611
weaknessforcats
9,207 Expert Mod 8TB
struct foo{
int test;
}foo;

struct foo one,*one_ptr;
one_ptr=mmap(NULL,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.
Apr 20 '08 #2

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

1 post views Thread by Parzival | last post: by
4 posts views Thread by Hao Xu | last post: by
1 post views Thread by Hao Xu | last post: by
2 posts views Thread by Martin v. Lwis | last post: by
1 post views Thread by Carl Mackey | last post: by
26 posts views Thread by myeates | last post: by
13 posts views Thread by George Sakkis | last post: by
5 posts views Thread by castironpi | last post: by
reply views Thread by NPC403 | last post: by
1 post views Thread by fmendoza | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.