一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - C/C++ - C++ delete之靜態變量問題詳解

C++ delete之靜態變量問題詳解

2022-01-11 15:07weixin_43436587 C/C++

這篇文章主要為大家詳細介紹了C++delete的一些問題,學習如何動態創建對象,動態創建的對象與一般對象的區別,動態創建的對象的初始化以及釋放動態分配的內存等知識點,感興趣的朋友可以參考一下

delete釋放的指針,再訪問

例1

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#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 之后的內存是一個未定義行為。 未定義行為可能產生任何結果,包括但不限于:產生期望的結果,產生未期望的結果,產生隨機的結果,產生無法解釋的結果,運行錯誤,隨機的運行時錯誤,編譯錯誤,等等 ---- 你只是放棄了對這片內存的所有權。獲得所有權的人對這片內存做什么(或者說什么都不做)都不關你的事

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#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.   自動調用析構函數
*/

例2 幫助理解

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#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.
*/

總結

本篇文章就到這里了,希望能夠給你帶來幫助,也希望您能夠多多關注服務器之家的更多內容!

原文鏈接:https://blog.csdn.net/qq_44888950/article/details/120442843

延伸 · 閱讀

精彩推薦
  • C/C++c++ 單線程實現同時監聽多個端口

    c++ 單線程實現同時監聽多個端口

    這篇文章主要介紹了c++ 單線程實現同時監聽多個端口的方法,幫助大家更好的理解和學習使用c++,感興趣的朋友可以了解下...

    源之緣11542021-10-27
  • C/C++深入理解goto語句的替代實現方式分析

    深入理解goto語句的替代實現方式分析

    本篇文章是對goto語句的替代實現方式進行了詳細的分析介紹,需要的朋友參考下...

    C語言教程網7342020-12-03
  • C/C++C語言中炫酷的文件操作實例詳解

    C語言中炫酷的文件操作實例詳解

    內存中的數據都是暫時的,當程序結束時,它們都將丟失,為了永久性的保存大量的數據,C語言提供了對文件的操作,這篇文章主要給大家介紹了關于C語言中文件...

    針眼_6702022-01-24
  • C/C++學習C++編程的必備軟件

    學習C++編程的必備軟件

    本文給大家分享的是作者在學習使用C++進行編程的時候所用到的一些常用的軟件,這里推薦給大家...

    謝恩銘10102021-05-08
  • C/C++C/C++經典實例之模擬計算器示例代碼

    C/C++經典實例之模擬計算器示例代碼

    最近在看到的一個需求,本以為比較簡單,但花了不少時間,所以下面這篇文章主要給大家介紹了關于C/C++經典實例之模擬計算器的相關資料,文中通過示...

    jia150610152021-06-07
  • C/C++C++之重載 重定義與重寫用法詳解

    C++之重載 重定義與重寫用法詳解

    這篇文章主要介紹了C++之重載 重定義與重寫用法詳解,本篇文章通過簡要的案例,講解了該項技術的了解與使用,以下就是詳細內容,需要的朋友可以參考下...

    青山的青6062022-01-04
  • C/C++C語言實現電腦關機程序

    C語言實現電腦關機程序

    這篇文章主要為大家詳細介紹了C語言實現電腦關機程序,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下...

    xiaocaidayong8482021-08-20
  • C/C++詳解c語言中的 strcpy和strncpy字符串函數使用

    詳解c語言中的 strcpy和strncpy字符串函數使用

    strcpy 和strcnpy函數是字符串復制函數。接下來通過本文給大家介紹c語言中的strcpy和strncpy字符串函數使用,感興趣的朋友跟隨小編要求看看吧...

    spring-go5642021-07-02
主站蜘蛛池模板: 精品一区二区三区免费毛片 | 日一日操一操 | 日本无卡无吗中文免费 | 10个免费货源网站 | 日产免费自线一二区 | 天天舔天天操天天干 | 男人的天堂在线观看免费 | 福利一区在线观看 | 免费在线看片网站 | 色中文| 女人张开腿让男人做爽爽 | 99综合在线| 亚洲国产精品婷婷久久久久 | 亚洲图片综合区 | 精精国产xxxx视频在线播放器 | bestialityvideo另类| 国产精品va在线观看无 | 亚洲+欧美+国产+综合 | 国产裸舞在线一区二区 | 免费观看欧美性一级 | 国产精品一区二区三 | 亚洲人成影院午夜网站 | 2019韩国最新三级 | 国产欧美日韩精品在线 | 美女扒开尿口让男生添 漫画 | 激情婷婷综合久久久久 | 扒开女人下面 | 特黄a级三级三级野战 | 成人免费淫片95视频观看网站 | 国产男人搡女人免费视频 | 日本男男漫画 | 亚洲黄色高清 | 情欲满载2012美国dvd | 午夜在线观看免费观看 视频 | 亚洲精品国产精品麻豆99 | 国产伦精品一区二区三区免费迷 | 美女扒开腿让男人桶爽免费gif | 99九九国产精品免费视频 | 亚洲va精品中文字幕 | 四虎免费入口 | 成人福利视频网址 |