title: C++ Exception: bad_alloc at memory location
body: <blockquote>
    <p><strong>Unhandled exception at at 0x7650C41F in binary.exe: Microsoft C++ exception: std::bad_alloc at memory location 0x003EEE00.</strong></p>
  </blockquote>
  
  <pre><code>First-chance exception at 0x77983AB3 (ntdll.dll) in binary.exe: 0xC0000005: Access violation reading location 0x6F726369.
  First-chance exception at 0x7650C41F in binary.exe: Microsoft C++ exception: std::bad_alloc at memory location 0x003DF0DC.
  First-chance exception at 0x7650C41F in binary.exe: Microsoft C++ exception: std::bad_alloc at memory location 0x003DF0DC.
  First-chance exception at 0x77983AB3 (ntdll.dll) in binary.exe: 0xC0000005: Access violation reading location 0x6F726369.
  First-chance exception at 0x7650C41F in binary.exe: Microsoft C++ exception: std::bad_alloc at memory location 0x003DEA40.
  First-chance exception at 0x7650C41F in binary.exe: Microsoft C++ exception: std::bad_alloc at memory location 0x003DEA40.
  First-chance exception at 0x7650C41F in binary.exe: Microsoft C++ exception: [rethrow] at memory location 0x00000000.
  First-chance exception at 0x7650C41F in binary.exe: Microsoft C++ exception: [rethrow] at memory location 0x00000000.
  Unhandled exception at at 0x7650C41F in binary.exe: Microsoft C++ exception: std::bad_alloc at memory location 0x003DEA40.
  The program '[7632] binary.exe' has exited with code 0 (0x0).
  </code></pre>
  
  <p>I don't know if I'm making a newbie mistake, but every time I try to run the code below I get the errors listed above - from what I can gather through various forum posts and the error messages, there is an issue with the allocation of memory but that's as far as i've got.</p>
  
  <p>The code listed below is a shortened version of my project, as the source files are quite long and don't really need to be posted.</p>
  
  <pre><code>int _tmain(int argc, _TCHAR* argv[])
  {
      check(true);
      system("pause");
      return 0;
  }
  
  int check(bool initialCheck)
  {
      char* path = getDocumentRootA(); strcat(path, "Test//file.test");
      char* filePathA = getDocumentRootA(); strcat(filePathA, "Test2\\file.test");
      char* filePathB = getDocumentRootA(); strcat(filePathB, "Test3\\file.test");
      cout &lt;&lt; "Checking if files exists...";
      if (doesFileExist(path) == true)
      {
          cout &lt;&lt; "Yes\n";
      } else if (doesFileExist(path) == false) {
          cout &lt;&lt; "No\n"; // todo
      }
      cout &lt;&lt; "Checking if other files exist...";
      if (doesFileExist(filePathA) == true &amp;&amp; doesFileExist(filePathB) == true)
      {
          cout &lt;&lt; "Yes\n";
      }
      return 0;
  }
  char* getDocumentRootA()
  {
      CHAR documentRootC[MAX_PATH]; CA2W uDocumentRoot(documentRootC);
      HRESULT result = SHGetFolderPath(NULL, CSIDL_PERSONAL, NULL, SHGFP_TYPE_CURRENT, uDocumentRoot); CW2A documentRoot_T(uDocumentRoot); strcat(documentRoot_T, "\\");
      string documentRootTemp = documentRoot_T; char* documentRoot = const_cast&lt;char*&gt;(documentRootTemp.c_str());
      cout&lt;&lt;documentRoot;
      return documentRoot;
  }
  </code></pre>
  
  <p>It's probably also worth noting that I attempted to change the first part of the code (see example below) so that the <code>getDocumentRootA()</code> function was only called once, but that didn't solve the issue.</p>
  
  <pre><code>char* testvar = getDocumentRootA();
  char* path = testvar; strcat(path, "Microsoft\\file.test");
  char* filePathA = testvar; strcat(filePathA, "Windows\\AppLoc\\file.test");
  char* filePathB = testvar; strcat(filePathB, "Windows\\U\\file.test");
  </code></pre>