我無法刪除這些兩個指針:當我運行程序,他沒有「刪除刪除多個字符指針
int *p = new int;
char * string1 = new char[20];
char * string2 = new char[25];
strcpy(string1, "Sheldon");
string2 = string1;
delete p;
delete[] string2; //this works without the next line
delete[] string1; //when I add this, it fails at string2 = string1
我使用的內存泄漏檢測與
#define _CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>
[] string1「,它給了我」{66}在0x0075E300正常的塊,25個字節長。「所以「delete [] string2」正在刪除string1。這對我來說沒有任何意義,但我猜測它與任務有關。我試圖查找有關它的東西,但沒有任何工作。
'字符串2 = string1' - 你的印象是,這是一個字符串,下副本?你爲什麼在C++中使用'char *'s而不是'std :: string's? – user2357112
沒有人有時間。使用'std :: string'。 –
您正在嘗試刪除相同的指針兩次。另一個在'string2 = string1;'之後丟失。 –