views:

265

answers:

2

Hi,

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 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?

+2  A: 

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.

caf
You are right, that was the problem. Thanks caf
theBlinker
+2  A: 

Get rid of the casts and include the proper headers:

#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/types.h>
pmg
Yep, the missing headers where the problem, thank you.
theBlinker
If you hadn't cast in the first place, you'd have gotten a better warning :)
pmg