C++でのnewキーワードの基礎知識
newとdeleteの基本的な使い方
C++におけるnewキーワードは、動的メモリ割り当てを行うための演算子です。メモリの確保と解放は常にペアで考える必要があります。
// 単一オブジェクトの動的確保 int* ptr = new int; // 初期化なしの確保 int* ptr2 = new int(10); // 値10で初期化 delete ptr; // メモリの解放 delete ptr2; // 配列の動的確保 int* arr = new int[5]; // 大きさ5の配列を確保 delete[] arr; // 配列のメモリ解放
スタックメモリとヒープメモリの違い
メモリ領域には大きく分けて2種類があります:
- スタックメモリ:
- 自動的に確保・解放される
- サイズは比較的小さい(通常1MB程度)
- アクセス速度が速い
void stackExample() {
int x = 10; // スタックメモリに確保
double y = 20.5; // スタックメモリに確保
} // x, yは自動的に解放される
- ヒープメモリ:
- プログラマが明示的に確保・解放する
- 大きなサイズを確保可能
- アクセス速度は比較的遅い
void heapExample() {
int* x = new int(10); // ヒープメモリに確保
delete x; // 明示的な解放が必要
}
メモリリークが発生する仕組み
メモリリークは、確保したメモリを適切に解放しない場合に発生します。主な原因は以下の通りです:
- 基本的なメモリリーク:
void basicLeak() {
int* ptr = new int(10);
// deleteを忘れるとメモリリーク
}
- 例外発生時のメモリリーク:
void exceptionLeak() {
int* ptr = new int(10);
// 例外が発生するとdeleteが実行されない
throw std::runtime_error("エラー発生");
delete ptr; // この行は実行されない
}
- ポインタの上書きによるメモリリーク:
void pointerOverwriteLeak() {
int* ptr = new int(10);
ptr = new int(20); // 最初のメモリがリークする
delete ptr; // 2番目のメモリのみ解放
}
メモリリークを防ぐベストプラクティス:
- スマートポインタの使用(後述)
- RAII原則の遵守
- 例外安全なコードの作成
- デストラクタでの適切なクリーンアップ
以上が、C++でのnewキーワードの基礎知識です。次のセクションでは、これらの問題を解決するための実践的なテクニックを説明します。
新しいを使う際の7つの実践テクニック
1. スマートポインタを活用したメモリ管理
モダンC++では、生のポインタの代わりにスマートポインタを使用することが推奨されています。
#include <memory>
class Resource {
public:
Resource() { std::cout << "Resource constructed\n"; }
~Resource() { std::cout << "Resource destroyed\n"; }
};
void smartPointerExample() {
// unique_ptrの使用例
std::unique_ptr<Resource> res = std::make_unique<Resource>();
// 自動的にデストラクタが呼ばれる
// shared_ptrの使用例
std::shared_ptr<Resource> shared1 = std::make_shared<Resource>();
{
auto shared2 = shared1; // 参照カウントが2になる
} // shared2のスコープを抜けると参照カウントが1に
} // 全てのリソースが自動的に解放される
2. RAIIパターンの実装方法
RAIIは「Resource Acquisition Is Initialization」の略で、リソースの確保を初期化時に行い、解放をデストラクタで行うC++のイディオムです。
class FileHandler {
private:
FILE* file;
public:
FileHandler(const char* filename) {
file = fopen(filename, "r");
if (!file) throw std::runtime_error("File open failed");
}
~FileHandler() {
if (file) fclose(file);
}
// ムーブ操作のサポート
FileHandler(FileHandler&& other) noexcept : file(other.file) {
other.file = nullptr;
}
FileHandler& operator=(FileHandler&& other) noexcept {
if (this != &other) {
if (file) fclose(file);
file = other.file;
other.file = nullptr;
}
return *this;
}
// コピーの禁止
FileHandler(const FileHandler&) = delete;
FileHandler& operator=(const FileHandler&) = delete;
};
3. 例外安全な新しい使い方
例外安全なコードを書くためには、以下のような方法があります:
class ExceptionSafe {
private:
int* data;
size_t size;
public:
ExceptionSafe(size_t n) : data(nullptr), size(0) {
// 二段階構築パターン
data = new int[n]; // 例外が発生しても他のメンバは未初期化
size = n; // 例外が発生しなければサイズを設定
}
~ExceptionSafe() {
delete[] data;
}
};
// スマートポインタを使用した例外安全な実装
class BetterExceptionSafe {
private:
std::unique_ptr<int[]> data;
size_t size;
public:
BetterExceptionSafe(size_t n) : data(std::make_unique<int[]>(n)), size(n) {}
// デストラクタは自動生成で十分
};
4. 配列確保時のベストプラクティス
配列の動的確保には、以下のベストプラクティスがあります:
// vector使用推奨(最も安全)
std::vector<int> vec(100);
// どうしても生の配列が必要な場合
std::unique_ptr<int[]> arr = std::make_unique<int[]>(100);
// 2次元配列の場合
std::vector<std::vector<int>> matrix(rows, std::vector<int>(cols));
// パフォーマンスが重要な場合の1次元配列による2次元配列の実装
class Matrix {
private:
std::unique_ptr<int[]> data;
size_t rows, cols;
public:
Matrix(size_t r, size_t c) : data(std::make_unique<int[]>(r * c)), rows(r), cols(c) {}
int& at(size_t i, size_t j) {
return data[i * cols + j];
}
};
5. メモリプールを使用した効率的な確保
頻繁なnew/deleteを避けるためのメモリプール実装:
template<typename T, size_t BlockSize = 4096>
class MemoryPool {
private:
struct Block {
std::array<T, BlockSize> data;
size_t used = 0;
std::unique_ptr<Block> next;
};
std::unique_ptr<Block> head = std::make_unique<Block>();
public:
T* allocate() {
Block* current = head.get();
while (current->used == BlockSize) {
if (!current->next) {
current->next = std::make_unique<Block>();
}
current = current->next.get();
}
return ¤t->data[current->used++];
}
// 注: 簡略化のため、解放処理は省略
};
6. 配置newの活用シーン
配置new(placement new)は、既に確保済みのメモリ領域に対してオブジェクトを構築する機能です:
#include <new>
class Object {
int value;
public:
Object(int v) : value(v) {}
};
void placementNewExample() {
// メモリ領域の確保
alignas(Object) char buffer[sizeof(Object)];
// 配置newによるオブジェクトの構築
Object* obj = new (buffer) Object(42);
// 明示的なデストラクタ呼び出し
obj->~Object();
// bufferの解放は不要(スタック上にあるため)
}
7. カスタムアロケータの実装方法
STLコンテナで使用できるカスタムアロケータの実装例:
template<typename T>
class CustomAllocator {
public:
using value_type = T;
CustomAllocator() noexcept {}
template<typename U>
CustomAllocator(const CustomAllocator<U>&) noexcept {}
T* allocate(std::size_t n) {
if (n > std::numeric_limits<std::size_t>::max() / sizeof(T)) {
throw std::bad_alloc();
}
if (auto p = static_cast<T*>(std::malloc(n * sizeof(T)))) {
return p;
}
throw std::bad_alloc();
}
void deallocate(T* p, std::size_t) noexcept {
std::free(p);
}
};
// 使用例
std::vector<int, CustomAllocator<int>> vec;
以上が、newを使用する際の7つの実践的なテクニックです。これらを適切に組み合わせることで、メモリリークのない安全なプログラムを作成できます。
モダンC++でのメモリ管理手法
std::unique_ptrの効果的な使い方
std::unique_ptrは、単一の所有権を表現するスマートポインタです。リソースの排他的な所有権を管理する際に最適です。
#include <memory>
#include <iostream>
class Resource {
public:
Resource(int id) : id_(id) {
std::cout << "Resource " << id_ << " constructed\n";
}
~Resource() {
std::cout << "Resource " << id_ << " destroyed\n";
}
void use() const {
std::cout << "Using resource " << id_ << "\n";
}
private:
int id_;
};
// 関数での使用例
std::unique_ptr<Resource> createResource(int id) {
return std::make_unique<Resource>(id);
}
void useResource(std::unique_ptr<Resource> resource) {
resource->use();
// 関数を抜けると自動的に解放される
}
// コンテナでの使用例
class ResourceManager {
private:
std::vector<std::unique_ptr<Resource>> resources_;
public:
void addResource(int id) {
resources_.push_back(std::make_unique<Resource>(id));
}
// ムーブセマンティクスを活用した追加
void transferResource(std::unique_ptr<Resource> resource) {
resources_.push_back(std::move(resource));
}
};
std::shared_ptrの適切な使用シーン
std::shared_ptrは、複数のオブジェクトで共有される可能性があるリソースの管理に使用します。
#include <memory>
// 循環参照を避けるための前方宣言
class Child;
class Parent;
class Parent {
public:
Parent() {
std::cout << "Parent constructed\n";
}
~Parent() {
std::cout << "Parent destroyed\n";
}
// weak_ptrを使用して循環参照を防ぐ
std::vector<std::weak_ptr<Child>> children;
void addChild(std::shared_ptr<Child> child) {
children.push_back(child);
}
void printChildren() {
for (auto& weakChild : children) {
if (auto child = weakChild.lock()) {
// childを使用
}
}
}
};
class Child {
public:
Child(std::shared_ptr<Parent> parent) : parent_(parent) {
std::cout << "Child constructed\n";
}
~Child() {
std::cout << "Child destroyed\n";
}
private:
std::shared_ptr<Parent> parent_;
};
// 使用例
void sharedPtrExample() {
auto parent = std::make_shared<Parent>();
{
auto child1 = std::make_shared<Child>(parent);
auto child2 = std::make_shared<Child>(parent);
parent->addChild(child1);
parent->addChild(child2);
} // child1とchild2はここで解放される
} // parentは最後に解放される
std::make_unique と std::make_shared の使い方
これらのヘルパー関数を使用することで、より安全で効率的なメモリ管理が可能になります:
class ComplexResource {
public:
ComplexResource(int x, double y, std::string z)
: x_(x), y_(y), z_(z) {}
private:
int x_;
double y_;
std::string z_;
};
// make_uniqueの使用例
void uniquePtrCreation() {
// 推奨される方法
auto resource1 = std::make_unique<ComplexResource>(1, 2.0, "three");
// 非推奨の方法(例外安全でない)
std::unique_ptr<ComplexResource> resource2(new ComplexResource(1, 2.0, "three"));
}
// make_sharedの使用例
void sharedPtrCreation() {
// 推奨される方法(メモリ割り当てが1回で済む)
auto resource1 = std::make_shared<ComplexResource>(1, 2.0, "three");
// 非推奨の方法(メモリ割り当てが2回必要)
std::shared_ptr<ComplexResource> resource2(new ComplexResource(1, 2.0, "three"));
// 配列の作成
auto arr1 = std::make_unique<int[]>(10);
auto arr2 = std::make_shared<int[]>(10);
}
// パフォーマンスの最適化例
struct LargeObject {
std::array<char, 1024> data;
};
void optimizedCreation() {
// 大量のオブジェクトを作成する場合
std::vector<std::shared_ptr<LargeObject>> objects;
objects.reserve(1000); // メモリの再割り当てを避ける
for (int i = 0; i < 1000; ++i) {
objects.push_back(std::make_shared<LargeObject>());
}
}
以上が、モダンC++におけるメモリ管理の主要な手法です。これらを適切に使用することで、メモリリークを防ぎながら効率的なプログラムを作成できます。
メモリリークを防ぐデバッグテクニック
Visual Studio メモリプロファイラーの使い方
Visual Studioのメモリプロファイラーを使用したメモリリーク検出の手順:
// メモリリークを含むサンプルコード
class LeakyClass {
int* data;
public:
LeakyClass() : data(new int[1000]) {}
// デストラクタの実装を忘れている
};
void leakExample() {
LeakyClass* obj = new LeakyClass();
// deleteを忘れている
}
int main() {
// プロファイリングの開始ポイント
leakExample();
// プロファイリングの終了ポイント
}
プロファイリングの手順:
- デバッグ → パフォーマンスプロファイラー
- メモリ使用量を選択
- アプリケーションを実行
- スナップショットを取得
- メモリリークの分析
Valgrindを使用したメモリリーク検出
Linuxでのメモリリーク検出ツールValgrindの使用方法:
# コンパイル(デバッグ情報付き) g++ -g memory_leak.cpp -o memory_leak # Valgrindでの実行 valgrind --leak-check=full ./memory_leak
よくあるValgrindの出力とその解釈:
// メモリリークを含むコード
void valgrindExample() {
int* ptr1 = new int[10]; // 確実なリーク
int* ptr2 = new int[20]; // 間接的なリーク
ptr2 = ptr1; // ptr2が指していたメモリがリーク
}
/*
Valgrind出力の例:
==12345== HEAP SUMMARY:
==12345== in use at exit: 120 bytes in 2 blocks
==12345== total heap usage: 2 allocs, 0 frees
==12345==
==12345== LEAK SUMMARY:
==12345== definitely lost: 40 bytes in 1 blocks
==12345== indirectly lost: 80 bytes in 1 blocks
*/
単体テストでのメモリリークチェック方法
Google Testを使用したメモリリーク検出の例:
#include <gtest/gtest.h>
#include <memory>
class MemoryLeakTest : public ::testing::Test {
protected:
void SetUp() override {
// テスト開始時のメモリ使用量を記録
}
void TearDown() override {
// テスト終了時のメモリ使用量を確認
}
};
// メモリリークを検出するテスト
TEST_F(MemoryLeakTest, NoLeaksInClass) {
{
MyClass obj;
obj.allocateMemory();
} // objのデストラクタが呼ばれる
// メモリ使用量をチェック
EXPECT_EQ(getCurrentMemoryUsage(), getInitialMemoryUsage());
}
// カスタムメモリリーク検出器
class MemoryLeakDetector {
private:
size_t allocated = 0;
public:
void* allocate(size_t size) {
allocated += size;
return malloc(size);
}
void deallocate(void* ptr, size_t size) {
allocated -= size;
free(ptr);
}
size_t getCurrentAllocation() const {
return allocated;
}
};
// グローバルな検出器
MemoryLeakDetector globalDetector;
// オーバーロードされたnew/delete演算子
void* operator new(size_t size) {
return globalDetector.allocate(size);
}
void operator delete(void* ptr, size_t size) noexcept {
globalDetector.deallocate(ptr, size);
}
// テストでの使用例
TEST(MemoryTest, CheckNoLeaks) {
size_t before = globalDetector.getCurrentAllocation();
{
// メモリを使用するコード
std::vector<int> vec(1000);
}
size_t after = globalDetector.getCurrentAllocation();
EXPECT_EQ(before, after);
}
これらのデバッグテクニックを組み合わせることで、効果的にメモリリークを検出し、修正することができます。
実践的なコード例で学ぶnewの応用
大規模配列の効率的な確保と解放
大規模なデータを扱う際の効率的なメモリ管理手法を示します:
#include <memory>
#include <vector>
#include <chrono>
#include <iostream>
// 大規模配列を効率的に管理するクラス
template<typename T>
class LargeArray {
private:
std::unique_ptr<T[]> data_;
size_t size_;
// メモリアライメントを考慮した補助関数
static size_t alignedSize(size_t requested) {
const size_t alignment = 64; // キャッシュライン考慮
return (requested + alignment - 1) & ~(alignment - 1);
}
public:
explicit LargeArray(size_t size)
: size_(alignedSize(size))
, data_(std::make_unique<T[]>(size_)) {
}
// チャンク単位での初期化
void initialize() {
constexpr size_t CHUNK_SIZE = 1024 * 1024; // 1MB単位
for (size_t i = 0; i < size_; i += CHUNK_SIZE) {
size_t chunk = std::min(CHUNK_SIZE, size_ - i);
std::fill_n(&data_[i], chunk, T());
}
}
// 要素アクセス
T& operator[](size_t index) {
return data_[index];
}
const T& operator[](size_t index) const {
return data_[index];
}
};
// パフォーマンス比較用の関数
void performanceComparison() {
constexpr size_t SIZE = 1024 * 1024 * 100; // 100MB
auto start = std::chrono::high_resolution_clock::now();
// 通常の配列確保
auto regular = new int[SIZE];
delete[] regular;
auto regularTime = std::chrono::high_resolution_clock::now() - start;
start = std::chrono::high_resolution_clock::now();
// 最適化された配列確保
LargeArray<int> optimized(SIZE);
auto optimizedTime = std::chrono::high_resolution_clock::now() - start;
std::cout << "Regular allocation time: "
<< std::chrono::duration_cast<std::chrono::milliseconds>(regularTime).count()
<< "ms\n";
std::cout << "Optimized allocation time: "
<< std::chrono::duration_cast<std::chrono::milliseconds>(optimizedTime).count()
<< "ms\n";
}
クラスメンバでのnewの適切な使用方法
クラスでメモリを動的に管理する際のベストプラクティスを示します:
class ResourceManager {
private:
// リソースを管理するための構造体
struct Resource {
std::unique_ptr<int[]> data;
size_t size;
Resource(size_t s) : data(std::make_unique<int[]>(s)), size(s) {}
};
// リソースのプール
std::vector<std::unique_ptr<Resource>> resources_;
// スレッドセーフな操作のためのミューテックス
mutable std::mutex mutex_;
public:
// リソースの追加(スレッドセーフ)
size_t addResource(size_t size) {
std::lock_guard<std::mutex> lock(mutex_);
resources_.push_back(std::make_unique<Resource>(size));
return resources_.size() - 1;
}
// リソースの取得(スレッドセーフ)
int* getResource(size_t index) const {
std::lock_guard<std::mutex> lock(mutex_);
if (index >= resources_.size()) {
throw std::out_of_range("Invalid resource index");
}
return resources_[index]->data.get();
}
// ムーブセマンティクスのサポート
ResourceManager() = default;
ResourceManager(ResourceManager&&) = default;
ResourceManager& operator=(ResourceManager&&) = default;
// コピーの禁止
ResourceManager(const ResourceManager&) = delete;
ResourceManager& operator=(const ResourceManager&) = delete;
};
循環参照を防ぐスマートポインタの実装
循環参照を防ぐための実践的な実装例を示します:
#include <memory>
#include <string>
// 前方宣言
class Node;
using NodePtr = std::shared_ptr<Node>;
using WeakNodePtr = std::weak_ptr<Node>;
class Node {
private:
std::string data_;
std::vector<NodePtr> strong_children_; // 強い参照
std::vector<WeakNodePtr> weak_children_; // 弱い参照
WeakNodePtr parent_; // 親への弱い参照
public:
explicit Node(std::string data) : data_(std::move(data)) {}
// 子ノードの追加(強い参照)
void addStrongChild(NodePtr child) {
strong_children_.push_back(std::move(child));
}
// 子ノードの追加(弱い参照)
void addWeakChild(NodePtr child) {
weak_children_.push_back(child);
}
// 親ノードの設定
void setParent(NodePtr parent) {
parent_ = parent;
}
// 到達可能な全ノードの表示
void printReachableNodes() const {
std::cout << "Node: " << data_ << "\n";
// 強い参照の子ノード
std::cout << "Strong children:\n";
for (const auto& child : strong_children_) {
std::cout << " - " << child->data_ << "\n";
}
// 弱い参照の子ノード(有効なもののみ)
std::cout << "Weak children:\n";
for (const auto& weak_child : weak_children_) {
if (auto child = weak_child.lock()) {
std::cout << " - " << child->data_ << "\n";
}
}
// 親ノード(存在する場合)
if (auto p = parent_.lock()) {
std::cout << "Parent: " << p->data_ << "\n";
}
}
};
// 使用例
void demonstrateCircularReferencePrevention() {
// ツリー構造の作成
auto root = std::make_shared<Node>("root");
auto child1 = std::make_shared<Node>("child1");
auto child2 = std::make_shared<Node>("child2");
// 関係の設定
root->addStrongChild(child1); // rootはchild1への強い参照を持つ
root->addWeakChild(child2); // rootはchild2への弱い参照を持つ
child1->setParent(root); // child1はrootへの弱い参照を持つ
child2->setParent(root); // child2はrootへの弱い参照を持つ
// 構造の確認
root->printReachableNodes();
}
これらの実装例は、実際のプロジェクトで遭遇する可能性のある課題に対する具体的な解決策を提供します。特に大規模なメモリ管理、スレッドセーフな実装、循環参照の防止など、実践的な場面で役立つパターンを示しています。