title: Segmentation fault while trying to insert objects to nullpointers array
body: <p>This is a part of a class I need to implement.</p>
  
  <pre><code>template &lt;class Element, class Compare = std::equal_to&lt;Element&gt;&gt;
  class UniqueArray {
      Element** data;
      unsigned int size;
      unsigned int max_size;
  public:
      explicit UniqueArray(unsigned int size);
      UniqueArray(const UniqueArray&amp; other);
      ~UniqueArray();
      unsigned int insert(const Element&amp; element);
  </code></pre>
  
  <p>I can't use STL (only exception is <code>std::equal_to</code>).</p>
  
  <p>I implemented the constructors as follows : </p>
  
  <pre><code>template &lt;class Element, class Compare&gt;
  UniqueArray&lt;Element, Compare&gt;::UniqueArray(unsigned int size):
      data(new Element* [size]), size(0), max_size(size){
  }
  
  
  template &lt;class Element, class Compare&gt;
  UniqueArray&lt;Element, Compare&gt;::UniqueArray(const UniqueArray&amp; other):
      data(new Element *[other.size]), size(other.size), max_size(other.max_size){
      for(int i = 0; i &lt; size; i++){
          *data[i] = *other.data[i];
      }
  }
  </code></pre>
  
  <p><code>data</code> is actually an array of pointers. 
  each pointer points to an Element.</p>
  
  <p>This is my implementation of <code>insert</code> function which causes <code>SEGFAULT</code></p>
  
  <pre><code>template &lt;class Element, class Compare&gt;
  unsigned int UniqueArray&lt;Element, Compare&gt;::insert(const Element&amp; element){
      if (size &gt;= max_size){
          throw UniqueArrayIsFullException();
      }
      for(int i = 0; i &lt; size; i++){
          if((Compare(), *data[i]) == element){
              return i;
          }
      }
  
      *data[size++] = element;
      return size;
  }
  </code></pre>
  
  <p>The test im running is this : </p>
  
  <pre><code>static void testInt() {
      typedef UniqueArray&lt;int, std::equal_to&lt;int&gt;&gt; intUA;
      // Test ctor
      unsigned int size = 4;
      intUA ua(size);
      assert(ua.getSize() == size);
      assert(ua.getCount() == 0);
      // Test insert
      ua.insert(4);
      unsigned int index = ua.insert(1);
      ua.insert(5);
      assert(ua.insert(1) == index);
      ua.insert(2);
      assert(ua.getCount() == 4);
      ua.insert(2);
      // Test UniqueArrayIsFullException
      try {
          ua.insert(3);
      }
      catch (intUA::UniqueArrayIsFullException&amp; e) {
          return;
      }
      assert(0);
  }
  </code></pre>
  
  <p>Test fails at the first call to <code>insert</code> and points that the problem is in line <code>*data[size++] = element;</code></p>