title: Memory mapped file storage in stl vector
body: <p>I'm trying to implement custom <code>allocator</code> for storing memory mapped files in the <code>std::vector</code>. Files mapping performed by <code>boost::iostreams::mapped_file</code></p>
  
  <p>Allocator type for file memory mapping:</p>
  
  <pre><code>template&lt;typename T&gt;
  class mmap_allocator 
  {
  public:
    typedef T value_type;
  
    mmap_allocator(const std::string&amp; filename) 
    : _mmfile(filename) {  } 
  
    T* allocate (size_t n) 
    { 
       return reinterpret_cast&lt;T*&gt;(_mmfile.data());
    }
    void deallocate (T* p, size_t n) 
    { 
       p = nullptr;
       _mmfile.close();
    }
  
  private:
    boost::iostreams::mapped_file _mmfile;
  };
  </code></pre>
  
  <p>Container for memory mapped file, based on <code>std::vector</code>:</p>
  
  <pre><code>//Get file size
  long GetFileSize(std::string filename)
  {
      FILE *p_file = NULL;
      p_file = fopen(filename.c_str(),"rb");
      fseek(p_file,0,SEEK_END);
      int size = ftell(p_file);
      fclose(p_file);
      return size;
  }
  
  template&lt;typename T&gt;
  class mm_vector : public std::vector&lt;T, mmap_allocator&lt;T&gt; &gt;
  {
  public:
    typedef mmap_allocator&lt;T&gt; allocator_type;
    typedef std::vector&lt;T, allocator_type &gt; b_vector;
  
    mm_vector(const std::string filename) : b_vector(GetFileSize(filename)/sizeof(T), allocator_type(filename)) 
    {  
      b_vector::reserve(GetFileSize(filename)/sizeof(T));
    }
  };
  </code></pre>
  
  <p>Test code:</p>
  
  <pre><code>int main()
  {
    mm_vector&lt;int&gt; v("test.f");//test.f - binary file contain several integers
    for(auto x : v) std::cout&lt;&lt;x&lt;&lt;"  ";
  }
  </code></pre>
  
  <p>This code don't work properly - output always equals to zero. File contains correct content - several integers. This code works well:</p>
  
  <pre><code>boost::iostreams::mapped_file _mmfile("test.f");
  int* p = (int*)(_mmfile.data());
  std::cout&lt;&lt;p[0];
  </code></pre>
  
  <p>What am I doing wrong?</p>