主题:DELETE[] 回收多个数组空间时出现的问题!
circlesky
[专家分:170] 发布于 2010-05-21 00:16:00
比如用new分配了两个动态数组的内存:
char *p,*q,m;
p=new char[m];
q=new char[m];
如果该代码放在类B中,类A派生出类B,类B派生出类C。然后在main()函数中生成C对象,运行程序时析构函数中:delete[] p;delete[] q会出错,而delete[] p,q正常运行。然而我在没有继承的类中测试delete[] p;delete[] q和delete[] p,q时,好像两者没有区别,都能实现正常回收空间。
请问高手在回收内存时:delete[] p;delete[] q和delete[] p,q的区别是什么?谢谢!
最后更新于:2010-05-21 00:22:00
回复列表 (共4个回复)
沙发
雪光风剑 [专家分:27190] 发布于 2010-05-21 08:02:00
先检查两个指针是不是被分配给了同一块内存
板凳
bruceteen [专家分:42660] 发布于 2010-05-21 10:57:00
1. 把代码贴出来嘛
2. "而delete[] p,q正常运行" --- 很奇怪吆!
难道你认为 delete[] p,q; 等同于 delete[] p; delete[] q; ??????
3 楼
circlesky [专家分:170] 发布于 2010-05-21 18:11:00
首先谢谢各位。
bruceteen:
我的代码如下(该代码能正确运行,在VC++6.0下调试的程序),问题是将红色的代码改成:delete[] color;delete[] collar;就不能正常运行。请您帮忙解答!并说明两种方法回收内存的区别!谢谢!
#include<iostream.h>
#include<string.h>
class Cloth
{
protected:
char* material;
public:
Cloth(){};
Cloth(char*);
void setMaterial(char*);
void printMaterial();
~Cloth()
{
delete[] material;
material = 0L;
}
};
Cloth::Cloth(char *newMaterial)
{
material = new char[strlen(newMaterial) + 1];
strcpy(material,newMaterial);
}
void Cloth::setMaterial(char* newMaterial)
{
material = new char[strlen(newMaterial) + 1];
strcpy(material,newMaterial);
}
void Cloth :: printMaterial()
{
cout << "布料:" << material << endl;
}
class Clothes : public Cloth
{
protected:
char* color;
char* collar;
public:
Clothes(){}
Clothes(char*,char*);
Clothes(char*,char*,char*);
void setColor(char* newColor);
void setCollar(char* newCollar);
void printColor();
void printCollar();
~Clothes();
};
Clothes::Clothes(char* newColor,char* newCollar)
{
color = new char[strlen(newColor) + 1];
strcpy(color,newColor);
collar = new char[strlen(newCollar) + 1];
strcpy(collar,newCollar);
}
Clothes::Clothes(char* newMaterial,char* newColor,char* newCollar):Cloth(newMaterial)
{
color = new char[strlen(newColor) + 1];
strcpy(color,newColor);
collar = new char[strlen(newCollar) + 1];
strcpy(collar,newCollar);
}
void Clothes::setCollar(char* newCollar)
{
collar = new char[strlen(newCollar) + 1];
strcpy(collar,newCollar);
}
void Clothes::printColor()
{
cout << "颜色:" << color << endl;
}
void Clothes::setColor(char* newColor)
{
color = new char[strlen(newColor) + 1];
strcpy(color,newColor);
}
void Clothes::printCollar()
{
cout << "衣领:" << collar <<endl;
}
Clothes::~Clothes()
{
[color=FF0000]delete[] color,collar;[/color]
color = 0L;
collar = 0L;
}
class Shirt : public Clothes
{
private:
int sleeve;
public:
Shirt(){};
Shirt(int newSleeve)
{
sleeve = newSleeve;
}
Shirt(char* newColor,char* newCollar,int newSleeve);
void setSleeve(int newSleeve)
{
sleeve = newSleeve;
}
int getSleeve()
{
return sleeve;
}
};
Shirt::Shirt(char* newColor,char* newCollar,int newSleeve):Clothes(newColor,newCollar)
{
sleeve = newSleeve;
}
int main()
{
Shirt shirt(35);
cout << "shirt:" << endl;
shirt.setMaterial("纯棉");
shirt.setColor("红色");
shirt.printColor();
shirt.printMaterial();
cout << "衣袖长度:" << shirt.getSleeve() << endl;
Shirt shirt2("白色","翻领",30);
cout << "shirt2:" << endl;
shirt2.setMaterial("涤纶");
shirt2.printCollar();
shirt2.printColor();
shirt2.printMaterial();
return 0;
}
4 楼
xiaoruo331 [专家分:100] 发布于 2010-05-25 09:31:00
深圳华信群英科技有限公司,目前主要从事软件外包开发、高级软件人才定向输送等业务,面对日益严峻的大学生就业难和IT企业招聘难,自2006以来,与知名通信软件公司正式签订定向输送人才的合作协议,针对想进入软件行业的人员提供企业实训,实训课程采用“2+3”分段教学模式,分为2个月理论强化和3个月项目实训两个阶段,共计900学时。
企业订单委培,内训前签订就业安置协议,100%保证就业。实训结束之后,在华信群英科技及其战略合作伙伴从事通信软件研发工作。欢迎有意向者加QQ:649846168咨询。
我来回复