title: new operator overwriting an existing object
body: <p>I have a custom FastStack class, implemented as a fixed size array and an index into that array.</p>
  
  <p>In my copy constructor, I allocate the array and then assign each object from the copy's array into the new array. There's some refcounting in the objects on the stack, hence assignment is used rather than a simple copy.</p>
  
  <p>The problem is that when allocating the array, it sometimes overwrites part of the other stack's array. As can be expected, this leads to eventual segmentation faults when that data is dereferenced.</p>
  
  <pre><code>class FastStack {
  private:
      int m_size, m_ptr;
      ObjectRef* m_stack;
  
  public:
      FastStack(int size) : m_size(size), m_ptr(-1) {
          m_stack = new ObjectRef[m_size];
      }
  
      FastStack(const FastStack&amp; copy) : m_size(copy.m_size), m_ptr(copy.m_ptr) {
          long a = (long)copy.m_stack[0];
  
          m_stack = new ObjectRef[m_size];
  
          if ((long)copy.m_stack[0] != a)
              fprintf(stderr, "\nWe have a serious problem!\n\n");
  
          for (int i = 0; i &lt;= m_ptr; i++)
              m_stack[i] = copy.m_stack[i];
      }
  
      ~FastStack() {
          delete[] m_stack;
      }
  };
  
  class ObjectRef {
  private:
      DataObj* m_obj;
  
  public:
      ObjectRef() : m_obj(0) { }
  
      ObjectRef(DataObj* obj) : m_obj(obj) {
          if (m_obj) m_obj-&gt;addRef();
      }
  
      ObjectRef(const ObjectRef&amp; obj) : m_obj(obj.m_obj) {
          if (m_obj) m_obj-&gt;addRef();
      }
  
      ~ObjectRef() {
          if (m_obj) m_obj-&gt;delRef();
      }
  
      ObjectRef&amp; operator=(DataObj* obj) {
          if (obj) obj-&gt;addRef();
          if (m_obj) m_obj-&gt;delRef();
          m_obj = obj;
          return *this;
      }
  
      ObjectRef&amp; operator=(const ObjectRef&amp; obj) {
          if (obj.m_obj) obj.m_obj-&gt;addRef();
          if (m_obj) m_obj-&gt;delRef();
          m_obj = obj.m_obj;
          return *this;
      }
  };
  </code></pre>
  
  <p>I see that "We have a serious problem!" line shortly before a segfault, and stepping through it with gdb I can see that one of the ObjectRefs created by new has the same address as the other stack's array.</p>
  
  <p>My first instinct is to say that new should never be allocating memory that is already in use, but that clearly seems to be the case here and I am at a complete loss as to what can be done.</p>
  
  <p>Added: At the time that I see this happen, m_size = 2 and m_ptr = 0.</p>
  
  <p>The full code for this program is on github at <a href="https://github.com/dvpdiner2/pycdc" rel="nofollow">https://github.com/dvpdiner2/pycdc</a> but is quite convoluted and difficult to follow. </p>