Files
XCEngine/docs/api/containers/array/setallocator.md

39 lines
1.0 KiB
Markdown
Raw Normal View History

# Array::SetAllocator()
```cpp
void SetAllocator(Memory::IAllocator* allocator);
```
设置数组使用的内存分配器。
**用途:** 允许自定义内存分配策略,如使用对象池、固定大小分配器或调试分配器。
**参数:**
- `allocator` - 指向 `Memory::IAllocator` 接口的指针。如果为 `nullptr`,使用默认 `::operator new/delete`
**注意:**
- 如果数组已有元素,设置新的分配器后,**不会**迁移现有元素
- 仅影响后续的内存分配操作
- 通常在构造后立即调用,或在数组为空时调用
**线程安全:** ❌ 操作期间不可并发访问
**示例:**
```cpp
// 使用线性分配器(适合帧分配)
auto* linear = new Memory::LinearAllocator(1024 * 1024);
Containers::Array<int> arr;
arr.SetAllocator(linear);
// 使用对象池分配器
auto* pool = new Memory::PoolAllocator(sizeof(MyObject), 100);
Containers::Array<MyObject> objects;
objects.SetAllocator(pool);
```
## 相关文档
- [Array 总览](array.md) - 返回类总览