您好,登錄后才能下訂單哦!
這篇文章將為大家詳細講解有關C++中delete之靜態變量問題的示例分析,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。
#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*/
#include <iostream> using namespace std; int * func(){ int * a = new int(10); return a; } int main(){ int * p = func(); cout << *p << endl;//10 //delete關鍵字用來釋放堆區數據 delete p; // p = new int(5); cout << *p << endl;//10 return 0; }
//輸出
// 10
// 16584968
解釋:
訪問 delete 之后的內存是一個未定義行為。 未定義行為可能產生任何結果,包括但不限于:產生期望的結果,產生未期望的結果,產生隨機的結果,產生無法解釋的結果,運行錯誤,隨機的運行時錯誤,編譯錯誤,等等 ---- 你只是放棄了對這片內存的所有權。獲得所有權的人對這片內存做什么(或者說什么都不做)都不關你的事
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參考文章
#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創建的對象,必須自己用delete回收,不然系統不會幫助回收,出現內存泄漏 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 新對象的地址 0x405004 靜態變量和普通變量地址不連續,是靜態變量存在數據段 0xe91470 普通變量存在 開辟的堆上 0xe91474 4 指針大小 8 對象所占內存大小 8 類大小 point 0x61fefc 新對象a的地址 0x405004 靜態變量地址不變,靜態變量屬于整個類 0x61fefc 屬于局部變量,普通變量存在 棧空間上 0x61ff00 4 8 8 point 0x61fef4 新對象b的地址, b與a之間相差8個字節 0x405004 靜態變量地址不變,靜態變量屬于整個類 0x61fef4 屬于局部變量,普通變量存在 棧空間上,地址連續 0x61fef8 4 8 width: 3the pointer is released. 自動調用析構函數 width: 1the pointer is released. 自動調用析構函數 */
#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. */
關于“C++中delete之靜態變量問題的示例分析”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,使各位可以學到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。