Question

I have tried to use Boost and had a segv after 3000 allocations of objects of size 24, now i started to use sys/ipc.h and sys/shm.h , i allocate 25 mio bytes (if i understand this properly)

it seems also to work properly on my linux box ipcs -m will show the allocated segment

0x000081bc 917516     testUser 644        25000000   0

sysctl -p will print

kernel.shmmax = 25500000

For some reason it works until the program reaches "43406 x 24 bytes" that's the place it will segv. I would be glad to get some hints where my problem lies. Please also note if this is the wrong way to allocate and use the shared memory with objects.

#define MAXMYMEM 25000000
int sharedMemId;
x *p_sharedMemory;
x *p_other;
sharedMemId = shmget(2232, MAXMYMEM, IPC_CREAT | 0644);

if(sharedMemId >= 0){

    p_sharedMemory = (x*) shmat( sharedMemId, 0 , 0);

    if(p_sharedMemory != ( x *)-1) {

        cout << sizeof(x) << endl;

        for(unsigned int i = 0 ; i < 1000000;i++ ){



            (p_sharedMemory + (sizeof(x) * i))->setTest(i);

        }
Was it helpful?

Solution

(p_sharedMemory + (sizeof(x) * i))->setTest(i);

Why are you using sizeof(x) here? Adding one to a pointer that points to an x will point at the next x, not the next byte. I suspect this is your problem.

change (p_sharedMemory + (sizeof(x) * i))->setTest(i); to

++p_sharedMemory;
p_sharedMemory->setTest(i);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top