title: Memory leak without the use of new operator in C++
body: <p>I am currently in the process of recreating a basic linux shell for my class and I'm having problems with valgrind detecting a memory leak even without the use of the new operator. I'm not sure where to look at and I can't make anything out of the valgrind error message. If anyone has any idea where the memory leak is coming from, that would be very appreciated. Thank you!</p>
  
  <pre><code>==26432== 
  ==26432== HEAP SUMMARY:
  ==26432==     in use at exit: 61 bytes in 2 blocks
  ==26432==   total heap usage: 268 allocs, 266 frees, 28,546 bytes allocated
  ==26432== 
  ==26432== 29 bytes in 1 blocks are possibly lost in loss record 1 of 2
  ==26432==    at 0x4C2B0E0: operator new(unsigned long) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
  ==26432==    by 0x4EF1208: std::string::_Rep::_S_create(unsigned long, unsigned long, std::allocator&lt;char&gt; const&amp;) (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.19)
  ==26432==    by 0x4EF1DCA: std::string::_Rep::_M_clone(std::allocator&lt;char&gt; const&amp;, unsigned long) (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.19)
  ==26432==    by 0x4EF1E63: std::string::reserve(unsigned long) (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.19)
  ==26432==    by 0x4EACF5E: std::basic_istream&lt;char, std::char_traits&lt;char&gt; &gt;&amp; std::getline&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt;(std::basic_istream&lt;char, std::char_traits&lt;char&gt; &gt;&amp;, std::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt;&amp;, char) (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.19)
  ==26432==    by 0x401895: main (in /home/randyhe/Documents/FALL2015/CS100/rshell/bin/rshell)
  ==26432== 
  ==26432== 32 bytes in 1 blocks are possibly lost in loss record 2 of 2
  ==26432==    at 0x4C2B0E0: operator new(unsigned long) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
  ==26432==    by 0x4EF1208: std::string::_Rep::_S_create(unsigned long, unsigned long, std::allocator&lt;char&gt; const&amp;) (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.19)
  ==26432==    by 0x4EF2930: char* std::string::_S_construct&lt;char const*&gt;(char const*, char const*, std::allocator&lt;char&gt; const&amp;, std::forward_iterator_tag) (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.19)
  ==26432==    by 0x4EF2D47: std::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt;::basic_string(char const*, std::allocator&lt;char&gt; const&amp;) (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.19)
  ==26432==    by 0x401826: main (in /home/randyhe/Documents/FALL2015/CS100/rshell/bin/rshell)
  ==26432== 
  ==26432== LEAK SUMMARY:
  ==26432==    definitely lost: 0 bytes in 0 blocks
  ==26432==    indirectly lost: 0 bytes in 0 blocks
  ==26432==      possibly lost: 61 bytes in 2 blocks
  ==26432==    still reachable: 0 bytes in 0 blocks
  ==26432==         suppressed: 0 bytes in 0 blocks
  ==26432== 
  ==26432== For counts of detected and suppressed errors, rerun with: -v
  </code></pre>
  
  <p>A portion of my code is below.</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;vector&gt;
  #include &lt;queue&gt;
  #include &lt;utility&gt;
  #include &lt;sstream&gt;
  #include &lt;string&gt;
  #include &lt;unistd.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;sys/wait.h&gt;
  #include &lt;errno.h&gt;
  #include &lt;limits.h&gt;
  #include &lt;pwd.h&gt;
  
  std::pair&lt;std::vector&lt;std::vector&lt;std::string&gt; &gt;, std::queue&lt;std::string&gt; &gt;
      parseInput(std::string s, bool&amp; b);
  void check(std::string singleWord, std::string keyword, bool&amp; b, int&amp; index,
          std::vector&lt;std::vector&lt;std::string&gt; &gt;&amp; v, std::queue&lt;std::string&gt;&amp; q);
  void executeAll(std::pair&lt;std::vector&lt;std::vector&lt;std::string&gt; &gt;,
      std::queue&lt;std::string&gt; &gt; p);
  void printCommands(const std::vector&lt;std::vector&lt;std::string&gt; &gt;&amp; v);
  void printConnectors(std::queue&lt;std::string&gt; q);
  bool executeSingle(std::vector&lt;std::string&gt;&amp; v);
  std::vector&lt;char*&gt; toCharPointers(std::vector&lt;std::string&gt;&amp; v);
  char* getHost();
  
  int main()
  {
      // Store hostname into character array object hostname
      char hostname[HOST_NAME_MAX];
      int status = gethostname(hostname, HOST_NAME_MAX);
      if(status != 0)
      {
          perror("gethostname failed");
          exit(errno);
      }
  
      // Store username to login object
      struct passwd *pws;
      pws = getpwuid(geteuid());
      if(pws == NULL)
      {
          perror("getid failed");
          exit(errno);
      }
      std::string login = pws-&gt;pw_name;
  
      for(;;)
      {
          // Output login and hostname
          std::cout &lt;&lt; login &lt;&lt; "@" &lt;&lt; hostname &lt;&lt; "$ ";
  
          // Get user input
          std::string userInput;
          std::getline(std::cin, userInput);
  
          // If user inputs exit, end program
          if(userInput == "exit")
              exit(0);
  
          // parsedPair holds the list of commands on its .first, and holds
          // the list of connectors on its .second
          bool correctSyntax = true;
          std::pair&lt;std::vector&lt;std::vector&lt;std::string&gt; &gt;, 
              std::queue&lt;std::string&gt; &gt; parsedPair = 
                  parseInput(userInput, correctSyntax);
  
          if(correctSyntax)
              executeAll(parsedPair);
      }
      return 0;
  }
  </code></pre>