我有一个程序存在内存泄漏,每次我在代码中添加删除时它都会崩溃。我只是想知道是否有人知道为什么会这样。崩溃的代码如下。这是带有删除名字和姓氏的析构函数,声明如下。
char* firstName;
char* lastName;
Name::~Name(){
delete[] firstName;
delete[] lastName;
}
这是分配内存的地方
Name::Name(Name& name){
//copys the first and last name from one Name to the other
firstName = new char [strlen(name.firstName)+1];
strcpy(firstName,name.firstName);
lastName = new char [strlen(name.lastName)+1];
strcpy(lastName,name.lastName);
}