📘 Learning Objectives

After completing this chapter, you will: - Master advanced memory management techniques - Understand custom allocators and memory pools - Learn about memory alignment and optimization - Master smart pointer advanced usage - Understand memory debugging and profiling

🎯 Key Concepts

1. Custom Allocators

  • Allocator concept: Custom memory allocation
  • Memory pools: Efficient memory management
  • Stack allocators: Stack-based allocation
  • Arena allocators: Arena-based allocation
  • Allocator traits: Allocator type traits

2. Memory Alignment

  • Alignment requirements: Memory alignment rules
  • Aligned allocation: Aligned memory allocation
  • Cache optimization: CPU cache optimization
  • SIMD alignment: SIMD instruction alignment
  • Custom alignment: User-defined alignment

3. Smart Pointers

  • unique_ptr: Exclusive ownership
  • shared_ptr: Shared ownership
  • weak_ptr: Non-owning references
  • Custom deleters: Custom cleanup functions
  • Smart pointer patterns: Advanced usage patterns

4. Memory Optimization

  • Memory layout: Object memory layout
  • Cache optimization: CPU cache efficiency
  • Memory fragmentation: Fragmentation reduction
  • Memory pooling: Memory pool optimization
  • Zero-cost abstractions: Zero-overhead abstractions

5. Memory Debugging

  • Memory leaks: Leak detection
  • Buffer overflows: Overflow detection
  • Use-after-free: Dangling pointer detection
  • Double-free: Double deletion detection
  • Memory profiling: Memory usage analysis

🧩 Practice Exercises

Exercise 26.1: Custom Allocators

Implement custom memory allocators.

Exercise 26.2: Memory Alignment

Work with aligned memory allocation.

Exercise 26.3: Smart Pointers

Use advanced smart pointer techniques.

Exercise 26.4: Memory Optimization

Optimize memory usage and performance.

💻 Code Examples

Custom Allocator

#include <iostream>
#include <memory>

template<typename T>
class StackAllocator {
private:
    char* memory;
    size_t size;
    size_t offset;
public:
    StackAllocator(size_t s) : size(s), offset(0) {
        memory = static_cast<char*>(std::aligned_alloc(alignof(T), size));
    }

    ~StackAllocator() {
        std::free(memory);
    }

    T* allocate(size_t n) {
        size_t bytes = n * sizeof(T);
        if (offset + bytes > size) {
            throw std::bad_alloc();
        }
        T* ptr = reinterpret_cast<T*>(memory + offset);
        offset += bytes;
        return ptr;
    }

    void deallocate(T*, size_t) {
        // Stack allocator doesn't support deallocation
    }
};

int main() {
    StackAllocator<int> allocator(1024);
    int* ptr = allocator.allocate(10);

    for (int i = 0; i < 10; ++i) {
        ptr[i] = i;
    }

    for (int i = 0; i < 10; ++i) {
        std::cout << ptr[i] << " ";
    }
    std::cout << std::endl;

    return 0;
}

Smart Pointers

#include <iostream>
#include <memory>
#include <vector>

class Resource {
public:
    Resource(int id) : id_(id) {
        std::cout << "Resource " << id_ << " created" << std::endl;
    }

    ~Resource() {
        std::cout << "Resource " << id_ << " destroyed" << std::endl;
    }

    int getId() const { return id_; }

private:
    int id_;
};

int main() {
    // unique_ptr
    auto resource1 = std::make_unique<Resource>(1);
    std::cout << "Resource 1 ID: " << resource1->getId() << std::endl;

    // shared_ptr
    auto resource2 = std::make_shared<Resource>(2);
    std::cout << "Resource 2 ID: " << resource2->getId() << std::endl;

    // weak_ptr
    std::weak_ptr<Resource> weak_resource = resource2;
    if (auto locked = weak_resource.lock()) {
        std::cout << "Weak pointer locked, ID: " << locked->getId() << std::endl;
    }

    return 0;
}

🎓 Key Takeaways

  1. Use custom allocators for specialized memory management
  2. Consider memory alignment for performance optimization
  3. Master smart pointers for automatic memory management
  4. Optimize memory layout for cache efficiency
  5. Debug memory issues with proper tools and techniques

🔗 Next Steps

After mastering memory management, proceed to Chapter 27 to learn about performance optimization.

📚 Additional Resources

  • C++ Reference: Memory Management
  • C++ Core Guidelines: Memory Management
  • Practice with memory optimization techniques