Why delete can perform on pointers to const while free cannot?

前端 未结 4 909
鱼传尺愫
鱼传尺愫 2021-02-14 06:22

I\'ve just noticed that the pointers passed to delete can be const qualified while those passed to free cannot. That is really a surprise

4条回答
  •  花落未央
    2021-02-14 07:00

    free is C function. Its signature is 20-30 years old, from times where there were no const in C language (and there was no C++ language as well). C++ compiler treats free like every other function, and can't let it accept const pointer without cast, because free could possibly change object pointed. Actually, it does, as well as delete, but C++ doesn't know that free is used for memory management.

提交回复
热议问题