64,676
社区成员
发帖
与我相关
我的任务
分享
#include<iostream>
#include<string>
#include<list>
using namespace std;
const int MM=100000;
struct node{
node(){}
node(string _v):v(_v){}
~node(){}
string v; //
list<node> child; //
};
node v;
int main(){
node *temp ;
temp= &v;
for(int i=0;i<5;++i){
for(int j=0;j<MM;++j){
string a="aa";
temp->child.push_back(a);
}
temp = &*temp->child.begin();
system("pause");
}
cout<<"释放:"<<endl;
v.~node();
system("pause");
return 0;
}
/*
打开任务管理器可以监测内存的变换情况:
for循环中,内存占用是不断增加的,
调用 v.~node()后,内存减小到初始值。 (这里有疑问,析构函数中什么都没有,是如何析构的?)
但程序结束时报错。
显示 “0x004317b"指令引用的”0x00ff77b0" 内存。该内存不能为 "read" 。
这是肿么回事?
*/
#include<iostream>
#include<string>
#include<list>
using namespace std;
struct node{
friend bool operator == (const node &a,const node &b){ //重载等于号
return a.s == b.s;
}
string s; //本节点内容
list<node> child; //若干个子节点
};
void destroy(node &rt){ //删除 rt 所有的孩子
for(list<node>::iterator It=rt.child.begin(); It!=rt.child.end(); ++It)
destroy(*It);
rt.child.clear();
}
node root;
int main(){
/* 构造 root 这棵树 */
/* ............. */
/* 是否空间 */
destroy(root);
system("pause");
return 0;
}