Skip to Content
C++の紹介 (Japanese Edition)
book

C++の紹介 (Japanese Edition)

by Frances Buontempo
March 2026
Intermediate
348 pages
4h 30m
Japanese
O'Reilly Media, Inc.
Content preview from C++の紹介 (Japanese Edition)

第12章 std::unique_ptr によるメモリ管理

この作品はAIを使って翻訳されている。ご意見、ご感想をお待ちしている:translation-feedback@oreilly.com

この章では、ヒープ上のオブジェクトを効果的に扱い、使用後にメモリが解放されるようにする方法について説明する。 第9章charポインタを第11章std::stringの詳細を学んだ。 std::stringcharポインタよりも高い抽象化レベルを提供し、作業を容易にする。 std::vectorと同様にstd::stringは自動的にサイズ変更を行い、スコープ外に出ると片付けを行うため、自分で処理する必要はない。 C++はオブジェクトの使用終了時にクリーンアップを支援する他の機能も提供する。 ヒープ上にで割り当てられたオブジェクトをクリーンアップしたい場合、スマートポインタを使うことで低レベルな手動メモリ管理を避けられる。スマートポインタとは、ポインタのように動作するが自動でクリーンアップを行う型だ。 スマートポインタにはいくつかの種類があるが、まずは最も使いやすいものから始めよう。 そうすれば第13章でスマートポインタを使って取引ゲームを拡張できるようになる。

では、なぜヒープを使いたいと思うのか? これまで、std::stringstd::vectorがバックグラウンドでヒープを使っていたとしても、直接自分でこれを行う必要はなかった。 第13章では、ヒープオブジェクトの重要なユースケースを示す:クラス型に応じて振る舞いを変えられることだ。 もう一つのユースケースは、サイズが変わるオブジェクトのためだ。 整数のように固定サイズのオブジェクトも多い。 一方、std :: vectorとstd::stringはどちらも要素数を可変にできる。コンパイル時に必要なサイズが分からない場合、ヒープを使って動的に割り当てるのだ。

この章では、動的メモリへのポインタを安全に扱うスマートな方法を説明する。 コンストラクタについてさらに学び、コンパイル時にコードをテストする新しい方法を見る。 また参照の復習も行い、この章の終わりまでに次の章で改良版トレーディングゲームを書く準備が整う。 第10章から、構成要素を使ってクラスを作成・移動・コピーする方法を学んできたが、この章ではクラスを使って振る舞いを変化させる準備を整える。

std::unique_ptrの作成

<MEMORY>ヘッダは様々なスマートポインタと、それらを作成する関数を提供する。 このセクションではstd::unique_ptrの使用方法を示し、その後「スマートポインタの詳細」で他のスマートポインタのタイプの概要を簡単に示す。 スマートポインタはメモリ管理を代行するため、生のポインタより扱いやすい。 ヒープメモリを取得したら、使用後は解放すべきだ。そうしないと、プログラムがメモリを保持し続け、システムがメモリ不足に陥る可能性がある。 スマートポインタは他のリソースの管理にも使える。

std::unique_ptrstd::make_unique と密接に関連している。 std::make_unique関数はnewを呼び出し、スマートポインタのデストラクタはデフォルトで削除を呼び出す。 (これらの関数については「std::string の仕組み」で触れた) スマートポインタを使えば、これらの関数を直接扱う必要はない。

新しいmain.cppファイルを作成する。 メモリヘッダとStockヘッダをインクルードする: ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

エージェントメッシュ (Japanese Edition)

エージェントメッシュ (Japanese Edition)

Eric Broda, Davis Broda

Publisher Resources

ISBN: 0642572352653