title: Why did I get a Memory-related error here?
body: <p>I had a problem from a website. Given a <code>string</code> <code>s</code> and <code>st</code>, I have to found all possible combination of <code>st</code> in <code>s</code>. For example,</p>
  
  <pre><code>s      = "doomdogged"
  st     = "dg"
  answer = 4
  </code></pre>
  
  <p>I can choose the d from 0 or 4, and g from 6 or 7. Which gives me 4 possible combinations.</p>
  
  <p>Here's my code:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;vector&gt;
  
  using namespace std;
  
  string s, st;
  
  bool target[26];
  vector&lt;int&gt; positions[26];
  vector&lt;vector&lt;int&gt;&gt; possibleCombinations;
  
  void DFS_Enumeration(int, vector&lt;int&gt;*);
  int DFS_index_max = 0;
  
  int main(int argc, char *argv[])
  {
      int    answer = 0;
      cin &gt;&gt; s;  //Given a string s
      cin &gt;&gt; st; //Given a string st
      //Find all possible combination of st in s
      for ( int i = 0 ; i &lt; 26 ; ++ i )
          target[i] = 0;
      for ( int i = 0 ; i &lt; st.length() ; ++ i )
          target[st[i] - 97] = 1;
      for ( int i = 0 ; i &lt; 26 ; ++ i )
      {
          if ( target[i] == 0 ) continue;
          for ( int j = 0 ; j &lt; s.length() ; ++ j )
          {
              if ( s[j] == i + 97 ) positions[i].push_back(j);
          }
      }
      DFS_index_max = st.length();
      vector&lt;int&gt; trail(0);
      DFS_Enumeration(0, &amp;trail); //Here I got an runtime error
      for ( vector&lt;int&gt; vi : possibleCombinations )
      {
          int currentMax = 0;
          for ( int i = 0 ; i &lt; vi.size() ; ++ i )
          {
              if ( vi[i] &gt; currentMax )
              {
                  if ( i == vi.size() - 1 ) ++ answer;
                  currentMax = vi[i];
                  continue;
              }
              else
                  break;
          }
      }
      cout &lt;&lt; answer;
  }
  
  void DFS_Enumeration(int index, vector&lt;int&gt;* trail)
  {
      if ( index == DFS_index_max )
      {
          possibleCombinations.push_back(*trail);
          return;
      }
      for ( int i = 0 ; i &lt; positions[st[index] - 97].size() ; ++ i )
      {
          trail -&gt; push_back(positions[st[index] - 97][i]);
          DFS_Enumeration(++index, trail);
          trail -&gt; pop_back();
      }
      return;
  }
  </code></pre>
  
  <p>First I look for characters in <code>st</code>, and mark them as needed to found in my boolean array target.</p>
  
  <p>Then, I use DFS to enumerate all possible combinations. For the above example of "doomdogged" and "dg", d exists in 0, 4, 9. And g exist in 6, 7. I will get 06, 07, 46, 47, 96, 97.</p>
  
  <p>Lastly, I count those which make sense, and output the answer. For some reason, my code doesn't work and generate an runtime error concerning memory at the line I've marked.</p>