「std::auto ptr」の版間の差分

提供: C++入門
移動: 案内検索
 
(同じ利用者による、間の2版が非表示)
行1: 行1:
<!--
 
vim: filetype=mediawiki
 
-->
 
 
 
[[std::auto_ptr]] メモリの開放管理を自動的に行うためのものです。
 
[[std::auto_ptr]] メモリの開放管理を自動的に行うためのものです。
  
読み方
+
'''読み方'''
 
+
 
;[[std::auto_ptr]]: えすてぃーでぃー おーと ぴーてぃーあーる, おーとぽいんた
 
;[[std::auto_ptr]]: えすてぃーでぃー おーと ぴーてぃーあーる, おーとぽいんた
  
行12: 行7:
  
 
== 概要 ==
 
== 概要 ==
 
 
[[スマートポインタ]]シリーズの中では、[[std::auto_ptr]] は、非常に単純な機能を提供します。
 
[[スマートポインタ]]シリーズの中では、[[std::auto_ptr]] は、非常に単純な機能を提供します。
 
[[std::auto_ptr]] は、[[C++]] の [[C++標準ライブラリ]] で提供されます。
 
[[std::auto_ptr]] は、[[C++]] の [[C++標準ライブラリ]] で提供されます。
  
 
[[std::auto_ptr]] のオブジェクトがスコープから外れたとき、保持するポインタを自動的に開放します。
 
[[std::auto_ptr]] のオブジェクトがスコープから外れたとき、保持するポインタを自動的に開放します。
 +
 +
[[std::auto_ptr]] は、[[C++11]] で非推奨となったため、[[std::unique_ptr]]をご利用ください。
  
 
[[スマートポインタ]] の概要については、[[スマートポインタ]] をご参照ください。
 
[[スマートポインタ]] の概要については、[[スマートポインタ]] をご参照ください。
  
 
== ヘッダファイル ==
 
== ヘッダファイル ==
 
 
<syntaxhighlight lang="cpp">
 
<syntaxhighlight lang="cpp">
 
#include <memory>
 
#include <memory>
行27: 行22:
  
 
== auto_ptr の使用例==
 
== auto_ptr の使用例==
 
 
=== ソースコード auto_ptr_1.cpp ===
 
=== ソースコード auto_ptr_1.cpp ===
  
行70: 行64:
  
 
=== コンパイル ===
 
=== コンパイル ===
 
 
<syntaxhighlight lang="bash">
 
<syntaxhighlight lang="bash">
 
g++ -I/usr/local/include -L/usr/local/lib auto_ptr_1.cpp -o auto_ptr_1
 
g++ -I/usr/local/include -L/usr/local/lib auto_ptr_1.cpp -o auto_ptr_1
行76: 行69:
  
 
=== 実行例 ===
 
=== 実行例 ===
 
 
<syntaxhighlight lang="bash">
 
<syntaxhighlight lang="bash">
 
% ./auto_ptr_1
 
% ./auto_ptr_1
行87: 行79:
  
 
== 代入により所有権が失われるの例==  
 
== 代入により所有権が失われるの例==  
 
 
[[std::auto_ptr]] を [[std::auto_ptr]] に代入すると所有権が代入された方へ移ります。
 
[[std::auto_ptr]] を [[std::auto_ptr]] に代入すると所有権が代入された方へ移ります。
  
行97: 行88:
  
 
=== ソースコード auto_ptr_2.cpp ===
 
=== ソースコード auto_ptr_2.cpp ===
 
 
<syntaxhighlight lang="cpp">
 
<syntaxhighlight lang="cpp">
 
#include <iostream>
 
#include <iostream>
行144: 行134:
  
 
=== コンパイル ===
 
=== コンパイル ===
 
 
<syntaxhighlight lang="bash">
 
<syntaxhighlight lang="bash">
 
g++ -I/usr/local/include -L/usr/local/lib auto_ptr_2.cpp -o auto_ptr_2
 
g++ -I/usr/local/include -L/usr/local/lib auto_ptr_2.cpp -o auto_ptr_2
行150: 行139:
  
 
=== 実行例 ===
 
=== 実行例 ===
 
 
* C のデストラクタが t のスコープが終わったときに、実行されていることが確認できます。
 
* C のデストラクタが t のスコープが終わったときに、実行されていることが確認できます。
 
* 変数 c がもっているポインタを確認すると 0 です。
 
* 変数 c がもっているポインタを確認すると 0 です。
行167: 行155:
  
 
== 関連項目 ==
 
== 関連項目 ==
 
 
* [[C++ライブラリ]]
 
* [[C++ライブラリ]]
 
* [[スマートポインタ]]
 
* [[スマートポインタ]]
 +
{{smartpointer}}
 +
<!-- vim: filetype=mediawiki -->

2014年1月3日 (金) 01:01時点における最新版

std::auto_ptr メモリの開放管理を自動的に行うためのものです。

読み方

std::auto_ptr
えすてぃーでぃー おーと ぴーてぃーあーる, おーとぽいんた

概要

スマートポインタシリーズの中では、std::auto_ptr は、非常に単純な機能を提供します。 std::auto_ptr は、C++C++標準ライブラリ で提供されます。

std::auto_ptr のオブジェクトがスコープから外れたとき、保持するポインタを自動的に開放します。

std::auto_ptr は、C++11 で非推奨となったため、std::unique_ptrをご利用ください。

スマートポインタ の概要については、スマートポインタ をご参照ください。

ヘッダファイル

#include <memory>

auto_ptr の使用例

ソースコード auto_ptr_1.cpp

#include <iostream>
#include <exception>
#include <memory>
 
class C {
        public:
                C() {
                }
                ~C() {
                        std::cout << __PRETTY_FUNCTION__ << std::endl;
                }
                void doit (){
                        std::cout << __PRETTY_FUNCTION__ << std::endl;
                }
};
 
void test_auto_ptr() {
        try {
                std::auto_ptr<C> c(new C());
                c->doit();
        } catch (std::exception &ex) {
                std::cerr << ex.what () << std::endl;
        }
}
 
int
main(int argc, char const* argv[])
{
        std::cout << "call test_auto_ptr" << std::endl;
        test_auto_ptr ();
        std::cout << "end test_auto_ptr" << std::endl;
 
        return 0;
}

コンパイル

g++ -I/usr/local/include -L/usr/local/lib auto_ptr_1.cpp -o auto_ptr_1

実行例

% ./auto_ptr_1
call test_auto_ptr
void C::doit()
C::~C()
end test_auto_ptr


代入により所有権が失われるの例

std::auto_ptrstd::auto_ptr に代入すると所有権が代入された方へ移ります。

std::auto_ptr<C> c(new C());
std::auto_ptr<C> t;
t = c; // ここで所有件が失われる。

ソースコード auto_ptr_2.cpp

#include <iostream>
#include <exception>
#include <memory>
 
class C {
        public:
                C() {
                }
                ~C() {
                        std::cout << __PRETTY_FUNCTION__ << std::endl;
                }
                void doit (){
                        std::cout << __PRETTY_FUNCTION__ << std::endl;
                }
};
 
void test_auto_ptr() {
        try {
                std::auto_ptr<C> c(new C());
                std::cout << "c.get()=" << c.get() << std::endl;
                {
                        std::auto_ptr<C> t;
                        t = c; // ここで所有件が失われる。
                        std::cout << "t.get()=" << t.get() << std::endl;
                } // ここのスコープが終わることで、 t が C のインスタンスを開放する。
		// t に所有権を移したので、ポインタは0になる
                std::cout << "c.get()=" << c.get() << std::endl;
                c->doit(); // ここで doit が実行できてるのがおかしい。
        } catch (std::exception &ex) {
                std::cerr << ex.what () << std::endl;
        }
}
 
int
main(int argc, char const* argv[])
{
        std::cout << "call test_auto_ptr" << std::endl;
        test_auto_ptr ();
        std::cout << "end test_auto_ptr" << std::endl;
 
        return 0;
}

コンパイル

g++ -I/usr/local/include -L/usr/local/lib auto_ptr_2.cpp -o auto_ptr_2

実行例

  • C のデストラクタが t のスコープが終わったときに、実行されていることが確認できます。
  • 変数 c がもっているポインタを確認すると 0 です。
  • なぜか、 doit() が呼べてますが、不正な処理でしょう。
% ./auto_ptr_2
call test_auto_ptr
c.get()=0x28464100
t.get()=0x28464100
C::~C()
c.get()=0
void C::doit()
end test_auto_ptr

関連項目