激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

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

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

服務器之家 - 編程語言 - C/C++ - C++中的auto_ptr智能指針的作用及使用方法詳解

C++中的auto_ptr智能指針的作用及使用方法詳解

2021-04-09 13:482010120422 C/C++

這篇文章主要介紹了C++中的auto_ptr智能指針的作用及使用方法詳解的相關資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下

智能指針(auto_ptr) 這個名字聽起來很酷是不是?其實auto_ptr 只是C++標準庫提供的一個類模板,它與傳統的new/delete控制內存相比有一定優勢,但也有其局限。本文總結的8個問題足以涵蓋auto_ptr的大部分內容。

 auto_ptr是什么?

auto_ptr 是C++標準庫提供的類模板,auto_ptr對象通過初始化指向由new創建的動態內存,它是這塊內存的擁有者,一塊內存不能同時被分給兩個擁有者。當auto_ptr對象生命周期結束時,其析構函數會將auto_ptr對象擁有的動態內存自動釋放。即使發生異常,通過異常的棧展開過程也能將動態內存釋放。auto_ptr不支持new 數組。

該類型在頭文件memory中,在程序的開通通過 #include<memory> 導入,接下來講解該智能指針的作用和使用。

使用方法:

  auto_ptr<type> ptr(new type()); 這是該指針的定義形式,其中 type 是指針指向的類型,ptr 是該指針的名稱。

  比如該type 是int,具體定義如下:

  auto_ptr<int> ptr(new int(4));

  比如該type 是map<int,vector<int> >,具體定義如下:

  auto_ptr<map<int,vector<int> > > ptr(new map<int,vector<int> > ());

  當然可以先定義,后賦值,如下所示:

  auto_ptr<map<int,int> > ptr;
  ptr = auto_ptr<map<int,int> >(new map<int,int> ());

作用1:保證一個對象在某個時間只能被一個該種類型的智能指針所指向,就是通常所說的對象所有權。

作用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
#include <iostream>
#include <string.h>
#include <memory>
#include <string>
#include <Windows.h>
#include <map>
#include <ctime>
#include <vector>
using namespace std;#define MAXN 20000000
class test_ptr
{
public:
map<int,int> *p;
test_ptr()
{
p = new map<int,int>();
for(int i = 0;i<MAXN;i++)
p->insert(make_pair(i,i));
}
};
int main(int argc,char *argv[])
{
for(int i = 0;i<100;i++)
{
Sleep(1000);
cout << i << endl; // 輸出 創建次數
test_ptr * tmp = new test_ptr();
}
system("pause");
return 0;
}

 

在某些情況下,可能我們就會寫出上面的代碼來,通過運行會發現存在內存溢出。對于一些經驗老道的程序員可能會作如下改寫:

代碼片段二:

?
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
#include <iostream>
#include <string.h>
#include <memory>
#include <string>
#include <Windows.h>
#include <map>
#include <ctime>
#include <vector>
using namespace std;
#define MAXN 20000000
class test_ptr
{
public:
map<int,int> *p;
test_ptr()
{
//p = auto_ptr<map<int,int> > (new map<int,int>());
p = new map<int,int>();
for(int i = 0;i<MAXN;i++)
p->insert(make_pair(i,i));
}
~test_ptr()
{
delete p;
}
};
int main(int argc,char *argv[])
{
for(int i = 0;i<100;i++)
{
Sleep(1000);
cout << i << endl;
test_ptr * tmp = new test_ptr();
}
system("pause");
return 0;
}

在test_ptr 類中的析構函數中添加內存釋放代碼,但是在main函數中,定義的局部指針,當局部指針失效時并不會自動調用析構函數,在這種情況下也會導致內存泄漏問題。當然,如果細心的程序員可以在 test_ptr * tmp = new test_ptr() 后面加上一句 delete tmp ,這樣也能夠釋放內存,不會出現內存泄漏問題。但是在某些情況下,很容易漏寫,為了解決此問題,auto_ptr 就能發揮作用了。

代碼片段三:

?
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
#include <iostream>
#include <string.h>
#include <memory>
#include <string>
#include <Windows.h>
#include <map>
#include <ctime>
#include <vector>
using namespace std;
#define MAXN 20000000
class test_ptr
{
public:
map<int,int> *p;
test_ptr()
{
p = new map<int,int>();
for(int i = 0;i<MAXN;i++)
p->insert(make_pair(i,i));
}
~test_ptr()
{
delete p;
}
};
int main(int argc,char *argv[])
{
for(int i = 0;i<100;i++)
{
Sleep(1000);
cout << i << endl; //輸出創建次數
auto_ptr<test_ptr> tmp = auto_ptr<test_ptr> (new test_ptr());
}
system("pause");
return 0;
}

在main函數中,創建test_ptr類型指針時,該指針是auto_ptr 類型的智能指針,當智能指針失效時,會自動調用該類的析構函數。所以這種寫法可以不再顯示調用delete 語句了。但是該智能指針也只是保證調用類的析構函數,如果析構函數并沒有釋放類中聲明的變量,那該怎么辦。

代碼片段四:

?
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
#include <iostream>
#include <string.h>
#include <memory>
#include <string>
#include <Windows.h>
#include <map>
#include <ctime>
#include <vector>
using namespace std;
#define MAXN 20000000
class test_ptr
{
public:
//auto_ptr<map<int,int> > p;
map<int,int> *p;
test_ptr()
{
//p = auto_ptr<map<int,int> > (new map<int,int>());
p = new map<int,int>();
for(int i = 0;i<MAXN;i++)
p->insert(make_pair(i,i));
}
/*
~test_ptr()
{
delete p;
}
*/
};
int main(int argc,char *argv[])
{
for(int i = 0;i<100;i++)
{
Sleep(1000);
cout << i << endl; //輸出創建次數
auto_ptr<test_ptr> tmp = auto_ptr<test_ptr> (new test_ptr());
}
system("pause");
return 0;
}

在這種情況下,還是會出現內存泄漏問題,為了解決該問題,對類中聲明的指針也是需要聲明為auto_ptr類型。

代碼片段五:

?
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
#include <iostream>
#include <string.h>
#include <memory>
#include <string>
#include <Windows.h>
#include <map>
#include <ctime>
#include <vector>
using namespace std;
#define MAXN 20000000
class test_ptr
{
public:
auto_ptr<map<int,int> > p;
test_ptr()
{
p = auto_ptr<map<int,int> > (new map<int,int>());
for(int i = 0;i<MAXN;i++)
p->insert(make_pair(i,i));
}
};
int main(int argc,char *argv[])
{
for(int i = 0;i<100;i++)
{
Sleep(1000);
cout << i << endl; //輸出創建次數
auto_ptr<test_ptr> tmp = auto_ptr<test_ptr> (new test_ptr());
}
system("pause");
return 0;
}

這樣就不用顯示定義類的析構函數,不用在外部顯示調用delete函數,當然如果盡早調用delete函數也是可以的,盡早釋放內存也比該指針失效再釋放好一些,這些就是為了防止忘記調用。

通過如上分析:可以得出如下結論。

1 定義了智能指針,當智能指針失效時會自動調用類的析構函數。

2 在 類中定義的智能指針,不必在析構函數中顯示的delete,當外部調用該類的析構函數時,會自動釋放該智能指針指向的對象,釋放內存。

3 如果類中定義的是智能指針,但是外部沒有觸發類中的析構函數調用,該智能指針指向的對象還是不能釋放。

auto_ptr 智能指針的bug

auto_ptr 智能指針在c++ 11 標準中已經被拋棄,被拋棄的原因就是因為該bug。前面也提到過,一個對象只能被一個智能指針所引用,這樣就會導致一個賦值問題。看如下代碼

代碼片段六:

?
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
#include <iostream>
#include <string.h>
#include <memory>
#include <set>
using namespace std;
#define MAXN 20000000
void pri(auto_ptr<set<int> > p)
{
set<int>::iterator ite = p->begin();
for(;ite!=p->end();ite++)
{
cout << *ite << endl;
}
}
int main(int argc,char *argv[])
{
auto_ptr<set<int> > ptr(new set<int> ());
for(int i = 0;i< 3;i++)
{
int a;
cin >> a;
ptr->insert(a);
}
pri(ptr);
pri(ptr);
system("pause");
return 0;
}

初看這代碼沒什么問題,不過運行程序會崩潰。這就是該智能指針最大的bug, 在程序32行 調用pri(ptr) ,程序到這并沒什么問題,但是第二次調用pri(ptr) 時程序就會崩潰。原因就是前面講過,一個對象智能被一個智能指針所指向,在第一次調用pri()函數時,為了保證這一原則,當把ptr指針傳入pri函數時,程序內部就把ptr置為空,所以到第二次調用時,就會出現崩潰的情況。對于這種情況的解決之道就是使用shared_ptr 指針(該指針的原理是通過引用計數器來實現的)。

如果要使用shared_ptr 智能指針,需要安裝boost庫,該庫還包括許多其他功能。有興趣的可以嘗試以下,該類中的智能指針還是比較好用。也不存在很多其他bug。

以上所述是小編給大家介紹的C++中的auto_ptr智能指針實例詳解,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對服務器之家網站的支持!

原文鏈接:http://www.cnblogs.com/ChenAlong/archive/2016/07/12/5662851.html

延伸 · 閱讀

精彩推薦
  • C/C++詳解c語言中的 strcpy和strncpy字符串函數使用

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

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

    spring-go5642021-07-02
  • C/C++C++之重載 重定義與重寫用法詳解

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

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

    青山的青6062022-01-04
  • C/C++C/C++經典實例之模擬計算器示例代碼

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

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

    jia150610152021-06-07
  • 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++,感興趣的朋友可以了解下...

    源之緣11542021-10-27
  • C/C++C語言實現電腦關機程序

    C語言實現電腦關機程序

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

    xiaocaidayong8482021-08-20
Weibo Article 1 Weibo Article 2 Weibo Article 3 Weibo Article 4 Weibo Article 5 Weibo Article 6 Weibo Article 7 Weibo Article 8 Weibo Article 9 Weibo Article 10 Weibo Article 11 Weibo Article 12 Weibo Article 13 Weibo Article 14 Weibo Article 15 Weibo Article 16 Weibo Article 17 Weibo Article 18 Weibo Article 19 Weibo Article 20 Weibo Article 21 Weibo Article 22 Weibo Article 23 Weibo Article 24 Weibo Article 25
主站蜘蛛池模板: 激情久久一区二区 | 18视频网站在线观看 | 黄色网址你懂的 | 欧美一级做a| 国产高潮国产高潮久久久91 | 中韩毛片 | 欧美视频99 | 国产一区二区三区在线视频 | 一本精品999爽爽久久久 | 成人免费毛片一 | 国产免费传媒av片在线 | 日日操日日操 | 久久久综合视频 | 成年人免费视频大全 | 国产1区在线观看 | 2021av视频| 日韩视频一二三 | 欧美日韩高清不卡 | 毛片视频免费观看 | 久久激情免费视频 | 72pao成人国产永久免费视频 | 日本综合久久 | 精品一区二区三区在线视频 | 久久久www成人免费毛片 | 羞羞视频在线免费 | 久久美女色视频 | 国产免费一区二区三区在线能观看 | 国产亚洲精品久久久久久久软件 | 久草成人在线观看 | 久久国产精品免费视频 | 国产品久久 | 国产中文av在线 | 国产在线欧美日韩 | 成人免费淫片视频观 | 在线成人亚洲 | 性欧美一区二区 | 91国内精品久久久久免费影院 | www.成人精品 | 欧美日韩经典在线 | 伊人网站| 斗破苍穹在线观看免费完整观看 |