std::auto ptr

提供: C++入門
移動: 案内検索
スポンサーリンク

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

関連項目




スポンサーリンク