C++ allocator<X>::deallocate(NULL,1) allowed?

孤街醉人 提交于 2019-12-22 04:45:12

问题


Both free(NULL) and ::operator delete(NULL) are allowed. Does the allocator concept (e.g. std::allocator also allow deallocate(NULL,1), or is it required to put your own guard around it?


回答1:


You'll need to add your own check.

According to §20.4.​1.1/8, deallocate requires:

p shall be a pointer value obtained from allocate(). n shall equal the value passed as the first argument to the invocation of allocate which returned p.

allocate throws an exception when storage can't be given (§20.4.​1.1/7). In other words, allocate never returns 0, and therefore deallocate should never get a 0. Passing a 0 would lead to undefined behavior.



来源:https://stackoverflow.com/questions/3104543/c-allocatorxdeallocatenull-1-allowed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!