在C++中,
编辑: 在阅读了答案后,我测试了Visual Studio的
对于
因此,使用
new int[0]
是允许的,但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?。