Date: Sat, 23 Aug 2025 06:25:09 +0100
The current guideline is to use std::unique_ptr or other smartpointer, and not raw pointers. So by that argument we have alreadyachieved what you appear to be trying to do by having a guideline.Smart pointers don't suffer, use after free or double free problems, so obviously I'm talking about low level code dealing with raw pointers.> 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.I didn't say that the originating pointer is deleted/freed, I said itslifetime ends. The pointer, not what it's pointing at. void somefn() { T * subthing = new T; // manipulate *subthing, perhaps some calculations somevec.push_back(BiggerThing{subthing}); }The BiggerThing would have had to take the T* by reference, and storethat T* reference in itself. BiggerThing gets put in a vector, andthus its lifetime is now longer than the execution of somefn (letsignore the complications of what a member variable which is areference introduces). Somefn ends and subthing ends its lifetime,rendering the reference stored in BiggerThing now dangling.Heck, that guideline would break std::unique_ptr by itself. Thatguideline would have unique_ptr take the pointer in by reference,store that reference, and pretty much by intention the originatingpointer ends its lifetime. Never mind that one would not be able tomove the unique_ptr since you can't re-seat the reference its storing,so the originating unique_ptr could not be reset back to nullptr, it'sforced to continue to reference the originating pointer (which is nolonger in its lifetime, so the reference is dangling and attemtping touse it is UB anyway!).You are mixing between raw pointers usage and smart pointers usage. Passing by copy, creates resources ownership confusion. Passing the pointer variable by reference imposes on it to outlive all the references, but at the same time, if any of these variables is a parameter to the delete operator, then it's guaranteed to become nullptr as per the proposal, so no false ownership.that's the guideline purpose. Let's forget the passing by reference thing....just focus on the delete operator which nullify it's pointer parameter. I don't see why it is not the standard..
Received on 2025-08-23 05:25:20