title: c++ Segmentation Fault using struct pointer
body: <p>I have a class called Dictionary that contains a key-value pair, a pointer to this key-value pair, and an int that holds the size of the dictionary.</p>
  
  <pre><code>template&lt;typename K, typename V&gt;
  class Dictionary
  {
  public:
  
  V&amp; operator[](K key);
  
  private:
  
  struct KeyValue
  {
      K key;
      V value;
  }; //the key-value pair struct
  
  KeyValue* array; //pointer to an array of items (the key-value pairs)
  
  int size; //size of the dictionary (i.e. the array size)
  };
  </code></pre>
  
  <p>I am trying to overload the [] operator for this class and when I do so, I get a segmentation fault error</p>
  
  <pre><code>template&lt;typename K, typename V&gt;
  V&amp; Dictionary&lt;K,V&gt;::operator[](K key){
    for (size_t i = 0; i &lt; size; i++) {
      if (key == array[i].key) {
        return array[i].value;
      }
    }
    array[size].value = 0;
    size++;
    return array[size-1].value;
  }
  </code></pre>
  
  <p>I beleive that the seg fault is occuring in this line</p>
  
  <pre><code>array[size].value = 0;
  </code></pre>
  
  <p>However, I don't know exactly why it's happening. Any help is greatly appreciated. Thanks!</p>