开发者

64 bit shared memory segment C \ linux problems

开发者 https://www.devze.com 2022-12-13 03:58 出处:网络
I\'m looking to do some IPC with shared memory segments in C \\ Linux. I go on creating it like normal :

I'm looking to do some IPC with shared memory segments in C \ Linux.

I go on creating it like normal :

typedef struct {
    int a[2];
} shm_segment;

...

shm_segment *shm;

int shm_id;
int shm_flags = IPC_CREAT | 0666
int shm_size = sizeof(struct shm_segment)
key_t key = 88899;
shm_id = shmget(key, shm_size, shm_flags); // ies this is in an if for error cheking but for example sake i omitted it

shm = (shm_segment*)shmat(shm_id, (void*)0, 0);

Last line is where it breaks, at compile it just gives a :

Warning cast to pointer from integer of a different size.

From what I've done before this code works perfectly on 32 bit machines (no warning) (have't tested 开发者_JAVA百科the exact same code, but the same), but on my 64 bit it gives this warning on compile.

And at run it segfaults. From the research i've done i believe casting from a void* to my pointer messes the pointer up, from 64 bit causes.

Any ideea how i can fix this guys? Or what is causing it?


It sounds like you're missing the correct declaration of shmat(), so the compiler is assuming it returns int.

Make sure you have

#include <sys/shm.h>

in your includes.


Get rid of the casts and include the proper headers:

#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/types.h>
0

精彩评论

暂无评论...
验证码 换一张
取 消