title: Why do I get access violation when trying to use a map
body: <p>I have a class here meant to log where memory is mapped. When I set a specific location on the map to some number, it give me an access violation.</p>
  
  <p>The exact error is Exception thrown at 0x003A5B4B in memorymapperexample.exe: 0xC0000005: Access violation reading location 0x00000004.</p>
  
  <p>This is my class. Addpair and clearlocation give errors when called. The map key is a void pointer and the map value is a int. I don't understand why this simple thing is not working.</p>
  
  <pre><code>#include &lt;map&gt;
  #include &lt;unordered_map&gt;
  #include &lt;string&gt;
  #include &lt;iostream&gt;
  #include &lt;memory&gt;
  
  using namespace std;
  
  
  template &lt;typename T&gt;
  class MallocAllocator
  {
  private:
      map&lt;void *, int&gt; ord;
      int total;
  public:
      typedef T value_type;
      MallocAllocator() { }
      template &lt;typename U&gt; MallocAllocator(const MallocAllocator&lt;U&gt;&amp; other) {}
      T* allocate(size_t count)
      {
          return (T*)malloc(count * sizeof(T));
      }
      void deallocate(T* object, size_t n)
      {
          void* ptr = reinterpret_cast&lt;void*&gt;(object);
          free(ptr);
  
      }
      void addpair(void* a, int b)
      {
          ord[a] = b; //ERROR HERE
      }
  
      void clearlocation(void * a)
      {
          ord[a] = 0; //ERROR HERE TOO
      }
  };
  
  MallocAllocator&lt;void*&gt; memoryManager;
  
  void* operator new(size_t size)
  {
      cout &lt;&lt; "Allocating memory..." &lt;&lt; endl;
      auto newObject = memoryManager.allocate(size);
  
      memoryManager.addpair(newObject, size);
      memoryManager.clearlocation(newObject);
      return newObject;
  }
  
  void operator delete(void* objectPtr) noexcept
  {
      cout &lt;&lt; "DEAllocating memory..." &lt;&lt; endl;
      void** ptr = reinterpret_cast&lt;void**&gt;(objectPtr);
      memoryManager.deallocate(ptr, 0);
      //free(objectPtr);
  }
  
  
  
  int main()
  {
      int * ima = new int(99);
      delete ima;
      return 1;
  }
  </code></pre>