【发布时间】:2018-12-04 10:03:25
【问题描述】:
new int[0] 在 C++ 中是允许的,但 std::allocator<int>().allocate(0) 是否定义明确?
更一般地说,所有分配器都必须接受0 作为分配参数吗?
编辑:
阅读答案后,我测试了 Visual Studio 的std::allocator:allocate(0) 给出了nullptr
deallocate(nullptr, anything) 是一个 nop。
所以使用nullptr 是一个很好的建议,但标准不要求deallocate(nullptr, 0) 是nop,请参阅C++ allocator::deallocate(NULL,1) allowed?
【问题讨论】:
标签: c++ memory-management language-lawyer dynamic-memory-allocation allocator