C++ delete之靜態(tài)變量問題詳解
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;
}
//輸出:
/*100
100
width 16257288
length 16253120
-1812113408*/
例2
#include <iostream>
using namespace std;
int * func(){
int * a = new int(10);
return a;
}
int main(){
int * p = func();
cout << *p << endl;//10
//delete關(guān)鍵字用來釋放堆區(qū)數(shù)據(jù)
delete p;
// p = new int(5);
cout << *p << endl;//10
return 0;
}
//輸出
// 10
// 16584968
解釋:
訪問 delete 之后的內(nèi)存是一個(gè)未定義行為。 未定義行為可能產(chǎn)生任何結(jié)果,包括但不限于:產(chǎn)生期望的結(jié)果,產(chǎn)生未期望的結(jié)果,產(chǎn)生隨機(jī)的結(jié)果,產(chǎn)生無法解釋的結(jié)果,運(yùn)行錯(cuò)誤,隨機(jī)的運(yùn)行時(shí)錯(cuò)誤,編譯錯(cuò)誤,等等 ---- 你只是放棄了對(duì)這片內(nèi)存的所有權(quán)。獲得所有權(quán)的人對(duì)這片內(nèi)存做什么(或者說什么都不做)都不關(guān)你的事
static 變量的儲(chǔ)存區(qū)域
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; //point 0xe91470
cout<<&(p->height)<<endl; //0x405004
cout<<&(p->width)<<endl; //0xe91470
cout<<&(p->length)<<endl; //0xe91474
cout<<sizeof(p)<<endl; //4
cout<<sizeof(*p)<<endl; //8
cout<<sizeof(Box)<<endl; //8
//delete p; //width: 10the pointer is released. 用new創(chuàng)建的對(duì)象,必須自己用delete回收,不然系統(tǒng)不會(huì)幫助回收,出現(xiàn)內(nèi)存泄漏
Box a = Box(1,2);
Box *pa = &a;
cout<<"point "<<pa<<endl; //point 0x61ff00
cout<<&(pa->height)<<endl; //0x405004
cout<<&(pa->width)<<endl; //0x61fefc
cout<<&(pa->length)<<endl; //0x61ff00
cout<<sizeof(pa)<<endl; //4
cout<<sizeof(*pa)<<endl; //8
cout<<sizeof(a)<<endl; //8
Box b = Box(3,4);
Box *pb = &b;
cout<<"point "<<pb<<endl; //point 0x61fef4
cout<<&(pb->height)<<endl; //0x61fef4
cout<<&(pb->width)<<endl; //0x61fef4
cout<<&(pb->length)<<endl; //0x61fef8
cout<<sizeof(pb)<<endl;
cout<<sizeof(*pb)<<endl;
return 0;
}
/*
point 0xe91470 新對(duì)象的地址
0x405004 靜態(tài)變量和普通變量地址不連續(xù),是靜態(tài)變量存在數(shù)據(jù)段
0xe91470 普通變量存在 開辟的堆上
0xe91474
4 指針大小
8 對(duì)象所占內(nèi)存大小
8 類大小
point 0x61fefc 新對(duì)象a的地址
0x405004 靜態(tài)變量地址不變,靜態(tài)變量屬于整個(gè)類
0x61fefc 屬于局部變量,普通變量存在 ??臻g上
0x61ff00
4
8
8
point 0x61fef4 新對(duì)象b的地址, b與a之間相差8個(gè)字節(jié)
0x405004 靜態(tài)變量地址不變,靜態(tài)變量屬于整個(gè)類
0x61fef4 屬于局部變量,普通變量存在 ??臻g上,地址連續(xù)
0x61fef8
4
8
width: 3the pointer is released. 自動(dòng)調(diào)用析構(gòu)函數(shù)
width: 1the pointer is released. 自動(dòng)調(diào)用析構(gòu)函數(shù)
*/
例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;
// delete p;
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;
}
/*
point 0x791470
0x405004
0x791470
0x791474
4
8
8
point 0x791108
0x405004
0x791108
0x79110c
4
8
8
width: 10the pointer is released.
width: 30the pointer is released.
point 0x61fef8
0x405004
0x61fef8
0x61fefc
4
8
8
point 0x61fef0
0x405004
0x61fef0
0x61fef4
4
8
width: 3the pointer is released.
width: 1the pointer is released.
*/
總結(jié)
本篇文章就到這里了,希望能夠給你帶來幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!
相關(guān)文章
C語言kmp算法簡(jiǎn)單示例和實(shí)現(xiàn)原理探究
這篇文章主要介紹了C語言kmp算法簡(jiǎn)單示例和實(shí)現(xiàn)原理探究,本文用簡(jiǎn)潔的語言說明KMP算法的原理,并給出了示例,需要的朋友可以參考下2014-09-09
C++使用string的大數(shù)快速模冪運(yùn)算(6)
這篇文章主要為大家詳細(xì)介紹了C++使用string的大數(shù)快速模冪運(yùn)算,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-09-09
基于Opencv實(shí)現(xiàn)顏色識(shí)別
這篇文章主要為大家詳細(xì)介紹了基于Opencv實(shí)現(xiàn)顏色識(shí)別,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-07-07
C語言中等待socket連接和對(duì)socket定位的方法
這篇文章主要介紹了C語言中等待socket連接和對(duì)socket定位的方法,分別為listen()函數(shù)和bind()函數(shù)的用法,需要的朋友可以參考下2015-09-09
對(duì)一個(gè)數(shù)組進(jìn)行zig-zag重新排列
本文介紹了“對(duì)一個(gè)數(shù)組進(jìn)行zig-zag重新排列”,需要的朋友可以參考一下2013-03-03

