使用這些功能時,我遇到了錯誤。 debbuger在_chdir(dirCorrente);
行上寫着「damped heap」。使用動態分配的變量和_chdir窗口時損壞的堆功能
主要調用以下函數: - char * temp = getCartellaCorrente(); - 不是相對於這些功能的一些其他的東西... - TEMP = setCartellaCorrente( 「CD測試」)
當執行停止setCartellaCorrente的dirCorrente值是「「C:\用戶\盧卡\桌面\的遠程控制項目\ FirstService \調試\測試「'
我想我做錯了動態分配的變量。
自48小時以來,我正在處理這個問題,我已經在互聯網上進行了培訓,但沒有任何問題。我想我不知道重要的分配變量或_chdir函數。
如果你能解釋我的想法,我會非常感激。
char* getCartellaCorrente() {
char* temp;
size_t size;
LPWSTR dirCorrente = new TCHAR[DEFAULT_BUFLEN];
GetCurrentDirectory(DEFAULT_BUFLEN, dirCorrente);
size = wcslen(dirCorrente);
temp = (char *)malloc(size);
wcstombs_s(NULL, temp, size+1, dirCorrente, size);
return temp;
}
char* setCartellaCorrente(char* relative) {
char *dirCorrente;
if (strlen(relative)>=5 && relative[4] == ':') {
dirCorrente = (char *)malloc(DEFAULT_BUFLEN);
strcpy_s(dirCorrente, DEFAULT_BUFLEN, &relative[3]);
}
else {
dirCorrente = getCartellaCorrente();
relative[2] = '\\';
strcat_s(dirCorrente, DEFAULT_BUFLEN, &relative[2]);
printf("goode %s \n", dirCorrente);
}
//fixPathSlash(dirCorrente);
printf("\n2: %s\n", dirCorrente);
int i = _chdir(dirCorrente); //HERE IT STOPS
printf("wtf: %d\n", i);
free(dirCorrente);
printf("boh\n");
return getCartellaCorrente();
}
這是我的第一個問題。對不起,如果我錯過了一些重要的信息,我會很快編輯。
'_chdir'didn't不會損壞你的路徑,你會覆蓋某處的緩衝區。此外,你絕對**必須**提供[mcve],一個簡短的例子,重現了這個問題,包括'main'函數和* included *頭文件。 –
此代碼不會編譯,因爲'/ fixPathSlash' –
此外,您的代碼是** C++ **,而不是C,因爲a)您正在使用C++編譯器進行編譯,b)您正在使用'new'。 –