delete释放的指针,再访问
例1
#include <iostream>
using namespace std;
class Box
{
public:
Box(int,int);
~Box();
void volume();
static int height;
int width;
int length;
};
Box::Box(int wi, int le)
{
width = wi;
length = le;
}
Box::~Box(){cout<<"the pointer is released."<<endl;}
void Box::volume()
{
cout<<height*width*length<<endl;
}
int Box::height = 100;
int main()
{
Box* p = new Box(10,20);
delete p;
cout<<p->height<<endl;
cout<<Box::height<<endl;
cout<<"width" <<p->width<<endl;
cout<<"length "<<p->length<<endl;
p->volume();
return 0;
}
例2
#include <iostream>
using namespace std;
int * func(){
int * a = new int(10);
return a;
}
int main(){
int * p = func();
cout << *p << endl;
delete p;
cout << *p << endl;
return 0;
}
解释: 访问 delete 之后的内存是一个未定义行为。 未定义行为可能产生任何结果,包括但不限于:产生期望的结果,产生未期望的结果,产生随机的结果,产生无法解释的结果,运行错误,随机的运行时错误,编译错误,等等 ---- 你只是放弃了对这片内存的所有权。获得所有权的人对这片内存做什么(或者说什么都不做)都不关你的事
static 变量的储存区域
https://blog.csdn.net/qq_32900237/article/details/107094377?utm_medium=distribute.pc_relevant.none-task-blog-2~default~baidujs_title~default-0.no_search_link&spm=1001.2101.3001.4242 参考文章
例1
#include <iostream>
using namespace std;
class Box
{
public:
Box(int,int);
~Box();
void volume();
static int height;
int width;
int length;
};
Box::Box(int wi, int le)
{
width = wi;
length = le;
}
Box::~Box(){cout<<"width: "<< width <<"the pointer is released."<<endl;}
void Box::volume()
{
cout<<height*width*length<<endl;
}
int Box::height = 100;
int main()
{
Box* p = new Box(10,20);
cout<<"point "<<p<<endl;
cout<<&(p->height)<<endl;
cout<<&(p->width)<<endl;
cout<<&(p->length)<<endl;
cout<<sizeof(p)<<endl;
cout<<sizeof(*p)<<endl;
cout<<sizeof(Box)<<endl;
Box a = Box(1,2);
Box *pa = &a;
cout<<"point "<<pa<<endl;
cout<<&(pa->height)<<endl;
cout<<&(pa->width)<<endl;
cout<<&(pa->length)<<endl;
cout<<sizeof(pa)<<endl;
cout<<sizeof(*pa)<<endl;
cout<<sizeof(a)<<endl;
Box b = Box(3,4);
Box *pb = &b;
cout<<"point "<<pb<<endl;
cout<<&(pb->height)<<endl;
cout<<&(pb->width)<<endl;
cout<<&(pb->length)<<endl;
cout<<sizeof(pb)<<endl;
cout<<sizeof(*pb)<<endl;
return 0;
}
例2 帮助理解
#include <iostream>
using namespace std;
class Box
{
public:
Box(int,int);
~Box();
void volume();
static int height;
int width;
int length;
};
Box::Box(int wi, int le)
{
width = wi;
length = le;
}
Box::~Box(){cout<<"width: "<< width <<"the pointer is released."<<endl;}
void Box::volume()
{
cout<<height*width*length<<endl;
}
int Box::height = 100;
int main()
{
Box* p = new Box(10,20);
cout<<"point "<<p<<endl;
cout<<&(p->height)<<endl;
cout<<&(p->width)<<endl;
cout<<&(p->length)<<endl;
cout<<sizeof(p)<<endl;
cout<<sizeof(*p)<<endl;
cout<<sizeof(Box)<<endl;
Box* p1 = new Box(30,40);
cout<<"point "<<p1<<endl;
cout<<&(p1->height)<<endl;
cout<<&(p1->width)<<endl;
cout<<&(p1->length)<<endl;
cout<<sizeof(p1)<<endl;
cout<<sizeof(*p1)<<endl;
cout<<sizeof(Box)<<endl;
delete p;
delete p1;
Box a = Box(1,2);
Box *pa = &a;
cout<<"point "<<pa<<endl;
cout<<&(pa->height)<<endl;
cout<<&(pa->width)<<endl;
cout<<&(pa->length)<<endl;
cout<<sizeof(pa)<<endl;
cout<<sizeof(*pa)<<endl;
cout<<sizeof(a)<<endl;
Box b = Box(3,4);
Box *pb = &b;
cout<<"point "<<pb<<endl;
cout<<&(pb->height)<<endl;
cout<<&(pb->width)<<endl;
cout<<&(pb->length)<<endl;
cout<<sizeof(pb)<<endl;
cout<<sizeof(*pb)<<endl;
return 0;
}
|