title: vector.push_back working with adding object pointer but not with adding object
body: <p>I am trying to implement a stack using a set of sub-stacks of fixed length. For the sub-stack, I use a simple array of fixed length initialized in the constructor. For the stack, I use a vector of sub-stacks.</p>
  
  <p>The below push method tries to allocate memory to a new substack and push it to the vector. But whenever a new push happens (let's say on vector setofstacks[1], the previous setofstacks[0] gets garbage values by the substack destructor being called. Not sure why this happens. </p>
  
  <pre><code>vector&lt;substack&lt;T&gt;&gt; setofstacks;
  </code></pre>
  
  <hr>
  
  <pre><code>substack&lt;T&gt; *s = new substack&lt;T&gt;(size);
  setofstacks.push_back(*s);
  setofstacks[currStackIndex].push(elem);
  </code></pre>
  
  <p>On the other hand, if I convert the object to object pointer of substack and store that in the vector, that works perfectly well. </p>
  
  <pre><code>vector&lt;substack&lt;T&gt;*&gt; setofstacks;
  </code></pre>
  
  <hr>
  
  <pre><code>setofstacks.push_back(new substack&lt;T&gt;(size));
  setofstacks[currStackIndex]-&gt;push(elem);
  </code></pre>
  
  <p><strong>Not working full code</strong></p>
  
  <pre><code>/*  Program: Implement a stack of substacks of fixed size
  *
  *   Date: 12/28/2015
  */
  
  #include &lt;iostream&gt;
  #include &lt;vector&gt;
  
  using namespace std;
  
  template &lt;class T&gt;
  class substack
  {
  private:
      T *arr;
      int nextAvailable;
      int size;
  public:
      substack(int capacity);
      ~substack();
      void push(T elem);
      T pop();
      T peek();
      bool isfull();
      bool isempty();
  };
  
  // Constructor initializing size of substack
  template &lt;class T&gt;
  substack&lt;T&gt;::substack(int capacity)
  {
      nextAvailable = 0;
      size = capacity;
      arr = new T[capacity];
  }
  
  // Destructor freeing memory allocated by the stack implemented using array
  template &lt;class T&gt;
  substack&lt;T&gt;::~substack()
  {
      delete[] arr;
      arr = NULL;
  }
  
  // Pushing an element on top of a substack of fixed size
  template &lt;class T&gt;
  void substack&lt;T&gt;::push(T elem)
  {
      if (nextAvailable &lt; size)
      {
          arr[nextAvailable] = elem;
          nextAvailable++;
      }
      else
      {
          cout &lt;&lt; "Substack is full" &lt;&lt; endl;
          return;
      }
  }
  
  // Popping the top element from a stack of fixed size
  template &lt;class T&gt;
  T substack&lt;T&gt;::pop()
  {
      nextAvailable--;
  
      if (nextAvailable &gt;= 0)
      {
          T del = arr[nextAvailable];
          arr[nextAvailable] = NULL;
          return del;
      }
      else
      {
          cout &lt;&lt; "Stack is empty" &lt;&lt; endl;
          return NULL;
      }
  }
  
  // Peeking the top element from a stack of fixed size
  template &lt;class T&gt;
  T substack&lt;T&gt;::peek()
  {
      if (nextAvailable &gt; 0)
      {
          return arr[nextAvailable - 1];
      }
      else
      {
          cout &lt;&lt; "Stack is empty" &lt;&lt; endl;
          return NULL;
      }
  }
  
  // Checking if a substack is full or not
  template &lt;class T&gt;
  bool substack&lt;T&gt;::isfull()
  {
      return (nextAvailable == size);
  }
  
  // Checking if a substack is empty or not
  template &lt;class T&gt;
  bool substack&lt;T&gt;::isempty()
  {
      return (nextAvailable == 0);
  }
  
  template &lt;class T&gt;
  class SetOfStacks
  {
  private:
      //vector&lt;substack&lt;T&gt;*&gt; setofstacks;
      vector&lt;substack&lt;T&gt;&gt; setofstacks;
      int currStackIndex;
      int size;
  public:
      SetOfStacks(int capacity);
      ~SetOfStacks();
      void push(T elem);
      T pop();
      T peek();
  };
  
  // Constructor initializing a set of stacks
  template &lt;class T&gt;
  SetOfStacks&lt;T&gt;::SetOfStacks(int maxStackCapacity)
  {
      currStackIndex = 0;
      size = maxStackCapacity;
      substack&lt;T&gt; *s = new substack&lt;T&gt;(maxStackCapacity);
      setofstacks.push_back(*s);
      //setofstacks.push_back(new substack&lt;T&gt;(maxStackCapacity));
  }
  
  // Destructor clearing set of stacks vector
  template &lt;class T&gt;
  SetOfStacks&lt;T&gt;::~SetOfStacks()
  {
      setofstacks.clear();
  }
  
  // Pushing an element on top of a stack implemented by a set of fixed length substacks
      template &lt;class T&gt;
      void SetOfStacks&lt;T&gt;::push(T elem)
      {
          if (!setofstacks[currStackIndex].isfull())
          {
              setofstacks[currStackIndex].push(elem);
          }
          else
          {
              currStackIndex++;
              substack&lt;T&gt; *s = new substack&lt;T&gt;(size);
              setofstacks.push_back(*s);
              //setofstacks.push_back(new substack&lt;T&gt;(size));
              setofstacks[currStackIndex].push(elem);
          }
      }
  
  // Popping an element from top of a stack implemented by a set of fixed length substacks
  template &lt;class T&gt;
  T SetOfStacks&lt;T&gt;::pop()
  {
      if (!setofstacks[currStackIndex].isempty())
      {
          return setofstacks[currStackIndex].pop();
      }
      else
      {
          setofstacks.pop_back();
          currStackIndex--;
  
          if (currStackIndex &gt;= 0)
          {
              return setofstacks[currStackIndex].pop();
          }
          else
          {
              cout &lt;&lt; "Stack is empty" &lt;&lt; endl;
              return NULL;
          }
      }
  }
  
  // Peeking an element from top of a stack implemented by a set of fixed length substacks
  template &lt;class T&gt;
  T SetOfStacks&lt;T&gt;::peek()
  {
      return setofstacks[currStackIndex].peek();
  }
  
  int main()
  {
      SetOfStacks&lt;int&gt; *s = new SetOfStacks&lt;int&gt;(5);
      s-&gt;push(1);
      s-&gt;push(2);
      s-&gt;push(3);
      s-&gt;push(4);
      s-&gt;push(5);
  
      s-&gt;push(6);
      s-&gt;push(7);
      s-&gt;push(8);
  
      cout &lt;&lt; s-&gt;pop() &lt;&lt; endl;
      cout &lt;&lt; s-&gt;pop() &lt;&lt; endl;
      cout &lt;&lt; s-&gt;pop() &lt;&lt; endl;
      cout &lt;&lt; s-&gt;pop() &lt;&lt; endl;
      cout &lt;&lt; s-&gt;pop() &lt;&lt; endl;
      cout &lt;&lt; s-&gt;pop() &lt;&lt; endl;
      cout &lt;&lt; s-&gt;pop() &lt;&lt; endl;
      cout &lt;&lt; s-&gt;pop() &lt;&lt; endl;
  
      s-&gt;push(1);
      s-&gt;push(2);
      s-&gt;push(3);
  
      cout &lt;&lt; s-&gt;pop() &lt;&lt; endl;
      cout &lt;&lt; s-&gt;pop() &lt;&lt; endl;
      cout &lt;&lt; s-&gt;pop() &lt;&lt; endl;
      //cout &lt;&lt; s-&gt;pop() &lt;&lt; endl;
  
      system("pause");
  }
  </code></pre>
  
  <p><strong>Output</strong></p>
  
  <pre><code>8
  7
  6
  -572662307
  -572662307
  -572662307
  -572662307
  -572662307
  3
  2
  1
  Press any key to continue . . .
  </code></pre>
  
  <p><strong>Working Code</strong></p>
  
  <pre><code>/*  Program: Implement a stack of substacks of fixed size
  *
  *   Date: 12/28/2015
  */
  
  #include &lt;iostream&gt;
  #include &lt;vector&gt;
  
  using namespace std;
  
  template &lt;class T&gt;
  class substack
  {
  private:
      T *arr;
      int nextAvailable;
      int size;
  public:
      substack(int capacity);
      ~substack();
      void push(T elem);
      T pop();
      T peek();
      bool isfull();
      bool isempty();
  };
  
  // Constructor initializing size of substack
  template &lt;class T&gt;
  substack&lt;T&gt;::substack(int capacity)
  {
      nextAvailable = 0;
      size = capacity;
      arr = new T[capacity];
  }
  
  // Destructor freeing memory allocated by the stack implemented using array
  template &lt;class T&gt;
  substack&lt;T&gt;::~substack()
  {
      delete[] arr;
      arr = NULL;
  }
  
  // Pushing an element on top of a substack of fixed size
  template &lt;class T&gt;
  void substack&lt;T&gt;::push(T elem)
  {
      if (nextAvailable &lt; size)
      {
          arr[nextAvailable] = elem;
          nextAvailable++;
      }
      else
      {
          cout &lt;&lt; "Substack is full" &lt;&lt; endl;
          return;
      }
  }
  
  // Popping the top element from a stack of fixed size
  template &lt;class T&gt;
  T substack&lt;T&gt;::pop()
  {
      nextAvailable--;
  
      if (nextAvailable &gt;= 0)
      {
          T del = arr[nextAvailable];
          arr[nextAvailable] = NULL;
          return del;
      }
      else
      {
          cout &lt;&lt; "Stack is empty" &lt;&lt; endl;
          return NULL;
      }
  }
  
  // Peeking the top element from a stack of fixed size
  template &lt;class T&gt;
  T substack&lt;T&gt;::peek()
  {
      if (nextAvailable &gt; 0)
      {
          return arr[nextAvailable - 1];
      }
      else
      {
          cout &lt;&lt; "Stack is empty" &lt;&lt; endl;
          return NULL;
      }
  }
  
  // Checking if a substack is full or not
  template &lt;class T&gt;
  bool substack&lt;T&gt;::isfull()
  {
      return (nextAvailable == size);
  }
  
  // Checking if a substack is empty or not
  template &lt;class T&gt;
  bool substack&lt;T&gt;::isempty()
  {
      return (nextAvailable == 0);
  }
  
  template &lt;class T&gt;
  class SetOfStacks
  {
  private:
      vector&lt;substack&lt;T&gt;*&gt; setofstacks;
      int currStackIndex;
      int size;
  public:
      SetOfStacks(int capacity);
      ~SetOfStacks();
      void push(T elem);
      T pop();
      T peek();
  };
  
  // Constructor initializing a set of stacks
  template &lt;class T&gt;
  SetOfStacks&lt;T&gt;::SetOfStacks(int maxStackCapacity)
  {
      currStackIndex = 0;
      size = maxStackCapacity;
      //substack&lt;T&gt; *s = new substack&lt;T&gt;(maxStackCapacity);
      setofstacks.push_back(new substack&lt;T&gt;(maxStackCapacity));
  }
  
  // Destructor clearing set of stacks vector
  template &lt;class T&gt;
  SetOfStacks&lt;T&gt;::~SetOfStacks()
  {
      setofstacks.clear();
  }
  
  // Pushing an element on top of a stack implemented by a set of fixed length substacks
  template &lt;class T&gt;
  void SetOfStacks&lt;T&gt;::push(T elem)
  {
      if (!setofstacks[currStackIndex]-&gt;isfull())
      {
          setofstacks[currStackIndex]-&gt;push(elem);
      }
      else
      {
          currStackIndex++;
          //substack&lt;T&gt; *s = new substack&lt;T&gt;(size);
          setofstacks.push_back(new substack&lt;T&gt;(size));
          setofstacks[currStackIndex]-&gt;push(elem);
      }
  }
  
  // Popping an element from top of a stack implemented by a set of fixed length substacks
  template &lt;class T&gt;
  T SetOfStacks&lt;T&gt;::pop()
  {
      if (!setofstacks[currStackIndex]-&gt;isempty())
      {
          return setofstacks[currStackIndex]-&gt;pop();
      }
      else
      {
          setofstacks.pop_back();
          currStackIndex--;
  
          if (currStackIndex &gt;= 0)
          {
              return setofstacks[currStackIndex]-&gt;pop();
          }
          else
          {
              cout &lt;&lt; "Stack is empty" &lt;&lt; endl;
              return NULL;
          }
      }
  }
  
  // Peeking an element from top of a stack implemented by a set of fixed length substacks
  template &lt;class T&gt;
  T SetOfStacks&lt;T&gt;::peek()
  {
      return setofstacks[currStackIndex].peek();
  }
  
  int main()
  {
      SetOfStacks&lt;int&gt; *s = new SetOfStacks&lt;int&gt;(5);
      s-&gt;push(1);
      s-&gt;push(2);
      s-&gt;push(3);
      s-&gt;push(4);
      s-&gt;push(5);
  
      s-&gt;push(6);
      s-&gt;push(7);
      s-&gt;push(8);
  
      cout &lt;&lt; s-&gt;pop() &lt;&lt; endl;
      cout &lt;&lt; s-&gt;pop() &lt;&lt; endl;
      cout &lt;&lt; s-&gt;pop() &lt;&lt; endl;
      cout &lt;&lt; s-&gt;pop() &lt;&lt; endl;
      cout &lt;&lt; s-&gt;pop() &lt;&lt; endl;
      cout &lt;&lt; s-&gt;pop() &lt;&lt; endl;
      cout &lt;&lt; s-&gt;pop() &lt;&lt; endl;
      cout &lt;&lt; s-&gt;pop() &lt;&lt; endl;
  
      s-&gt;push(1);
      s-&gt;push(2);
      s-&gt;push(3);
  
      cout &lt;&lt; s-&gt;pop() &lt;&lt; endl;
      cout &lt;&lt; s-&gt;pop() &lt;&lt; endl;
      cout &lt;&lt; s-&gt;pop() &lt;&lt; endl;
      cout &lt;&lt; s-&gt;pop() &lt;&lt; endl;
  
      system("pause");
  }
  </code></pre>
  
  <p><strong>Output</strong></p>
  
  <pre><code>8
  7
  6
  5
  4
  3
  2
  1
  3
  2
  1
  Stack is empty
  0
  Press any key to continue . . .
  </code></pre>
  
  <p>Please let me know what I am missing about using push_back while adding actual substack object so that it does not mess up the previous index substack in the vector. </p>
  
  <p>Not sure how is this a dupe of rule of three. Am I missing one of the three - copy constructor, assignment operator? Please help me understand. </p>