当STL分配器allocate发生错误时是否应该调用deallocate函数?
Should the deallocate function be called when an error occurs in the STL allocator allocate?
try
{
node = allocator.allocate(LIST_BASIC_UNIT);
}
catch (...)
{
allocator.deallocate(node);
throw;
}
当STL分配器allocate出现异常时是否应该调用deallocator?
您不能取消分配任何东西,因为没有任何东西可以取消分配。如果 allocate()
抛出,那么它会在 return 一个指针之前和 node
可以分配任何值之前停止。所以假设 node
在 try
块之前无效(应该是),你没有有效的指针传递给 deallocate()
.
如果给定的分配器实现在抛出异常时不能保证不发生变化,那是这个分配器的错误,而不是你的容器的错误。
try
{
node = allocator.allocate(LIST_BASIC_UNIT);
}
catch (...)
{
allocator.deallocate(node);
throw;
}
当STL分配器allocate出现异常时是否应该调用deallocator?
您不能取消分配任何东西,因为没有任何东西可以取消分配。如果 allocate()
抛出,那么它会在 return 一个指针之前和 node
可以分配任何值之前停止。所以假设 node
在 try
块之前无效(应该是),你没有有效的指针传递给 deallocate()
.
如果给定的分配器实现在抛出异常时不能保证不发生变化,那是这个分配器的错误,而不是你的容器的错误。