C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Delete...why not a parameter by reference?!

From: organicoman <organicoman_at_[hidden]>
Date: Sat, 23 Aug 2025 04:57:35 +0100
Sent from my Galaxy
Two problems: since the passing pointer thing is only a guideline, that diminishes the illusion of “safety” that this provides. And hand-waving away the problem of legacy code.Impeding a language feature to guarantee the safety, is not really practical. So it's better to provide a guideline.For legacy code....if it is wrong then it is wrong either in legacy or current code.If in some legacy code, you find a proliferation of copies of the same pointer, you will not know from which one the resource was freed.I guess a third problem: since everything is taken by reference (by the guideline) then the originating pointer has to have a longer lifetime than any of its “copies”.Yes, and that's the point of it, as soon as the originating pointer is freed then all references are nullptr, thus unusable. And if one of the references is freed, automatically the originating pointer gets nullified.On Fri, Aug 22, 2025 at 8:04 PM organicoman via Std-Proposals <std-proposals_at_[hidden]> wrote:Hello,One of the recurring bugs in c++ is use after free.I'm wondering, why the delete operator doesn't take the pointer parameter by reference and nullify it?So instead ofvoid operator delete(void* p);It becomes void operator delete(void* &p);And it assigns the value nullptr to p after freeing its corresponding memory.Look at this example:---------{ int* p = new int(42); int* const& to_p = p; // as a guideline delete p; // if taken by reference an nullified *p; // this will be a guaranteed runtime error *to_p; // and all previous copies obey too}---------On top of that, basically we need just to add a guideline that mandate:-All duplication of a pointer should be by reference if modifying, or const reference if not.So it is guaranteed that if you free the memory using any of the references, all copies will be nullptr.So, is there any constraints to prevents this other than breaking old code?RegardsSent from my Galaxy--
Std-Proposals mailing list
Std-Proposals_at_[hidden]
https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals

Received on 2025-08-23 03:57:41