当前位置: 首页 > news >正文

C++学习:六个月从基础到就业——STL:分配器与设计原理

C++学习:六个月从基础到就业——STL:分配器与设计原理

本文是我C++学习之旅系列的第三十篇技术文章,也是第二阶段"C++进阶特性"的第九篇,主要介绍C++ STL中的分配器设计原理与实现。查看完整系列目录了解更多内容。

引言

在之前的STL系列文章中,我们已经讨论了STL的三个主要组件:容器、迭代器和算法,以及函数对象与适配器。本文将探讨STL设计中另一个不太引人注目但至关重要的组件——分配器(Allocator)。

分配器是STL的核心组件之一,负责容器的内存管理。虽然它通常被隐藏在容器实现的背后,但了解其工作原理对深入理解STL的设计思想、优化程序性能、处理特殊内存需求都有很大帮助。特别是在资源受限的环境、高性能计算或特定硬件平台上,自定义分配器可以显著提升应用程序的性能和稳定性。

本文将详细介绍STL分配器的设计原理、接口规范、标准实现以及如何创建自定义分配器。我们还将讨论一些现代C++中关于分配器的发展和最佳实践。

分配器的基本概念

什么是分配器?

分配器是一个封装了内存分配和释放策略的类,它为STL容器提供了统一的内存管理接口。简单来说,分配器负责以下两个主要工作:

  1. 内存分配:为容器中的元素分配内存
  2. 内存释放:释放不再需要的内存

分配器使得容器的内存管理与其算法逻辑分离,符合单一职责原则,同时提供了灵活性,允许用户根据需要替换默认的内存管理策略。

分配器在STL中的位置

STL的基本架构可以概括为以下组件:

  • 容器(Containers):存储和管理数据的对象
  • 迭代器(Iterators):提供访问容器元素的接口
  • 算法(Algorithms):操作容器中数据的函数
  • 函数对象(Functors):封装可调用的对象
  • 适配器(Adapters):转换接口以适应不同需求
  • 分配器(Allocators):管理容器的内存分配

分配器是容器与底层内存管理之间的桥梁,所有STL容器都接受一个分配器类型参数,默认使用std::allocator<T>

template <class T, class Allocator = std::allocator<T>>
class vector;template <class T, class Allocator = std::allocator<T>>
class list;template <class T, class Allocator = std::allocator<T>>
class deque;

为什么需要分配器?

分配器的存在有以下几个重要理由:

  1. 抽象内存管理:隐藏底层内存管理细节,使容器实现更加清晰
  2. 提供定制能力:允许用户根据特定需求定制内存分配策略
  3. 适应不同环境:使STL可以在不同的内存模型和平台上工作
  4. 性能优化:通过专用分配器提高特定场景下的内存分配性能
  5. 特殊内存需求:支持对齐内存、共享内存、内存池等特殊需求

分配器的接口规范

C++17之前的分配器接口

在C++17之前,一个符合标准的分配器需要实现以下接口:

template <class T>
class SimpleAllocator {
public:// 类型定义typedef T value_type;typedef T* pointer;typedef const T* const_pointer;typedef T& reference;typedef const T& const_reference;typedef std::size_t size_type;typedef std::ptrdiff_t difference_type;// rebind结构,允许容器为不同类型分配内存template <class U>struct rebind {typedef SimpleAllocator<U> other;};// 构造函数SimpleAllocator() noexcept;// 复制构造函数SimpleAllocator(const SimpleAllocator&) noexcept;// 从其他类型分配器构造template <class U>SimpleAllocator(const SimpleAllocator<U>&) noexcept;// 析构函数~SimpleAllocator();// 分配内存pointer allocate(size_type n, const void* hint = nullptr);// 释放内存void deallocate(pointer p, size_type n);// 构造对象void construct(pointer p, const_reference val);// 析构对象void destroy(pointer p);// 获取可寻址的最大对象大小size_type max_size() const noexcept;// 比较两个分配器是否相等bool operator==(const SimpleAllocator& other) const noexcept;// 比较两个分配器是否不等bool operator!=(const SimpleAllocator& other) const noexcept;
};

C++17及以后的分配器接口

C++17简化了分配器的要求,移除了一些冗余的成员,现在的分配器最低要求是:

template <class T>
class ModernAllocator {
public:// 类型定义using value_type = T;// 构造函数ModernAllocator() noexcept = default;// 析构函数~ModernAllocator() = default;// 从其他类型分配器构造template <class U>ModernAllocator(const ModernAllocator<U>&) noexcept;// 分配未初始化的存储T* allocate(std::size_t n);// 释放存储void deallocate(T* p, std::size_t n);// C++17移除了construct和destroy方法,使用std::allocator_traits代替
};

C++17引入了std::allocator_traits来简化分配器的使用并提供默认行为,这让自定义分配器的实现变得更加简单。

std::allocator_traits

std::allocator_traits是一个用于完成分配器接口的模板类,它为分配器提供了标准化的接口和默认实现:

namespace std {template <class Alloc>struct allocator_traits {// 各种类型定义using allocator_type = Alloc;using value_type = typename Alloc::value_type;using pointer = /* 取决于Alloc是否定义了pointer */;using const_pointer = /* 取决于Alloc是否定义了const_pointer */;using void_pointer = /* ... */;using const_void_pointer = /* ... */;using difference_type = /* ... */;using size_type = /* ... */;// rebind结构template <class T>using rebind_alloc = /* 取决于Alloc是否定义了rebind */;template <class T>using rebind_traits = allocator_traits<rebind_alloc<T>>;// 分配和释放方法static pointer allocate(Alloc& a, size_type n);static pointer allocate(Alloc& a, size_type n, const_void_pointer hint);static void deallocate(Alloc& a, pointer p, size_type n);// 构造和析构对象template <class T, class... Args>static void construct(Alloc& a, T* p, Args&&... args);template <class T>static void destroy(Alloc& a, T* p);// 其他成员函数static size_type max_size(const Alloc& a) noexcept;static Alloc select_on_container_copy_construction(const Alloc& a);// 类型特征static constexpr bool propagate_on_container_copy_assignment = /* ... */;static constexpr bool propagate_on_container_move_assignment = /* ... */;static constexpr bool propagate_on_container_swap = /* ... */;static constexpr bool is_always_equal = /* ... */;};
}

allocator_traits通过SFINAE(替换失败不是错误)机制,会检查分配器是否提供了特定的方法或类型,如果没有则提供一个默认实现。

标准分配器 std::allocator

STL的默认分配器是std::allocator,它是一个相对简单的封装,直接使用操作系统的堆内存管理:

#include <iostream>
#include <memory>
#include <vector>
#include <string>void exploreStdAllocator() {// 创建一个int类型的分配器std::allocator<int> intAlloc;// 分配5个int的空间int* ints = intAlloc.allocate(5);// 在分配的空间中构造对象for (int i = 0; i < 5; ++i) {intAlloc.construct(ints + i, i * 10);}// 使用分配的内存std::cout << "Allocated integers: ";for (int i = 0; i < 5; ++i) {std::cout << ints[i] << " ";}std::cout << std::endl;// 析构对象for (int i = 0; i < 5; ++i) {intAlloc.destroy(ints + i);}// 释放内存intAlloc.deallocate(ints, 5);// 使用allocator_traitsstd::allocator<std::string> strAlloc;auto traits = std::allocator_traits<std::allocator<std::string>>();// 分配一个string的空间std::string* str = traits.allocate(strAlloc, 1);// 使用traits构造对象traits.construct(strAlloc, str, "Hello, allocator!");std::cout << "Allocated string: " << *str << std::endl;// 析构并释放traits.destroy(strAlloc, str);traits.deallocate(strAlloc, str, 1);
}int main() {exploreStdAllocator();return 0;
}

std::allocator的内部实现

虽然具体实现因标准库而异,但一般来说,std::allocator内部直接使用operator newoperator delete进行内存分配和释放:

template <class T>
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*>(::operator new(n * sizeof(T))))return p;throw std::bad_alloc();
}void deallocate(T* p, std::size_t n) noexcept {::operator delete(p);
}

std::allocator的优点是简单直接,它将内存管理委托给底层的系统分配器,而不添加额外的开销或优化。

std::allocator的局限性

标准分配器有以下局限性:

  1. 性能开销:每次分配都会调用系统内存分配函数,有一定开销
  2. 内存碎片:频繁的小块内存分配可能导致内存碎片
  3. 缺乏定制能力:无法针对特定容器或使用场景进行优化
  4. 不支持特殊内存:不直接支持共享内存、内存映射文件等
  5. 缓存不友好:没有考虑CPU缓存的影响

这些局限性也正是为什么STL允许自定义分配器的原因。

分配器的设计原理

设计目标

STL分配器的设计遵循以下原则:

  1. 效率:最小化内存分配和管理的开销
  2. 通用性:能够适应不同类型的容器需求
  3. 可扩展性:允许用户定义符合特定需求的自定义分配器
  4. 透明性:对容器的使用者隐藏内存管理的复杂性

分配策略

分配器可以采用多种内存分配策略:

  1. 直接分配:直接使用系统的内存分配函数(如malloc/freenew/delete
  2. 内存池:预先分配大块内存,然后分割成小块按需分配
  3. 固定大小分配:为特定大小的对象优化的分配器
  4. 区域分配:从预定义的内存区域分配内存
  5. 栈分配:从栈上分配内存(适用于小型、生命周期有限的容器)

内存布局

分配器处理的内存布局通常包括:

  1. 原始存储:未构造对象的内存
  2. 构造存储:已经构造了对象的内存
  3. 内存对齐:确保内存按照类型要求对齐
  4. 内存边界:标记内存块的边界,以便正确释放

状态管理

分配器可以是有状态的或无状态的:

  • 无状态分配器:不保存状态,相同类型的分配器实例完全等价
  • 有状态分配器:保存状态(如内存池指针),不同实例可能有不同行为

C++17引入的is_always_equal特性用于区分这两种情况,告诉容器两个分配器实例是否可以互换使用。

自定义分配器的实现

基本实现框架

以下是一个简单的自定义分配器框架:

#include <iostream>
#include <memory>
#include <vector>
#include <cstdlib>
#include <new>// 一个简单的自定义分配器示例
template <class T>
class SimpleAllocator {
public:// 类型定义(C++17以及之后)using value_type = T;// 构造函数和析构函数SimpleAllocator() noexcept { std::cout << "SimpleAllocator created\n"; }~SimpleAllocator() { std::cout << "SimpleAllocator destroyed\n"; }// 复制构造函数template <class U>SimpleAllocator(const SimpleAllocator<U>&) noexcept { std::cout << "SimpleAllocator copied\n"; }// 分配内存T* allocate(std::size_t n) {std::cout << "Allocating " << n << " objects of size " << sizeof(T) << std::endl;if (n > std::size_t(-1) / 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 n) noexcept {std::cout << "Deallocating " << n << " objects of size " << sizeof(T) << std::endl;std::free(p);}// C++17之前的构造和析构函数(现在通过allocator_traits处理)template <class U, class... Args>void construct(U* p, Args&&... args) {::new((void*)p) U(std::forward<Args>(args)...);}template <class U>void destroy(U* p) {p->~U();}
};// 两个分配器相等的条件
template <class T, class U>
bool operator==(const SimpleAllocator<T>&, const SimpleAllocator<U>&) {return true;
}template <class T, class U>
bool operator!=(const SimpleAllocator<T>&, const SimpleAllocator<U>&) {return false;
}// 测试自定义分配器
void testSimpleAllocator() {// 使用自定义分配器的vectorstd::vector<int, SimpleAllocator<int>> v;// 添加元素std::cout << "Adding elements to vector...\n";for (int i = 0; i < 5; ++i) {v.push_back(i);}// 访问元素std::cout << "Vector elements: ";for (int i : v) {std::cout << i << " ";}std::cout << std::endl;// vector析构时会自动释放内存std::cout << "Vector going out of scope...\n";
}int main() {testSimpleAllocator();return 0;
}

内存池分配器

下面是一个更复杂的内存池分配器实现:

#include <iostream>
#include <memory>
#include <vector>
#include <list>
#include <set>
#include <cstddef>// 内存池实现
template <std::size_t BlockSize = 4096>
class MemoryPool {
private:// 内存块结构struct Block {char data[BlockSize];Block* next;};struct Chunk {Chunk* next;};Block* currentBlock;Chunk* freeChunks;std::size_t chunkSize;std::size_t chunksPerBlock;public:// 构造函数MemoryPool(std::size_t chunkSz) : currentBlock(nullptr),freeChunks(nullptr),chunkSize(chunkSz) {// 确保chunk大小至少能存放一个指针,并且是8的倍数(对齐)chunkSize = std::max(chunkSize, sizeof(Chunk));chunkSize = (chunkSize + 7) & ~7;// 计算每个块可以容纳的chunk数量chunksPerBlock = (BlockSize - sizeof(Block*)) / chunkSize;}// 析构函数~MemoryPool() {// 释放所有分配的块while (currentBlock) {Block* temp = currentBlock->next;operator delete(currentBlock);currentBlock = temp;}}// 分配内存void* allocate() {if (!freeChunks) { // 如果没有空闲的chunk,分配新块// 分配新的内存块Block* newBlock = static_cast<Block*>(operator new(sizeof(Block)));newBlock->next = currentBlock;currentBlock = newBlock;// 将新块分割成chunks并添加到freeChunks链表char* start = currentBlock->data;std::size_t i = 0;// 为每个chunk设置一个指向下一个chunk的指针for (; i < chunksPerBlock - 1; ++i) {Chunk* chunk = reinterpret_cast<Chunk*>(start + i * chunkSize);chunk->next = reinterpret_cast<Chunk*>(start + (i + 1) * chunkSize);}// 设置最后一个chunkChunk* lastChunk = reinterpret_cast<Chunk*>(start + i * chunkSize);lastChunk->next = nullptr;// 将第一个chunk设为空闲链表的头freeChunks = reinterpret_cast<Chunk*>(start);}// 从空闲链表中取出一个chunkChunk* chunk = freeChunks;freeChunks = chunk->next;return chunk;}// 释放内存void deallocate(void* p) {if (!p) return;// 将回收的chunk添加到空闲链表的头部Chunk* chunk = static_cast<Chunk*>(p);chunk->next = freeChunks;freeChunks = chunk;}
};// 基于内存池的分配器
template <class T, std::size_t BlockSize = 4096>
class PoolAllocator {
private:// 类型特定的内存池static MemoryPool<BlockSize>& getPool() {static MemoryPool<BlockSize> pool(sizeof(T));return pool;}public:// 类型定义using value_type = T;// 构造函数和析构函数PoolAllocator() noexcept = default;~PoolAllocator() = default;// 复制构造函数template <class U>PoolAllocator(const PoolAllocator<U, BlockSize>&) noexcept {}// 内存分配T* allocate(std::size_t n) {if (n != 1) {// 对于多个对象,使用标准分配器return static_cast<T*>(operator new(n * sizeof(T)));}// 对于单个对象,使用内存池return static_cast<T*>(getPool().allocate());}// 内存释放void deallocate(T* p, std::size_t n) noexcept {if (n != 1) {// 对于多个对象,使用标准释放operator delete(p);} else {// 对于单个对象,使用内存池getPool().deallocate(p);}}// 相等比较(所有同类型的池分配器都相等)template <class U>bool operator==(const PoolAllocator<U, BlockSize>&) const noexcept {return true;}template <class U>bool operator!=(const PoolAllocator<U, BlockSize>&) const noexcept {return false;}
};// 测试池分配器
void testPoolAllocator() {std::cout << "=== Testing Pool Allocator ===\n";// 使用池分配器的容器std::vector<int, PoolAllocator<int>> v;std::list<double, PoolAllocator<double>> l;std::set<char, std::less<char>, PoolAllocator<char>> s;// 添加元素std::cout << "Adding elements to vector...\n";for (int i = 0; i < 10000; ++i) {v.push_back(i);}std::cout << "Adding elements to list...\n";for (int i = 0; i < 10000; ++i) {l.push_back(i * 1.1);}std::cout << "Adding elements to set...\n";for (char c = 'a'; c <= 'z'; ++c) {s.insert(c);}// 访问元素std::cout << "Vector size: " << v.size() << std::endl;std::cout << "List size: " << l.size() << std::endl;std::cout << "Set size: " << s.size() << std::endl;std::cout << "First few vector elements: ";for (int i = 0; i < 5 && i < v.size(); ++i) {std::cout << v[i] << " ";}std::cout << std::endl;std::cout << "First few list elements: ";auto it = l.begin();for (int i = 0; i < 5 && it != l.end(); ++i, ++it) {std::cout << *it << " ";}std::cout << std::endl;std::cout << "Set elements: ";for (char c : s) {std::cout << c << " ";}std::cout << std::endl;// 容器析构时会自动释放内存std::cout << "Containers going out of scope...\n";
}int main() {testPoolAllocator();return 0;
}

这个内存池分配器更加高效,特别是对于频繁分配和释放单个对象的情况。

跟踪和调试分配器

用于调试内存问题的跟踪分配器:

#include <iostream>
#include <memory>
#include <vector>
#include <map>
#include <string>
#include <sstream>
#include <mutex>// 跟踪内存分配的分配器
template <class T>
class TracingAllocator {
private:// 全局分配跟踪(线程安全)struct AllocationTracker {std::map<void*, std::size_t> allocations; // 指针 -> 大小std::size_t totalAllocated = 0;std::size_t currentAllocated = 0;std::size_t allocationCount = 0;std::mutex mutex;void recordAllocation(void* p, std::size_t n) {std::lock_guard<std::mutex> lock(mutex);allocations[p] = n;totalAllocated += n * sizeof(T);currentAllocated += n * sizeof(T);allocationCount++;}void recordDeallocation(void* p) {std::lock_guard<std::mutex> lock(mutex);if (allocations.count(p) > 0) {std::size_t n = allocations[p];currentAllocated -= n * sizeof(T);allocations.erase(p);}}std::string getReport() const {std::ostringstream ss;ss << "Memory Allocation Report for " << typeid(T).name() << ":\n"<< "Total allocated: " << totalAllocated << " bytes\n"<< "Currently allocated: " << currentAllocated << " bytes\n"<< "Allocation count: " << allocationCount << "\n"<< "Outstanding allocations: " << allocations.size();return ss.str();}void checkLeaks() const {if (!allocations.empty()) {std::cerr << "WARNING: Memory leaks detected!\n";std::cerr << "Outstanding allocations: " << allocations.size() << "\n";for (const auto& [ptr, size] : allocations) {std::cerr << "  " << ptr << ": " << size << " elements (" << size * sizeof(T) << " bytes)\n";}}}};static AllocationTracker& getTracker() {static AllocationTracker tracker;return tracker;}// 分配标签(用于区分不同实例)std::string tag;public:// 类型定义using value_type = T;// 构造函数TracingAllocator(std::string t = "Default") : tag(std::move(t)) {}// 复制构造函数template <class U>TracingAllocator(const TracingAllocator<U>& other) : tag(other.getTag()) {}// 分配内存T* allocate(std::size_t n) {T* p = static_cast<T*>(::operator new(n * sizeof(T)));std::cout << "[" << tag << "] Allocating " << n << " elements of type " << typeid(T).name() << " (" << n * sizeof(T) << " bytes) at " << p << std::endl;getTracker().recordAllocation(p, n);return p;}// 释放内存void deallocate(T* p, std::size_t n) noexcept {std::cout << "[" << tag << "] Deallocating " << n << " elements at " << p << std::endl;getTracker().recordDeallocation(p);::operator delete(p);}// 获取标签const std::string& getTag() const { return tag; }// 生成报告static std::string generateReport() {return getTracker().getReport();}// 检查内存泄漏static void checkForLeaks() {getTracker().checkLeaks();}// 相等比较template <class U>bool operator==(const TracingAllocator<U>& other) const noexcept {return tag == other.getTag();}template <class U>bool operator!=(const TracingAllocator<U>& other) const noexcept {return !(*this == other);}
};// 测试跟踪分配器
void testTracingAllocator() {std::cout << "=== Testing Tracing Allocator ===\n";// 创建使用跟踪分配器的容器std::vector<int, TracingAllocator<int>> v(TracingAllocator<int>("Vector"));// 添加和删除元素std::cout << "Adding elements...\n";for (int i = 0; i < 10; ++i) {v.push_back(i);}std::cout << "Removing some elements...\n";v.pop_back();v.pop_back();// 检查内存状态std::cout << "\nMemory allocation report:\n";std::cout << TracingAllocator<int>::generateReport() << std::endl;// 清空容器v.clear();v.shrink_to_fit();std::cout << "\nAfter clearing vector:\n";std::cout << TracingAllocator<int>::generateReport() << std::endl;// 检查内存泄漏std::cout << "\nChecking for memory leaks...\n";TracingAllocator<int>::checkForLeaks();std::cout << "\nVector going out of scope...\n";
}int main() {testTracingAllocator();std::cout << "\nFinal memory check:\n";TracingAllocator<int>::checkForLeaks();return 0;
}

这种跟踪分配器对于调试内存泄漏和分析内存使用模式非常有用。

分配器的高级应用

多级分配策略

在实际应用中,可能需要结合多种分配策略:

#include <iostream>
#include <memory>
#include <vector>// 多级分配器:小对象使用内存池,大对象直接分配
template <class T, std::size_t Threshold = 1024, std::size_t BlockSize = 4096>
class TieredAllocator {
private:// 内存池实现(简化版)class Pool {public:void* allocate(std::size_t bytes) {// 简化实现std::cout << "Pool allocating " << bytes << " bytes\n";return ::operator new(bytes);}void deallocate(void* p, std::size_t bytes) {std::cout << "Pool deallocating " << bytes << " bytes\n";::operator delete(p);}};static Pool& getPool() {static Pool pool;return pool;}public:using value_type = T;TieredAllocator() noexcept = default;template <class U>TieredAllocator(const TieredAllocator<U, Threshold, BlockSize>&) noexcept {}// 分配内存T* allocate(std::size_t n) {std::size_t totalBytes = n * sizeof(T);if (totalBytes <= Threshold) {// 小型分配使用内存池return static_cast<T*>(getPool().allocate(totalBytes));} else {// 大型分配直接使用系统分配器std::cout << "Direct allocating " << totalBytes << " bytes\n";return static_cast<T*>(::operator new(totalBytes));}}// 释放内存void deallocate(T* p, std::size_t n) noexcept {std::size_t totalBytes = n * sizeof(T);if (totalBytes <= Threshold) {getPool().deallocate(p, totalBytes);} else {std::cout << "Direct deallocating " << totalBytes << " bytes\n";::operator delete(p);}}
};// 测试多级分配器
void testTieredAllocator() {std::cout << "=== Testing Tiered Allocator ===\n";// 使用多级分配器的向量std::vector<int, TieredAllocator<int>> v;// 小型分配std::cout << "Small allocations:\n";for (int i = 0; i < 10; ++i) {v.push_back(i);}// 大型分配std::cout << "\nLarge allocation:\n";v.reserve(10000);  // 应该触发大型分配std::cout << "\nVector going out of scope...\n";
}

对齐内存分配器

专门处理对齐内存需求的分配器:

#include <iostream>
#include <memory>
#include <vector>
#include <cstddef>// 对齐内存分配器
template <class T, std::size_t Alignment = 64> // 默认缓存行大小对齐
class AlignedAllocator {
public:// 类型定义using value_type = T;using pointer = T*;using const_pointer = const T*;using size_type = std::size_t;static constexpr size_type alignment = Alignment;// 默认构造函数AlignedAllocator() noexcept = default;// 复制构造函数template <class U>AlignedAllocator(const AlignedAllocator<U, Alignment>&) noexcept {}// 分配对齐的内存T* allocate(std::size_t n) {if (n == 0) return nullptr;std::size_t totalSize = n * sizeof(T);void* ptr = nullptr;#ifdef _WIN32// Windowsptr = _aligned_malloc(totalSize, Alignment);if (!ptr) throw std::bad_alloc();
#else// POSIXint result = posix_memalign(&ptr, Alignment, totalSize);if (result != 0) throw std::bad_alloc();
#endifstd::cout << "Allocated " << totalSize << " bytes aligned to " << Alignment << " at " << ptr << std::endl;return static_cast<T*>(ptr);}// 释放对齐的内存void deallocate(T* p, std::size_t n) noexcept {if (!p) return;std::cout << "Deallocating aligned memory at " << p << std::endl;#ifdef _WIN32// Windows_aligned_free(p);
#else// POSIXfree(p);
#endif}
};// 测试对齐分配器
void testAlignedAllocator() {std::cout << "=== Testing Aligned Allocator ===\n";// 使用对齐分配器的向量std::vector<double, AlignedAllocator<double>> v;// 添加元素for (int i = 0; i < 10; ++i) {v.push_back(i * 1.1);}// 检查对齐std::cout << "Vector data address: " << &v[0] << std::endl;std::cout << "Address alignment check: " << (reinterpret_cast<std::uintptr_t>(&v[0]) % AlignedAllocator<double>::alignment == 0 ? "Properly aligned" : "Not aligned") << std::endl;// 访问元素std::cout << "Vector elements: ";for (auto val : v) {std::cout << val << " ";}std::cout << std::endl;
}

分配器的适配器模式

为现有分配器添加额外功能的适配器:

#include <iostream>
#include <memory>
#include <vector>
#include <chrono>// 计时分配器适配器
template <class Allocator>
class TimingAllocator {
private:Allocator allocator;// 获取当前时间戳auto now() const {return std::chrono::high_resolution_clock::now();}// 计算时间差(微秒)template <class TimePoint>long long microseconds(const TimePoint& start, const TimePoint& end) const {return std::chrono::duration_cast<std::chrono::microseconds>(end - start).count();}public:// 类型定义using value_type = typename Allocator::value_type;// 构造函数TimingAllocator() = default;template <class U>TimingAllocator(const TimingAllocator<U>& other) : allocator(other.getAllocator()) {}// 获取底层分配器const Allocator& getAllocator() const { return allocator; }// 分配内存并计时value_type* allocate(std::size_t n) {auto start = now();value_type* p = allocator.allocate(n);auto end = now();std::cout << "Allocation of " << n << " elements took "<< microseconds(start, end) << " microseconds" << std::endl;return p;}// 释放内存并计时void deallocate(value_type* p, std::size_t n) {auto start = now();allocator.deallocate(p, n);auto end = now();std::cout << "Deallocation of " << n << " elements took "<< microseconds(start, end) << " microseconds" << std::endl;}// 使用allocator_traits处理其它操作
};// 测试计时分配器适配器
void testTimingAllocator() {std::cout << "=== Testing Timing Allocator ===\n";// 使用计时分配器的向量std::vector<int, TimingAllocator<std::allocator<int>>> v;// 预留空间std::cout << "Reserving space...\n";v.reserve(1000);// 添加元素std::cout << "Adding elements...\n";for (int i = 0; i < 1000; ++i) {v.push_back(i);}// 调整大小std::cout << "Resizing...\n";v.resize(500);// 释放未使用内存std::cout << "Shrinking to fit...\n";v.shrink_to_fit();
}

分配器的性能考量

性能比较

不同分配器在不同场景下的性能可能有显著差异:

#include <iostream>
#include <vector>
#include <chrono>
#include <memory>
#include <random>// 测试不同分配器的性能
template <template <typename> class Allocator>
void benchmarkAllocator(const std::string& name, int iterations, int elementsPerIteration) {auto start = std::chrono::high_resolution_clock::now();for (int i = 0; i < iterations; ++i) {// 创建和销毁带有分配器的向量{std::vector<int, Allocator<int>> v;// 添加元素for (int j = 0; j < elementsPerIteration; ++j) {v.push_back(j);}// 执行一些操作int sum = 0;for (int val : v) {sum += val;}} // 向量在这里被销毁}auto end = std::chrono::high_resolution_clock::now();auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count();std::cout << name << ": " << duration << " ms" << std::endl;
}// 运行基准测试
void runAllocationBenchmarks() {std::cout << "=== Allocator Performance Benchmarks ===\n";const int iterations = 100;const int elementsPerIteration = 10000;// 测试标准分配器benchmarkAllocator<std::allocator>("Standard Allocator", iterations, elementsPerIteration);// 测试池分配器benchmarkAllocator<PoolAllocator>("Pool Allocator", iterations, elementsPerIteration);
}

内存使用分析

分析内存使用情况:

#include <iostream>
#include <iomanip>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <memory>// 内存使用分析
void analyzeMemoryUsage() {std::cout << "=== Memory Usage Analysis ===\n";// 分析不同容器的内存开销std::cout << std::left << std::setw(15) << "Container"<< std::setw(15) << "Element Size"<< std::setw(20) << "Container Size"<< std::setw(20) << "Memory Usage"<< std::setw(15) << "Overhead"<< std::endl;std::cout << std::string(85, '-') << std::endl;// 分析vector的内存使用{const size_t count = 1000;std::vector<int> v(count);size_t elementSize = sizeof(int);size_t containerSize = sizeof(v);size_t dataSize = count * elementSize;size_t capacity = v.capacity() * elementSize;size_t overhead = containerSize + capacity - dataSize;std::cout << std::setw(15) << "vector<int>"<< std::setw(15) << elementSize<< std::setw(20) << containerSize<< std::setw(20) << (containerSize + capacity)<< std::setw(15) << overhead<< std::endl;}// 分析list的内存使用{const size_t count = 1000;std::list<int> l(count);size_t elementSize = sizeof(int);size_t nodeSize = elementSize + 2 * sizeof(void*);  // 近似节点大小size_t containerSize = sizeof(l);size_t dataSize = count * elementSize;size_t totalSize = containerSize + count * nodeSize;size_t overhead = totalSize - dataSize;std::cout << std::setw(15) << "list<int>"<< std::setw(15) << elementSize<< std::setw(20) << containerSize<< std::setw(20) << totalSize<< std::setw(15) << overhead<< std::endl;}// 分析其他容器...
}

大小和对齐问题

处理特殊大小和对齐要求:

#include <iostream>
#include <memory>
#include <type_traits>// 打印类型的大小和对齐要求
template <typename T>
void printTypeInfo() {std::cout << "Type: " << typeid(T).name() << std::endl;std::cout << "Size: " << sizeof(T) << " bytes" << std::endl;std::cout << "Alignment: " << alignof(T) << " bytes" << std::endl;
}// 分析不同类型的大小和对齐
void analyzeSizeAlignment() {std::cout << "=== Size and Alignment Analysis ===\n";printTypeInfo<char>();std::cout << std::endl;printTypeInfo<int>();std::cout << std::endl;printTypeInfo<double>();std::cout << std::endl;struct Aligned {char c;         // 1 byteint i;          // 4 bytesalignas(16) double d;  // 8 bytes, aligned to 16};printTypeInfo<Aligned>();// 检查对齐内存分配器AlignedAllocator<double, 64> alignedAlloc;double* p = alignedAlloc.allocate(1);std::cout << "Allocated address: " << p << std::endl;std::cout << "Address aligned to 64 bytes: " << (reinterpret_cast<std::uintptr_t>(p) % 64 == 0 ? "Yes" : "No") << std::endl;alignedAlloc.deallocate(p, 1);
}

分配器的最佳实践

何时使用自定义分配器

以下场景适合使用自定义分配器:

  1. 性能关键应用:需要最小化内存分配开销
  2. 特定内存分配模式:容器有可预测的内存分配模式
  3. 内存受限环境:嵌入式系统或资源受限设备
  4. 特殊内存需求:SIMD算法需要对齐内存,或使用特殊内存区域
  5. 调试内存问题:需要跟踪内存分配和泄漏

分配器设计准则

设计高效分配器的准则:

  1. 简单性:除非有明显的性能增益,否则保持简单
  2. 避免虚函数:使用模板而非虚函数,减少运行时开销
  3. 减少同步:在可能的情况下避免互斥锁或原子操作
  4. 缓存友好:考虑CPU缓存效应,尤其是对小对象
  5. 避免碎片:使用固定大小或合并策略减少碎片
  6. 批量操作:尽可能批量分配和释放内存
  7. 异常安全:确保在异常情况下不会泄漏内存

现代C++中的分配器改进

C++17和C++20对分配器模型做了一些改进:

  1. PMR(多态内存资源):C++17引入了std::pmr命名空间,提供多态分配器
  2. 分配器感知智能指针:如std::pmr::polymorphic_allocator支持的智能指针
  3. 内存资源抽象std::pmr::memory_resource提供的内存管理抽象
  4. 标准内存池std::pmr::unsynchronized_pool_resourcestd::pmr::synchronized_pool_resource
#include <iostream>
#include <vector>
#include <memory_resource>
#include <string>// 使用PMR示例
void pmrExample() {std::cout << "=== PMR Example ===\n";// 创建一个缓冲区来存储内存char buffer[4096];// 创建一个使用缓冲区的内存资源std::pmr::monotonic_buffer_resource pool(buffer, sizeof(buffer));// 使用内存池创建一个分配器std::pmr::vector<int> v(&pool);// 使用这个容器std::cout << "Adding elements to pmr::vector...\n";for (int i = 0; i < 100; ++i) {v.push_back(i);}// 检查元素std::cout << "First few elements: ";for (int i = 0; i < 5; ++i) {std::cout << v[i] << " ";}std::cout << std::endl;// 使用不同类型的内存资源{// 使用池资源std::pmr::synchronized_pool_resource poolResource;// 使用池资源创建字符串std::pmr::string str1("Hello", &poolResource);std::pmr::string str2("World", &poolResource);// 连接字符串std::cout << "Concatenated strings: " << (str1 + " " + str2) << std::endl;} // 内存资源在这里被释放std::cout << "PMR resources going out of scope...\n";
}

总结

STL分配器是C++标准库中一个强大但通常被忽视的组件。它们为容器提供内存管理功能,并允许通过自定义实现来满足特定需求。本文详细介绍了分配器的设计原理、接口规范、标准实现以及如何创建和使用自定义分配器。

主要关键点回顾:

  1. 分配器的基本概念:分配器负责内存分配和释放,使容器的算法逻辑与内存管理分离
  2. 分配器接口规范:C++17之前需要很多类型定义和方法,C++17之后通过allocator_traits简化了要求
  3. 标准分配器std::allocator是STL容器使用的默认分配器,直接使用系统内存管理
  4. 自定义分配器:可以根据需要实现专用分配器,如跟踪分配器、内存池分配器和对齐分配器
  5. 高级应用:多级策略、内存对齐和分配器适配器等高级应用场景
  6. 性能考量:不同分配器在不同场景下的性能差异和内存使用分析
  7. 最佳实践:何时使用自定义分配器及其设计准则
  8. 现代改进:C++17和C++20对分配器模型的改进,特别是多态内存资源(PMR)

了解并掌握分配器的原理和实现,可以帮助你开发出更高效、更灵活的C++应用程序,特别是在性能关键或资源受限的环境中。

参考资源

  • C++参考手册 - 分配器
  • 《Effective STL》by Scott Meyers
  • 《The C++ Standard Library》by Nicolai M. Josuttis
  • 《Modern C++ Design》by Andrei Alexandrescu
  • CppCon 2017: Pablo Halpern “Allocators: The Good Parts”
  • CppCon 2015: Alisdair Meredith “Allocator Is to Allocation what vector Is to vexation”
  • ISO C++ Committee Paper P0339R4: polymorphic_allocator<> as a vocabulary type

这是我C++学习之旅系列的第三十篇技术文章。查看完整系列目录了解更多内容。

如有任何问题或建议,欢迎在评论区留言交流!

相关文章:

C++学习:六个月从基础到就业——STL:分配器与设计原理

C学习&#xff1a;六个月从基础到就业——STL&#xff1a;分配器与设计原理 本文是我C学习之旅系列的第三十篇技术文章&#xff0c;也是第二阶段"C进阶特性"的第九篇&#xff0c;主要介绍C STL中的分配器设计原理与实现。查看完整系列目录了解更多内容。 引言 在之前…...

QQMusic项目功能总结

QQMusic项目功能总结 一、核心功能分类 &#xff08;一&#xff09;界面交互功能 功能模块实现方式使用类&#xff08;自定义/Qt库&#xff09;核心类说明窗口布局Head区&#xff08;图标、搜索框、控制按钮&#xff09; Body区&#xff08;左侧功能栏右侧页面区&#xff09…...

人形机器人:MCP与人形机器人的联系

MCP&#xff08;Model Context Protocol&#xff09;与人形机器人的结合&#xff0c;正在重构智能体与物理世界的交互范式。这种联系不仅体现在技术架构的深度融合&#xff0c;更体现在对机器人认知能力、协作模式和应用场景的全方位赋能。以下从技术整合、场景落地和未来趋势三…...

matplotlib画图工具使用(1) 画折线统计图python代码

Python 画折线统计图&#xff08;line chart&#xff09;最常用的是 matplotlib。 最基本的折线图代码如下&#xff1a; import matplotlib.pyplot as plt# 假设这是你的数据 x [1, 2, 3, 4, 5] y [2, 3, 5, 7, 11]# 创建折线图 plt.plot(x, y, markero) # markero 是在点…...

神经网络与深度学习第四章-前馈神经网络

前馈神经网络 在本章中&#xff0c;我们主要关注采用误差反向传播来进行学习的神经网络。 4.1 神经元 神经元是构成神经网络的基本单元。主要是模拟生物神经元的结构和特性&#xff0c;接收一组输入信号并产生输出。 现代神经元中的激活函数通常要求是连续可导的函数。 净输…...

TC3xx学习笔记-UCB BMHD使用详解(一)

文章目录 前言UCB BMHDPINDISHWCFGLSENA0-3LBISTENACHSWENABMHDIDSTADCRCBMHDCRCBMHD_NPW0-7 总结 前言 AURIX Tc系列Mcu启动过程&#xff0c;必须要了解BMHD&#xff0c;本文详细介绍BMHD的定义及使用过程 UCB BMHD UCB表示User Configuration Block,UCB是Dflash,存储的地址…...

C语言 函数递归

目录 1.什么是递归 2.递归的限制条件 3.递归的举例 1.递归与迭代 1.递归是什么 递归是学习C语言函数绕不开的一个话题&#xff0c;那什么是递归呢? 递归其实是一种解决问题的方法&#xff0c;在C语言中&#xff0c;递归就是函数自己调用自己。 写一个史上最简单的C语言…...

4月25日日记(补)

最近实在是忙的不行了&#xff0c;做不到一天一更&#xff0c;但是实际上只需要每天拿出十分钟就可以写一篇不错的随笔或者说日记&#xff0c;我还是有点倦怠了。 昨天是4月25&#xff0c;我的生日&#xff0c;但是依旧是很忙碌的一天。零点准时拆了朋友们送的礼物&#xff0c…...

【股票系统】使用docker本地构建ai-hedge-fund项目,模拟大师炒股进行分析。人工智能的对冲基金的开源项目

股票系统: https://github.com/virattt/ai-hedge-fund 镜像地址: https://gitcode.com/gh_mirrors/ai/ai-hedge-fund 项目地址: https://gitee.com/pythonstock/docker-run-ai-hedge-fund 这是一个基于人工智能的对冲基金的原理验证项目。本项目旨在探讨利用人工智能进行…...

Ollama平替!LM Studio本地大模型调用实战

本文已收录在Github&#xff0c;关注我&#xff0c;紧跟本系列专栏文章&#xff0c;咱们下篇再续&#xff01; &#x1f680; 魔都架构师 | 全网30W技术追随者&#x1f527; 大厂分布式系统/数据中台实战专家&#x1f3c6; 主导交易系统百万级流量调优 & 车联网平台架构&a…...

2024江西ICPC部分题解

题目列表 A - Maliang Learning PaintingC - LiarG - Multiples of 5H - ConvolutionJ - Magic MahjongK - Magic Tree A - Maliang Learning Painting 题目来源&#xff1a;A - Maliang Learning Painting 思路分析 这是个签到题&#xff0c;直接输出abc即可 #include<b…...

RabbitMQ安装流程(Windows环境)

1.下载依赖&#xff0c;Downloads - Erlang/OTP 2.下载RabbitMQ安装包&#xff0c;Installing on Windows | RabbitMQ 3.下载的文件如下 4.安装ErLang依赖 5.安装RabbitMQ 6.RabbitMQ插件管理 6.1 进入Command Prompt命令行界面 6.2 输入rabbitmq-plugins.bat list 查看所有插…...

QT对话框及其属性

Qt中使用QDialog类表示对话框 对话框是一个顶层窗口&#xff0c;出现在程序最上层&#xff0c;用于实现短期任务或者简洁的用户交互 QDialog也是继承自QWidget,可以使用QWidget接口 Qt常用的内置对话框有: 对话框 说明 QFiledialog 文件对话框 QColorDialog 颜色对话框 …...

python怎么查看函数原型及变量是什么类型

python代码中看到一个变量或者对象名,怎么查看这个变量到底是个什么东西,是属性,还是函数,还是模块,还是个包,怎么去查看,要有一个查找流程: 1.可以先用print(变量名)和print(type(变量名)),确认变量是什么类型的参数 2.如果是模块或者类,可以通过dir()函数去查看模…...

住宅代理IP助力大规模数据采集实战

在数字化时代&#xff0c;数据就是燃料&#xff0c;而大规模数据采集则是从信息海洋中提炼价值的关键手段。面对目标网站的严格风控和地域限制&#xff0c;普通代理车轮战往往难以为继——流量一旦被识破&#xff0c;便可能付之东流。这时&#xff0c;住宅代理IP凭借来自真实家…...

【信息融合】卡尔曼滤波EKF毫米波雷达和红外数据信息融合

一、扩展卡尔曼滤波&#xff08;EKF&#xff09;的核心原理 EKF是针对非线性系统的改进卡尔曼滤波算法&#xff0c;其核心思想是通过一阶泰勒展开对非线性方程进行局部线性化&#xff0c;并利用雅可比矩阵&#xff08;Jacobian Matrix&#xff09;替换线性系统中的状态转移矩阵…...

一篇入门之-评分卡变量分箱(卡方分箱、决策树分箱、KS分箱等)实操例子

一、评分卡分箱-有哪些分箱方法 评分卡的分箱&#xff0c;是指将变量进行离散化。评分卡的分箱方法一般有&#xff1a;等频分箱、等距分箱、卡方分箱、决策树分箱、KS分箱等等。它们都属于自动分箱方法&#xff0c;其中&#xff0c;卡方分箱是实际中最常用的分箱方法。 1.1.等…...

【白雪讲堂】构建与优化企业知识图谱的实战指南

在GEO&#xff08;生成式引擎优化&#xff09;时代&#xff0c;知识图谱不仅是企业数据资产的“智慧大脑”&#xff0c;更是连接内容与AI理解之间的核心桥梁。一个高质量的知识图谱&#xff0c;能够显著提高AI平台对企业内容的识别度、相关性与推荐权重&#xff0c;从而在AI搜索…...

作为高速通道光纤传输模式怎么理解以及到底有哪些?

光纤的传输模式主要取决于光纤的结构(如纤芯直径和折射率分布),不同模式对应光波在光纤中传播的不同路径和电磁场分布。以下是光纤传输模式的主要分类及特点: 1. 单模光纤(Single-Mode Fiber, SMF) 核心特点: 纤芯直径极小(通常为 8-10微米),仅允许光以单一模式(…...

setup语法糖

为什么要有setup语法糖&#xff1a; 在选择式API中&#xff0c;一个模块涉及到的数据、方法、声明周期&#xff0c;会比较分撒&#xff0c;位置不集中&#xff0c;不利于解读代码&#xff0c;但是组合式API中的setup函数可以将他们组织在一起&#xff0c;提高了代码的可维护性…...

linux socket编程之tcp(实现客户端和服务端消息的发送和接收)

目录 一.创建socket套接字(服务器端) 二.bind将port与端口号进行绑定(服务器端) 2.1填充sockaddr_in结构 2.2bind绑定端口 三.建立连接 四.获取连接 五..进行通信(服务器端) 5.1接收客户端发送的消息 5.2给客户端发送消息 5.3引入多线程 六.客户端通信 6.1创建socke…...

Spring和Spring Boot集成MyBatis的完整对比示例,包含从项目创建到测试的全流程代码

以下是Spring和Spring Boot集成MyBatis的完整对比示例&#xff0c;包含从项目创建到测试的全流程代码&#xff1a; 一、Spring集成MyBatis示例 1. 项目结构 spring-mybatis-demo/ ├── src/ │ ├── main/ │ │ ├── java/ │ │ │ └── com.example/…...

Beta-VAE背景原理及解耦机制分析

Beta-VAE背景原理及解耦机制分析 论文链接&#xff1a;https://openreview.net/forum?idSy2fzU9gl&noteIdSy2fzU9gl 一、Beta-VAE的核心思想 Beta-VAE 是一种改进的变分自编码器&#xff08;VAE&#xff09;&#xff0c;旨在通过调整潜在变量的独立性来增强模型的解耦能…...

用c语言实现——一个动态顺序存储的串结构

一、思路概要 ①动态顺序存储的串结构&#xff1a; 动态应该使用动态内存分配&#xff0c;也就是用指针来存储字符数组&#xff0c;同时记录长度和当前容量。 这样结构体应该包含三个成员&#xff1a;一个char*指针&#xff0c;一个int表示当前长度&#xff0c;另一个int表示…...

小程序Npm package entry file not found?

修改依赖包的入口文件 看是不是cjs&#xff0c;小程序不支持cjs...

vue3学习之防抖和节流

​ 在前端开发中&#xff0c;我们经常会遇到这样的情况&#xff1a;某些事件&#xff08;如滚动、输入、点击等&#xff09;会频繁触发&#xff0c;如果不加以控制&#xff0c;可能会导致性能问题。Vue3 中的防抖&#xff08;Debounce&#xff09;和节流&#xff08;Throttle&a…...

当高级辅助驾驶遇上“安全驾校”:NVIDIA如何用技术给无人驾驶赋能?

高级辅助驾驶技术的商业化落地&#xff0c;核心在于能否通过严苛的安全验证。国内的汽车企业其实也在做高级辅助驾驶&#xff0c;但是吧&#xff0c;基本都在L2级别。换句话说就是在应急时刻内&#xff0c;还是需要人来辅助驾驶&#xff0c;AI驾驶只是决策层&#xff0c;并不能…...

Linux | Mfgtools 修改单独只烧写 Uboot,内核,文件系统

01 1. 打开 mfgtools_for_6ULL 文件夹,找到 cfg.ini 文件,如果您的板子是 EMMC 的修改如下图: 如果您的板子是 NAND 的,修改如下图: 02 2. 打开“Pro...

【Agent python实战】ReAct 与 Plan-and-Execute 的融合之道_基于DeepSeek api

写在前面 大型语言模型(LLM)驱动的 Agent 正在从简单的任务执行者向更复杂的问题解决者演进。在 Agent 的设计模式中,ReAct (Reason + Act) 以其步步为营、动态适应的特性见长,擅长处理需要与环境实时交互、快速响应的任务。而 Plan-and-Execute 则强调前瞻性规划,先制定…...

Native层Trace监控性能

一、基础实现方法 1.1 头文件引用 #include <utils/Trace.h> // 基础版本 #include <cutils/trace.h> // 兼容旧版本1.2 核心宏定义 // 区间追踪&#xff08;推荐&#xff09; ATRACE_BEGIN("TraceTag"); ...被监控代码... ATRACE_END();// 函数级自…...

【C++】15. 模板进阶

1. 非类型模板参数 模板参数分类类型形参与非类型形参。 类型形参即&#xff1a;出现在模板参数列表中&#xff0c;跟在class或者typename之类的参数类型名称。 非类型形参&#xff1a;就是用一个常量作为类(函数)模板的一个参数&#xff0c;在类(函数)模板中可将该参数当…...

C#进阶学习(十四)反射的概念以及关键类Type

目录 本文末尾有相关类中的总结&#xff0c;如有需要直接跳到最后即可 前置知识&#xff1a; 1、程序集&#xff08;Assembly&#xff09; 2、元数据&#xff08;Metadata&#xff09; 3、中间语言&#xff08;IL, Intermediate Language&#xff09; 中间语言&#xff08;…...

B. And It‘s Non-Zero

题目链接&#xff1a;https://codeforces.com/problemset/problem/1615/B 位运算之前没怎么写过&#xff0c;所以不会写。留一份题解&#xff0c;作为复习使用。 题解&#xff1a;按位与的结果不为0&#xff0c;则至少有一列全为1.要求删除的数最少&#xff0c;即要求该列原本…...

深入解析NuttX:为何它是嵌入式RTOS领域的标杆?​​

文章目录 引言一、NuttX简介&#xff1a;轻量级与高兼容性的结合二、架构特点&#xff1a;为何NuttX更灵活&#xff1f;三、横向对比&#xff1a;NuttX vs 主流嵌入式RTOS四、NuttX的核心优势五、何时选择NuttX&#xff1f;结语 引言 在资源受限的嵌入式系统中&#xff0c;实时…...

html初识

html 盖楼第一步&#xff1a;HTML1. HTML是啥玩意儿&#xff1f;2. 动手&#xff01;搭个你的"网络小窝" (第一个HTML页面)3. 添砖加瓦&#xff1a;常用HTML"建材"详解3.1 标题家族3.2 段落哥俩好3.3 传送门&#xff1a;链接3.4 挂画&#xff1a;图片 盖楼…...

leetcode66.加一

从后向前遍历&#xff0c;直到碰到非9的数&#xff08;也就是数组中中最后一个非9的数&#xff09; &#xff0c;该值&#xff0b;1&#xff0c;然后其后的数字全部0 class Solution {public int[] plusOne(int[] digits) {for (int i digits.length-1; i >0; i--) {if (d…...

【Vue】Vue3项目创建

执行npm run dev&#xff0c;如果报错检查nodejs版本...

缓存替换算法之 FIFO(先进先出)

FIFO&#xff08;First In, First Out&#xff0c;先进先出&#xff09;是一种常见的缓存替换算法&#xff0c;其基本思想是最早进入缓存的数据项将最先被移除。以下是FIFO的详细讲解&#xff1a; 一、FIFO的数据结构 队列&#xff08;Queue&#xff09; 队列是一种典型的线性…...

Linux下的I/O复用技术之epoll

I/O多路复用 指在单个线程或进程中&#xff0c;同时处理多个I/O操作的技术。 旨在提高程序处理多个并发I/O操作的能力&#xff0c;避免程序因等待某个I/O操作而被阻塞。在传统的I/O模型中当程序进行I/O操作时(如读取文件、接受网路数据等)&#xff0c;如果数据还未准备好&…...

数据分析管理软件 Minitab 22.2.2 中文版安装包 免费下载

Minitab22.2.2 安装包下载链接: https://pan.baidu.com/s/1cWuDbvcWhYrub01C6QR81Q?pwd6666 提取码: 6666 Minitab软件是现代质量管理统计软件&#xff0c;全球六西格玛实施的共同语言。Minitab 已经在全球120多个国家&#xff0c;5000多所高校被广泛使用。...

chrony服务器(1)

简介 NTP NTP&#xff08;Network Time Protocol&#xff0c;网络时间协议&#xff09;是一种用于同步计算机系统时间的协议是TCP/IP协议族中的一个应用层协议&#xff0c;主要用于在分布式时间服务器和客户端之间进行时钟同步&#xff0c;提供高精准度的时间校正通过分层的时…...

2025.04.26-淘天春招笔试题-第三题

📌 点击直达笔试专栏 👉《大厂笔试突围》 💻 春秋招笔试突围在线OJ 👉 笔试突围OJ 03. 二进制信号转换器 问题描述 卢小姐是一位通信工程师,她设计了一种特殊的二进制信号处理装置。该装置可以对由 0 0 0...

腾讯二面:TCC分布式事务 | 图解TCC|用Go语言实现一个TCC

写在前面 前段时间&#xff0c;有同学反馈的一个面试问题&#xff0c;觉得分布式事务有点偏了&#xff0c;但其实也不算偏了&#xff0c;在java领域就有很火很成熟的seata分布式事务框架(阿里和蚂蚁的同学主导&#xff0c;目前在apache孵化)。 之前我们讲过了两阶段提交、三阶…...

如何在 Conda 环境中降级 Python 版本:详细指南

如何在 Conda 环境中降级 Python 版本&#xff1a;详细指南 Python 版本的管理在开发过程中至关重要&#xff0c;特别是在处理不同项目需求时。对于使用 Conda 环境的 Python 程序员来说&#xff0c;版本管理不仅仅是安装不同的 Python 版本&#xff0c;还涉及到依赖关系的兼容…...

MCP 协议解读:STDIO 高效通信与 JSON-RPC 实战

本文深度解析 MCP 协议的传输机制与消息格式&#xff0c;涵盖 stdio、SSE 及自定义传输方式&#xff0c;剖析 JSON-RPC 2.0 的请求、响应与通知设计。 结合 RooCode 开源实现与天气查询案例&#xff0c;揭秘如何通过 MCP 实现跨进程通信与高效服务集成&#xff0c;为开发者提供…...

AI心理健康服务平台项目面试实战

AI心理健康服务平台项目面试实战 第一轮提问&#xff1a; 面试官&#xff1a; 请简要介绍一下AI心理健康服务平台的核心技术架构。在AI领域&#xff0c;心理健康服务的机遇主要体现在哪些方面&#xff1f;如何利用NLP技术提升用户与AI的心理健康对话体验&#xff1f; 马架构…...

路由器重分发(OSPF+RIP),RIP充当翻译官,OSPF充当翻译官

路由器重分发&#xff08;OSPFRIP&#xff09; 版本 1 RIP充当翻译官 OSPF路由器只会OSPF语言&#xff1b;RIP路由器充当翻译官就要会OSPF语言和RIP语言&#xff1b;则在RIP中还需要将OSPF翻译成RIPOSPF 把RIP路由器当成翻译官&#xff0c;OSPF路由器就只需要宣告自己的ip&am…...

29-算法打卡-字符串-KMP算法理论2-第二十九天

1、KMP算法前缀表计算逻辑 可以查看上一章节的前缀表概念以及逻辑&#xff0c;KMP算法基础理论[基础概念、前缀、后缀、最长公共前后缀、前缀表] 2、KMP算法前缀表使用 当模式串和文本串匹配失败的时候&#xff0c;前缀表会告诉我们下一次的匹配中&#xff0c;模式串应该跳到…...

解锁生成式AI潜力的金钥匙

一、引言&#xff1a;生成式AI的浪潮与“提示词”的崛起 在短短几年内&#xff0c;生成式人工智能&#xff08;Generative AI&#xff09;以前所未有的速度席卷全球&#xff0c;从文字创作到图像生成&#xff0c;从代码辅助到科学研究&#xff0c;以ChatGPT、Midjourney、DALL…...

统计定界子数组的数组

前言&#xff1a;看到这个题目的时候&#xff0c;只想着怎么暴力枚举右端点&#xff0c;结合线段树还是会超时&#xff0c;没找到很好的处理方法 超时代码 class Tree1:def __init__(self,n):self.t [0]*(4*n)def update(self,o,l,r,index,va):if lr:self.t[o] vareturnmid …...