title: Why do I get a segmentation fault for this?
body: <p>I just want to create a simple C++ struct that has an int index and an int grayValue .The function is given the vector with the gray values. When I try to compile it I get a segmentation fault, does anyone know why? (didn't sort the vector) Thank you.</p>
  <pre><code>vector&lt;gray&gt; createStruct( vector&lt;int&gt; grayValues)
  {
     vector &lt;gray&gt; grayStruct;
       for (int i = 0; i&lt;grayValues.size();i++)
        {
           grayStruct[i].originalIndex= i;
           grayStruct[i].grayValue= grayValues[i];
       }
  
       return grayStruct;
  }
  
  
  
  int main() {
  
    vector &lt;int&gt; grayVals={411,21,78,23};
    vector &lt;gray&gt; grayStruct=createStruct(grayVals);
  //  sort(grayStruct);
  
    for (int i = 0; i &lt; grayStruct.size(); i++)
        {cout &lt;&lt; grayStruct[i].originalIndex&lt;&lt;' '&lt;&lt;grayStruct[i].grayValue;
        cout&lt;&lt;endl;
      }
  return 0;
  
  }
  </code></pre>