title: std::vector of objects and sorting results in segmentation faults
body: <p>I been sitting over this problem quiet a few hours now and can not find a solution. I try to get rid of some Custom Array classes and want to change them to std::vector.</p>
  
  <p>I have a class member declared like this:</p>
  
  <pre><code>std::vector&lt;BaseCluster *&gt; baseClusters;
  </code></pre>
  
  <p>In the class constructor I do not initialize the vector to a certain size. I leave it as is, uninitialized. Before, I tried to initialize if to a certain size (to gain some performance and prevent many reallocations), but that gave me problems accessing the elements in a loop (eg. printing them out). I guess it is due to the initialization of the objects with the default constructor (of type BaseCluster) as NULL-Objects ??</p>
  
  <p>However, in a loop I fill the vector with objects like this </p>
  
  <pre><code>baseClusters.push_back(new BaseCluster(current, score));
  </code></pre>
  
  <p>After the loop finishes I do some cleanup of objects that are not interesting:</p>
  
  <pre><code>for (unsigned int i = 0; i &lt; baseClusters.size(); i++) {
      // --- testing   
      if (baseClusters.at(i) == NULL) {
         wcout &lt;&lt; i &lt;&lt; ". object is NULL" &lt;&lt; endl;
      }
      if (
        ((BaseCluster *) baseClusters.at(i))-&gt;getNode()-&gt;getSuffixedDocumentsCount() &lt; minimalGroupSize
      ) {
          baseClusters.erase(baseClusters.begin() + i);
      }
  
      if (i &gt;= noMoreBaseClustersThan) {
          baseClusters.erase(baseClusters.begin() + i);
      }
  }
  </code></pre>
  
  <p>Now I sort the vector (should be sorted by score descending) and here I have a problem:</p>
  
  <p>1.) using the sort as</p>
  
  <pre><code>sort(baseClusters.begin(), baseClusters.end()); 
  </code></pre>
  
  <p>doesn't sort at all, also my implemented "operators&lt;" or "operator>" in the BaseCluster class are not touched at all. The operators are public and look like this.</p>
  
  <pre><code>bool operator&lt; (const BaseCluster * rhs) const {
      return m_score &lt; rhs-&gt;m_score;
  }
  
  bool operator&gt; (const BaseCluster * rhs) const {
      return m_score &gt; rhs-&gt;m_score;
  }
  </code></pre>
  
  <p>2.) Using a predicate / functor</p>
  
  <pre><code>class BaseClusterComparator {
  public:
      bool operator() (const BaseCluster * a, const BaseCluster * b) const {
          wcout &lt;&lt; "BaseClusterComparator" &lt;&lt; endl;
          wcout &lt;&lt; a-&gt;getScore() &lt;&lt; " &lt;&gt; " &lt;&lt; b-&gt;getScore() &lt;&lt; endl;
          if (a-&gt;getScore() &gt; b-&gt;getScore()) {
              return -1;
          } else if (a-&gt;getScore() &lt; b-&gt;getScore()) {
              return 1;
          } else {
              return 0;
          }
      }
  };
  </code></pre>
  
  <p>the class member getScore looks like this</p>
  
  <pre><code>float
  BaseCluster::getScore() const {
      return m_score;
  }
  </code></pre>
  
  <p>and executing the sort with</p>
  
  <pre><code>sort(baseClusters.begin(), baseClusters.end()), BaseClusterComparator());
  </code></pre>
  
  <p>will result in an segmentation fault when trying to access the getScore() method after sorting about 60 - 70 objects.</p>
  
  <pre><code>Program received signal SIGSEGV, Segmentation fault.
  0x00000000004773ec in BaseCluster::getScore (this=0x2ef1) at algorithm/BaseCluster.cpp:44
  44      return m_score;
  </code></pre>
  
  <p>I have the feeling that due to reserving / resizing of the vector there are NULL-objects (constructed with the default constructor of BaseClass ?!). </p>
  
  <p>And now am stuck on how to solve the problem. What am I doing wrong here? I been reading through many other related stackoverflow questions before asking here, followed them by changing here and there but no way out of the seg. faults. As far as I am aware I do not insert any Null-Objects (tested that in the loop), so there must be another flaw in my code. Thought it was an quiet easy task changing to a vector of pointers, but seems more complicated now...</p>
  
  <p>Please, anybody help me on this issue. Thanks so much in advance!</p>