title: Custom allocator without pointer arithmetic
body: <p>I have a custom allocator class which looks something like this:</p>
  
  <pre><code>class allocator {
  private:
    char* memory;
    std::ptrdiff_t offset;
    std::size_t total_size;
  
  public:
    allocator(std::size_t size) : total_size(size), offset(0) {
      memory = new char[size];
    }
    void* allocate(std::size_t size, std::size_t alignment) {
      // unimportant stuff
  
      void* current_address = &amp;start_ptr[offset]; // --&gt; OUCH, pointer arithmethic
  
      offset += size;
      // unimportant stuff
      return current_address;
    }
  
  }
  </code></pre>
  
  <p>As you can see above I'm using pointer arithmetic to calculate the current address for the newly allocated block of memory. The <a href="https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#r2-in-interfaces-use-raw-pointers-to-denote-individual-objects-only" rel="nofollow noreferrer">CppCoreGuidelines</a> and many other guidelines discourage the use of pointer arithmethic. So is there another way of managing the memory pool?</p>
  
  <p>I was thinking of maybe using a <code>std::vector&lt;char&gt;</code>, as it contains a contiguous block of memory, and doing something like this:</p>
  
  <pre><code>std::vector&lt;char&gt; memory;
  
  void* current_address = &amp;(memory.at(offset));
  </code></pre>
  
  <p>But this doesn't seem any nicer to me. Do you have any ideas of how to cleanly manage the memory pool in a safe way?</p>