Map in Shared memory
<p>I am trying to create an unordered_map in shared memory. I am using allocator to server the purpose.</p>

<p>The code</p>

<pre><code>void *addr;
void *pool;
int shmid;

template&lt;class T&gt;
class MyPoolAlloc {
private:
public:
    typedef size_t     size_type;
    typedef ptrdiff_t  difference_type;
    typedef T*         pointer;
    typedef const T*   const_pointer;
    typedef T&amp;         reference;
    typedef const T&amp;   const_reference;
    typedef T          value_type;

   template&lt;class X&gt;
   struct rebind
   { typedef MyPoolAlloc&lt;X&gt; other; };

   MyPoolAlloc() throw() {
   }
   MyPoolAlloc(const MyPoolAlloc&amp;) throw()  {
   }

   template&lt;class X&gt;
   MyPoolAlloc(const MyPoolAlloc&lt;X&gt;&amp;) throw() {
   }

   ~MyPoolAlloc() throw() {
   }

  pointer address(reference __x) const { return &amp;__x; }

  const_pointer address(const_reference __x) const { return &amp;__x; }

  pointer allocate(size_type __n, const void * hint = 0) {
      pointer tmp = static_cast&lt;T*&gt;(addr);
      addr = (void*)((char*)addr + __n);
      return tmp;
  }

  void deallocate(pointer __p, size_type __n) {
      // pMyPool-&gt;Free(reinterpret_cast&lt;void *&gt;(__p));
  }

 size_type max_size() const throw() {
    //return size_t(-1) / sizeof(T);
  }

 void construct(pointer __p, const T&amp; __val) {
     ::new(__p) T(__val);
  }

 void destroy(pointer __p) {
    //__p-&gt;~T();
  }
};

typedef std::unordered_map&lt;int, int, std::hash&lt;int&gt;, std::equal_to&lt;int&gt;,  MyPoolAlloc&lt;std::pair&lt;const int,int&gt;&gt; &gt; Map;

int main ()
{
    shmid = shmget(shm_key, 1000, IPC_CREAT | IPC_EXCL | 644);
    if(shmid == -1){
            std::cerr &lt;&lt; "Failed to create the shared segment." &lt;&lt; strerror(errno)&lt;&lt; std::endl;
            exit(-1);
    }

    addr = shmat(shmid, NULL, 0);
    pool = addr;
    if(addr == (void*)-1){
            std::cerr &lt;&lt; "Failed to attach the segment to the process." &lt;&lt; std::endl;
            shmctl(shmid, IPC_RMID, 0);
            exit(-1);
    }

    Map *m = new(pool) Map;

    m-&gt;insert(std::pair&lt;int, int&gt;(2,4));

    shmdt(addr);
    shmctl(shmid, IPC_RMID, 0);

    return 0;
}
</code></pre>

<p>I am allocating memory for the map on shared memory address. And I suppose for the elements the allocator class' allocate() function will be used. But I am not able to figure out how to use allocate function to allocate memory for the elements of the map.
I am getting Arithmetic Exception at the line where I am trying to insert in the map.</p>

<p>Could someone please help to understand how the memory layout should be?</p>

<p>Thanks. </p>
