title: C++ passing vector<string> element to function causes SIGSEGV crash
body: <p>The problem is, as the title says, when I try to pass the second vector element to  the function "solveSide".This vector contains two string tokens, obtained from a bigger string with a user defined function.</p>
  
  <p>The program works like this:</p>
  
  <ul>
  <li>read line from file into a string</li>
  <li>call "stringSplit" function to obtain two tokens from that line string, and put them into a vector</li>
  <li>call "solveSide" for the first element in the vector</li>
  <li>call "solveSide" for the second element in the vector</li>
  </ul>
  
  <p>The problem appears in the second call to "solveSide", as the program crashes before entering the function body.</p>
  
  <p>If I delete the second call to the "solveSide" function, the program runs just fine.
  If I try to output the two elements of vector without deleting that line it doesn't ouptut anything, but with it deleted, it outputs both elements.</p>
  
  <p>In debug mode it throws SIGSEGV, when it calls "std::string::_M_mutate(unsigned int, unsigned int, unsigned int)" (as shown by the call stack).</p>
  
  <p>Crash Code:</p>
  
  <pre><code>ifstream fin("ecuatii.in");
  string ecuatie;
  vector&lt;string&gt; sideSplit;
  
  fin&gt;&gt;ecuatie;
  sideSplit = stringSplit(ecuatie, "=");
  fout&lt;&lt;sideSplit[0]&lt;&lt;"="&lt;&lt;sideSplit[1]&lt;&lt;"\n";
  solveSide(sideSplit[0], leftCoeffSum, leftSum);
  solveSide(sideSplit[1], rightCoeffSum, rightSum);
  </code></pre>
  
  <p>stringSplit function:</p>
  
  <pre><code>vector&lt;string&gt; stringSplit(string str, const char *delim){
      char *strCopy = new char [str.size()+1];
      char *token;
      char **tokens = new char* [(str.size()+1)*2];
      unsigned tokenCount = 0;
  
      strcpy(strCopy, str.c_str());
      token = strtok(strCopy, delim);
      while(token != NULL){
          tokens[tokenCount++] = token;
          token = strtok(NULL, delim);
      }
  
      vector&lt;string&gt; splits(tokenCount);
      for(unsigned i = 0; i &lt; tokenCount; ++i){
           splits[i] = "";
           splits[i] += tokens[i];
      }
  
      delete[] strCopy;
      for(unsigned i = 0; i &lt; tokenCount; ++i){
           delete[] tokens[i];
      }
      delete[] tokens;
  
      return splits;
  }
  </code></pre>
  
  <p>stringChunkToFloat function(should've used "substr()", now I know):</p>
  
  <pre><code>float stringChunkToFloat(string str, unsigned posStart, unsigned posEnd){
      char numberStr[20];
      for(unsigned i = posStart; i &lt;= posEnd; ++i){
          numberStr[i-posStart] = str[i];
      }
      numberStr[posEnd-posStart+1] = '\0';
  
      float number = atof(numberStr);
  
      return number;
  </code></pre>
  
  <p>}</p>
  
  <p>solveSide function:</p>
  
  <pre><code>void solveSide(string &amp;side, float &amp;coeffSum, float &amp;sum){
      coeffSum = 0;
      sum = 0;
  
      unsigned posStart = side.find_first_of("+-", 1);
      unsigned posEnd;
      float currentNumber;
  
      if(side.size() == 1){
          if(side[0] == 'x'){
              coeffSum = 1;
          }else{
              sum = atof(side.c_str());
          }
          return;
      }
  
      if(side[0] == 'x'){
          coeffSum += 1;
      }else if(side[1] == 'x' &amp;&amp; side[0] == '-'){
          coeffSum -= 1;
      }else if(side[posStart-1] == 'x'){
          currentNumber = stringChunkToFloat(side, 0, posStart-2);
          coeffSum += currentNumber;
      }else{
          currentNumber = stringChunkToFloat(side, 0, posStart-1);
          sum += currentNumber;
      }
  
      while(posStart != string::npos &amp;&amp; posStart != side.size()){
          posEnd = side.find_first_of("+-", posStart+1);
          if(posEnd == string::npos){
              posEnd = side.size();
          }
  
          if(side[posStart+1] == 'x'){
              if(side[posStart] == '+'){
                  coeffSum += 1;
              }else{
                  coeffSum -= 1;
              }
          }else if(side[posEnd-1] == 'x'){
              currentNumber = stringChunkToFloat(side, posStart, posEnd-2);
              coeffSum += currentNumber;
          }else{
              currentNumber = stringChunkToFloat(side, posStart, posEnd-1);
              sum += currentNumber;
          }
  
          posStart = posEnd;
      }
  }
  </code></pre>
  
  <p>Input string : <code>2.0x-4+5x+300=98x</code></p>