C++ - Ausnahme: bad_alloc at memory location
Unbehandelte Ausnahme bei 0x7650C41F in binary.exe: Microsoft C++ - Ausnahme: 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).
Ich weiß nicht, ob ich ein newbie Fehler, aber jedes mal, wenn ich versuche, führen Sie den code unten bekomme ich die Fehler, die oben aufgeführt sind - von dem, was ich sammeln können durch verschiedene Beiträge im forum und der Fehlermeldungen, gibt es ein Problem mit der Zuweisung von Speicher, aber das ist so weit wie ich habe.
Den unten aufgeführten code ist eine verkürzte version von meinem Projekt, wie die Quell-Dateien sind ziemlich lang und nicht wirklich brauchen, um veröffentlicht werden.
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;
}
Es ist wohl auch erwähnenswert, dass ich habe versucht, ändern Sie den ersten Teil des Codes (siehe Beispiel unten), so dass die getDocumentRootA()
Funktion nur einmal aufgerufen, aber das hat nicht das Problem zu lösen.
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");
- Da Sie erwähnen, könnten Sie ein Neuling sind, möchte ich darauf hinweisen, sollten Sie eigentlich nur eine Anweisung pro Zeile. Wenn mehr als eine Anweisung (irgendetwas gefolgt von einem Semikolon, ist wirklich schlecht: es macht den code schwerer zu Lesen und zu Debuggen ein Alptraum.
Du musst angemeldet sein, um einen Kommentar abzugeben.
In
getDocumentRootA()
Sie hat einen Zeiger auf eine member-variable von einem stack reservierten Objekts ist schlecht, denn sobald Sie die Funktion verlassen, wird es gereinigt. Es ist sogar noch schlimmer, da sollte man nicht ändern das interne Mitglied derstring
so gibt es zwei Probleme gibt es:und hier ist, wo Sie sind mit diesem Zeiger:
Sollte man wohl einfach nur wieder eine
std::string
ausGetDocumentRootA()
und dann incheck
mitc_str
in den Orten, wo Sie brauchenconst char *
. Sie können die+=
operator Anhängenchar *
zu einemstd::string
finden Sie in diesem Referenz. Hier ist ein sehr einfaches Beispiel, um meinen Vorschlag konkreter:GetDocumentRootA()
um einen string zurückzugeben, und veränderttestvar
zutestvar.c_str
jedoch beim kompilieren, bekomme ich folgende Fehlermeldung.Error 1 error C3867: 'std::basic_string<_Elem,_Traits,_Ax>::c_str': function call missing argument list; use '&std::basic_string<_Elem,_Traits,_Ax>::c_str' to create a pointer to member
. Der Grund, warum ich nicht verwendentestvar.c_str()
(wie ich gewohnt bin zu tun) ist, weil ich dann eine IntelliSense-Fehler sagen, dassa value of type "const char*" cannot be used to initialise an entity type of "char *"
.