1

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);

        }
Oliver
  • 928
  • 9
  • 25

1 Answers1

2

(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);
JimR
  • 15,513
  • 2
  • 20
  • 26
  • Hey JimR thanks for your quick response. I have checked your hint, actually cool it works! Now if i may ask what is the difference? I assumed that this would be exactly the same. Thanks for giving me a url or an explanation so ill get the background. – Oliver Feb 21 '12 at 20:38
  • When you do math with a pointer, such as '++pointer` the pointer is increased by sizeof( *pointer ) not 1 byte. Here is a [decent explanation](http://www.cs.umd.edu/class/sum2003/cmsc311/Notes/BitOp/pointer.html) – JimR Feb 21 '12 at 20:43
  • Now i understand i actually jumped 24 * type x instead of 24 bytes, thanks alot JimR – Oliver Feb 21 '12 at 20:53