title: Access violation reading - vector of string pointers to value in vector of strings
body: <p>I'm not very experienced programmer in C++ and I have a problem which I can't resolve. The project on which I'm working is quite big so I can't post here all codes. It is too much code and too much explanation. I write just little part of code, the part which causes me problem, so I hope it is enough. Sorry for the long of my question but I want explain all posted code. Maybe this part of code isn't enough to solve the problem but I want to try it.</p>
  
  <p>First I have a struct called "record":</p>
  
  <pre><code>struct record {
      vector&lt;string&gt; dataRow;
      vector&lt;string *&gt; keys;
      vector&lt;string *&gt; values;
  
      void setDataRow(vector&lt;string&gt; r) {
           dataRow = r;
      }
  }
  </code></pre>
  
  <p>Some of string data are marked as keys and others as values. I next processing is better for me to have all string data in one vector, so that's the reason why I don't have two vectors of string (vector keys, vector values).</p>
  
  <p>Then I have this:</p>
  
  <pre><code>vector&lt; vector&lt;record&gt; &gt; resultSet;
  </code></pre>
  
  <p>vector is like data table - set of lines with string data. I need specific count of these tables, therefore vector of vectors of records. The count of tables is optional, so when I set table count I prepare tables by reserve function:</p>
  
  <pre><code>resultSet.reserve(count);
  for(unsigned int i = 0; i &lt; count; i++) {
      vector&lt;record&gt; vec;
      resultSet.push_back(vec);
  }
  </code></pre>
  
  <p>When I want add new record to resultSet I know the number of table to which I need insert record. After resultSet[number].push_back(rec) I need change pointers in vectors "keys" and "values" because push_back() creates new copy of "rec" with values of "dataRow" in other memory addresses, right? So I have this function which does push_back and updates pointers:</p>
  
  <pre><code>void insert(int part, vector&lt;string&gt; &amp; dataRow) {
      record r;
      r.setDataRow(dataRow);
  
      resultSet[part].push_back(r);
      int pos = resultSet.size() - 1; // position of last record
      resultSet[part].at(pos).values.clear();
      resultSet[part].at(pos).keys.clear();
  
      for(unsigned int i = 0; i &lt; dataRow.size(); i++) {
          record * newRec = &amp;resultSet[part].at(pos);
          if(isValue(dataRow[i])) {
              newRec-&gt;values.push_back(&amp;(newRec-&gt;dataRow.at(i)));
              // control cout...
          } else {
              newRec-&gt;keys.push_back(&amp;(newRec-&gt;dataRow.at(i)));
              // control cout...
          }
      }
  }
  </code></pre>
  
  <p>This is working. After push_back in newRec I did control cout of inserted pointers and their referenced values, and everything was ok.</p>
  
  <p>But! After some inserts I call function processData(resultSet), which has to process all data in resultSet. Before implementing processing od data I just wanted print all keys for control to find out if everything is alright. This code:</p>
  
  <pre><code>for(unsigned int i = 0; i &lt; resultSet.size(); i++) {
      for(unsigned int j = 0; j &lt; resultSet[i].size(); j++) {
          cout &lt;&lt; "keys: ";
          for(unsigned int k = 0; k &lt; resultSet[i].at(j).keys.size(); k++) {
              cout &lt;&lt; *resultSet[i].at(j).keys.at(k) &lt;&lt; ", ";
          }
          cout &lt;&lt; endl;
      }
  }
  </code></pre>
  
  <p>is bad (Same problem with printing values vector of record). It throws exception of Access violation reading. I know that this exception is thrown when I want to read unaccessible memory, right? Please, tell me that I have mistake in code written above because I really don't know why it doesn't work. Before processing resultSet I do nothing with resultSet except some count of inserts.</p>
  
  <p>Thank you for reading and possible answers.</p>