最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C++ delete之靜態(tài)變量問題詳解

 更新時間:2021年09月24日 16:27:02   作者:weixin_43436587  
這篇文章主要為大家詳細(xì)介紹了C++delete的一些問題,學(xué)習(xí)如何動態(tài)創(chuàng)建對象,動態(tài)創(chuàng)建的對象與一般對象的區(qū)別,動態(tài)創(chuàng)建的對象的初始化以及釋放動態(tài)分配的內(nè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)存是一個未定義行為。 未定義行為可能產(chǎn)生任何結(jié)果,包括但不限于:產(chǎn)生期望的結(jié)果,產(chǎn)生未期望的結(jié)果,產(chǎn)生隨機的結(jié)果,產(chǎn)生無法解釋的結(jié)果,運行錯誤,隨機的運行時錯誤,編譯錯誤,等等 ---- 你只是放棄了對這片內(nèi)存的所有權(quán)。獲得所有權(quán)的人對這片內(nèi)存做什么(或者說什么都不做)都不關(guān)你的事

static 變量的儲存區(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)建的對象,必須自己用delete回收,不然系統(tǒng)不會幫助回收,出現(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       新對象的地址
0x405004              靜態(tài)變量和普通變量地址不連續(xù),是靜態(tài)變量存在數(shù)據(jù)段
0xe91470              普通變量存在 開辟的堆上
0xe91474
4                    指針大小
8                    對象所占內(nèi)存大小
8                    類大小
point  0x61fefc      新對象a的地址
0x405004             靜態(tài)變量地址不變,靜態(tài)變量屬于整個類
0x61fefc             屬于局部變量,普通變量存在 ??臻g上
0x61ff00
4
8
8
point  0x61fef4     新對象b的地址, b與a之間相差8個字節(jié)
0x405004            靜態(tài)變量地址不變,靜態(tài)變量屬于整個類
0x61fef4            屬于局部變量,普通變量存在 ??臻g上,地址連續(xù)
0x61fef8
4
8
width: 3the pointer is released.   自動調(diào)用析構(gòu)函數(shù)
width: 1the pointer is released.   自動調(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語言深入講解函數(shù)參數(shù)的使用

    C語言深入講解函數(shù)參數(shù)的使用

    函數(shù)的參數(shù)分為形參和實參兩種。形參出現(xiàn)在函數(shù)定義中,在整個函數(shù)體內(nèi)都可以使用,離開該函數(shù)則不能使用。實參出現(xiàn)在主調(diào)函數(shù)中,進入被調(diào)函數(shù)后,實參變量也不能使用
    2022-04-04
  • C語言學(xué)生信息管理系統(tǒng)設(shè)計與實現(xiàn)

    C語言學(xué)生信息管理系統(tǒng)設(shè)計與實現(xiàn)

    這篇文章主要為大家詳細(xì)介紹了C語言學(xué)生信息管理系統(tǒng)設(shè)計與實現(xiàn),文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-01-01
  • C語言輕松實現(xiàn)掃雷小游戲

    C語言輕松實現(xiàn)掃雷小游戲

    掃雷是一款經(jīng)典的小游戲,這篇文章主要為大家詳細(xì)介紹了C語言輕松實現(xiàn)掃雷小游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-08-08
  • C++的頭文件和實現(xiàn)文件詳解

    C++的頭文件和實現(xiàn)文件詳解

    這篇文章主要介紹了C++的頭文件和實現(xiàn)文件詳解的相關(guān)資料,需要的朋友可以參考下
    2015-01-01
  • Qt6遠(yuǎn)程連接MySQL數(shù)據(jù)庫的簡單易上手版

    Qt6遠(yuǎn)程連接MySQL數(shù)據(jù)庫的簡單易上手版

    在Qt應(yīng)用程序里,可實現(xiàn)遠(yuǎn)程MySQL服務(wù)器的連接操作,本文就來介紹一下Qt6遠(yuǎn)程連接MySQL數(shù)據(jù)庫,具有一定的參考價值,感興趣的可以了解一下
    2023-11-11
  • vscode C++開發(fā)環(huán)境配置步驟詳解(教你如何用vscode編寫寫C++)

    vscode C++開發(fā)環(huán)境配置步驟詳解(教你如何用vscode編寫寫C++)

    這篇文章主要介紹了vscode C++開發(fā)環(huán)境配置詳細(xì)教程(教你如何用vscode編寫寫C++),本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-03-03
  • C語言修煉之路函數(shù)篇真題訓(xùn)練下

    C語言修煉之路函數(shù)篇真題訓(xùn)練下

    函數(shù)是一組一起執(zhí)行一個任務(wù)的語句。每個 C 程序都至少有一個函數(shù),即主函數(shù) main() ,所有簡單的程序都可以定義其他額外的函數(shù)
    2022-03-03
  • c++中的繼承關(guān)系

    c++中的繼承關(guān)系

    繼承呈現(xiàn)了面向?qū)ο蟪绦蛟O(shè)計的層次結(jié)構(gòu),體現(xiàn)了由簡單到復(fù)雜的認(rèn)知過程,本文給大家介紹c++中的繼承關(guān)系,感興趣的朋友跟隨小編一起看看吧
    2021-07-07
  • C++實現(xiàn)十六進制字符串轉(zhuǎn)換成int整形值的示例

    C++實現(xiàn)十六進制字符串轉(zhuǎn)換成int整形值的示例

    今天小編就為大家分享一篇關(guān)于C++實現(xiàn)十六進制字符串轉(zhuǎn)換成int整形值的示例,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2018-12-12
  • Linux?C/C++實現(xiàn)顯示NIC流量統(tǒng)計信息

    Linux?C/C++實現(xiàn)顯示NIC流量統(tǒng)計信息

    NIC流量統(tǒng)計信息是由操作系統(tǒng)維護的,當(dāng)數(shù)據(jù)包通過NIC傳輸時,操作系統(tǒng)會更新相關(guān)的計數(shù)器,通過讀取這些計數(shù)器,我們可以獲得關(guān)于網(wǎng)絡(luò)流量的信息,下面我們就來學(xué)習(xí)一下如何通過C/C++實現(xiàn)顯示NIC流量統(tǒng)計信息吧
    2024-01-01

最新評論

乌拉特前旗| 深州市| 巴马| 阿克陶县| 两当县| 宜宾县| 阿克苏市| 博乐市| 文山县| 卢氏县| 长治县| 保靖县| 丘北县| 永安市| 高要市| 正定县| 安宁市| 平遥县| 贞丰县| 宁都县| 鸡西市| 忻城县| 九江县| 聊城市| 信宜市| 蒲江县| 越西县| 城步| 搜索| 泾阳县| 临潭县| 仁寿县| 新泰市| 卢湾区| 美姑县| 宜都市| 霍城县| 五莲县| 河北省| 德庆县| 信阳市|