title: Memory leak because of new() or malloc
body: <p>I'm trying to implement an alternative to usual contiguous dynamic arrays, where I am using a vector of pointers, each is pointing to a constant size array, referring to it as XVector.</p>
  
  <p>It works fine for a certain limit of inputs - say 150 elements -, but beyond that it starts throwing exceptions.</p>
  
  <p>I tried to use "new and delete" instead of "malloc and free", it did increased the limit - say about 1200 elements -, but still exists the same problem.</p>
  
  <p>I'm using C++.</p>
  
  <p>Here's my main program:</p>
  
  <pre><code>XVector&lt;int&gt; xv;
  
  for(int i=0;i&lt;1210;i++){
      int temp = rand()%100;
      xv.pushBack(temp);
  }
  
  
  for(int i=0;i&lt;xv.size();i++){
      cout&lt;&lt;xv.getValue(i)&lt;&lt;" ";
  }
  
  cout&lt;&lt;"\n\n"&lt;&lt;xv.capacity();
  return 0;
  </code></pre>
  
  <p>And here's is the XVector (The class of theD header file:</p>
  
  <pre><code>private:
      const int b = 10;
      vector&lt;T*&gt; arr;
      int currentIndex;
      int maxSize;
  
  
  protected:
      void expand(){
          T* temp = new T[b];
          arr.push_back(temp);
          maxSize+=(b);
  
      }
  
      void shrink(){
          delete[] arr[arr.size()-1];
          arr[arr.size()-1] = NULL;
          arr.pop_back();
          maxSize-=(b);
      }
  
      int ceil(float num) {
          int inum = (int)num;
          if (num == (float)inum) {
              return inum;
          }
          return inum + 1;
      }
  
      pair&lt;int,int&gt; position(int index){
          pair&lt;int,int&gt; pos;
          float result = ((float)index/b);
          pos.first = result; //Row #
          pos.second = ceil((result - pos.first)*b); //Exact cell in the row
          return pos;
      }
  
  
  
      public:
  
      XVector(){
          currentIndex=0;
          maxSize=b;
          arr.reserve(120);
          arr.push_back(new T[b]);
      }
  
      void pushBack(T value){
          if(currentIndex&gt;=maxSize-1){expand();}
          pair&lt;int,int&gt; indeces = position(currentIndex);
          arr[indeces.first][indeces.second]=value;
          currentIndex++;
      }
  
      void popBack(){
          currentIndex--;
          if(maxSize&gt;=currentIndex+(2*b)){shrink();}
      }
  
      T getValue(int index){
          pair&lt;int,int&gt; indeces = position(index);
          return arr[indeces.first][indeces.second];
      }
  
      void setValue(int index,T value){
          pair&lt;int,int&gt; indeces = position(index);
          arr[indeces.first][indeces.second] = value;
      }
  
      int capacity(){
          return maxSize;
      }
  
      int size(){
          return currentIndex;
      }
  
      bool empty(){
          return currentIndex==0?true:false;
      }
  </code></pre>
  
  <p>PS: I tried to use Valgrind, but failed to identify the exact problem.</p>