我无法删除这些两个指针:当我运行程序,他没有“删除删除多个字符指针
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;'之后丢失。 –