C++ 异常 : bad_alloc at memory location

标签 c++ exception memory visual-studio-2012 bad-alloc

Unhandled exception at at 0x7650C41F in binary.exe: Microsoft C++ exception: std::bad_alloc at memory location 0x003EEE00.

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).

我不知道我是否犯了一个新手错误,但每次我尝试运行下面的代码时,我都会得到上面列出的错误 - 从我通过各种论坛帖子和错误消息收集到的信息,有内存分配的问题,但据我所知。

下面列出的代码是我项目的简化版本,因为源文件很长,实际上不需要发布。

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 << "Checking if files exists...";
    if (doesFileExist(path) == true)
    {
        cout << "Yes\n";
    } else if (doesFileExist(path) == false) {
        cout << "No\n"; // todo
    }
    cout << "Checking if other files exist...";
    if (doesFileExist(filePathA) == true && doesFileExist(filePathB) == true)
    {
        cout << "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<char*>(documentRootTemp.c_str());
    cout<<documentRoot;
    return documentRoot;
}

可能还值得注意的是,我试图更改代码的第一部分(参见下面的示例),以便仅调用一次 getDocumentRootA() 函数,但这并没有解决问题。

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");

最佳答案

getDocumentRootA() 中,您正在重新调整指向堆栈分配对象的成员变量的指针,这很糟糕,因为一旦您离开该函数,它将被清除。更糟糕的是,你不应该修改 string 的内部成员,所以这里有两个问题:

  string documentRootTemp = documentRoot_T; 
  ^^^^^^
  Object on the stack

  char* documentRoot = const_cast<char*>(documentRootTemp.c_str());
                       ^^^^^^
                       Pointer to said object


   return documentRoot;
          ^^^^^
          Returning said pointer

这里是您使用该指针的地方:

  char* path = getDocumentRootA();
  strcat(path, "Test//file.test");
         ^^^^
         Modifying pointer to object that does not exist anymore

您可能应该只从 GetDocumentRootA() 返回一个 std::string,然后在 check 中使用 c_str 在你需要 const char * 的地方。您可以使用 += 运算符将 char * 附加到 std::string 上,参见 reference .这是一个非常基本的例子,使我的建议更具体:

#include <string>
#include <iostream>

std::string getDocumentRootA()
{
   std::string str( "Path//") ;

   return str ;
}

bool doesFileExist( const char *p )
{
   bool ret = false ;

   // Do checking here

   return ret  ;
}

int main()
{
   std::string str2( getDocumentRootA() ) ;

   str2 += "Test//file.test" ;

   std::cout << str2.c_str()  << std::endl ;

   if( doesFileExist( str2.c_str() ))
   {
      std::cout << "Yes" << std::endl ;
   }
   else
   {
      std::cout << "No" << std::endl ;
   }
}

关于C++ 异常 : bad_alloc at memory location,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15689817/

相关文章:

swift - 将 [UInt8] 数组转换为 xinpgen 结构

C++ Durand-Kerner 根查找算法与 GMP 崩溃,但与 long double 不崩溃

c++ - 运算符 x++;和++x;对于 int。哪个更快?为什么?

c++ - 使用 C++ 旋转字符串

c++ - Cimg 随机抛出 CImgIOException

c - 在结构的灵活成员数组中打印结构成员的地址

c++ - CreateProcess 异常 (kernel32.lib)

无论如何在 try & catch block 中显示 PHP 异常

python - 异常后对象的垃圾回收

c - 有什么方法可以防止应用程序在堆损坏时崩溃? - C 编程语言