You will be more productive and your code will be more robust. Sign up to join this community. The best answers are voted up and rise to the top. Stack Overflow for Teams — Collaborate and share knowledge with a private group.
Create a free Team What is Teams? Learn more. Ask Question. Asked 9 years, 9 months ago. Active 5 years, 4 months ago. Viewed 56k times. Improve this question. Not using pointers? Having a distinct owner of the object, that manages the lifetime?
It is hard to not use pointers. Have you considered listening less to the advice presented and more to the argument behind that advice? He explains pretty well the kind of system in which this sort of advice would work. NicolBolas: I listened to the advice and the argument but obviously I didn't feel I understood it well enough. It's ok if you need one but you won't be able to think locally. So, is a global object a last resort?
Show 3 more comments. Active Oldest Votes. Improve this answer. However, taking such an approach will severely limit a programmer's ability to apply concurrency programming patterns on most non-trivial OOP classes due to non-copyability. This issue is raised in the "Going Native ". Add a comment. A safe way to survive when the state of the world is indeterminate. Nicol Bolas Nicol Bolas It obviously doesn't apply to every situation, just another very useful tool in the tool-box.
FWIW, to counter the claim that Bjarne is living in academic world: in all my purely industrial career which included co-architecting a G20 stock exchange and solely architecting a K-player MOG I have seen only 3 cases when we did really need shared ownership. What about: If some hash table holds onto an object and that object's hashcode changes If some function is iterating a vector and an element is inserted into that vector Ben Voigt Ben Voigt 3, 18 18 silver badges 23 23 bronze badges.
But raw pointers aren't bad. Only making the first, owning pointer to an object a raw pointer is bad, because then you have to manually manage memory, which is non-trivial in the presence of exceptions.
But using raw pointers as handles or iterators is just fine. BenVoigt: Of course, the difficulty with passing around naked pointers is that you don't know the lifetime of objects. If some object holds onto a naked pointer and that object dies Bjarne tries to live in a world were everything has a nice, explicit lifetime, and everything's built around that.
And if you can build that world, great. Starting with Boost release 1. This is accomplished by using an array type T[] or T23109 as the template parameter. There is almost no difference between using an unsized array, T[] , and a sized array, T23109 ; the latter just enables operator[] to perform a range check on the index.
A simple guideline that nearly eliminates the possibility of memory leaks is: always use a named smart pointer variable to hold the result of new. Every occurence of the new keyword in the code should have the form:. See Herb Sutter's treatment also here of the issue for more information. These factory functions also provide an efficiency benefit by consolidating allocations. Requirements: Y must be a complete type. The expression delete[] p , when T is an array type, or delete p , when T is not an array type, must be well-formed, must not invoke undefined behavior, and must not throw exceptions.
Exception safety: If an exception is thrown, the constructor calls delete[] p , when T is an array type, or delete p , when T is not an array type. The postcondition that use count is 1 holds even if p is 0; invoking delete on a pointer that has a value of 0 is harmless.
The destructor will call delete with the same pointer, complete with its original type, even when T does not have a virtual destructor, or is void. Requirements: D must be CopyConstructible. The copy constructor and destructor of D must not throw.
The expression d p must be well-formed, must not invoke undefined behavior, and must not throw exceptions. Nevertheless, the destructor should be declared virtual in the classes that are meant to be used polymorphically. For instance:. The in-depth treatment of aliasing constructor deserves its own space.
There is more discussion about the managed object pointer in the 'Deleter' section below when we talk about the type erasure. We mentioned above that the control block could either contain a pointer to the managed object or the object itself. The control block is dynamically allocated. Constructing the managed object in-place within the control block can avoid the two separate memory allocations for the object and the control block, resulting in an uncomplicated control block and better performance.
The managed object is disposed of when the reference count reaches zero. The managed pointer is the one passed to the deleter when use count reaches zero. This is sometimes used to keep a dynamic library or a plugin loaded as long as any of its functions are referenced:. The managed object is constructed in-place in a data member of the control block. In this case, the control block stores a pointer to the managed object. These pointers are not necessarily equal. If that counter reaches zero, the control block calls the destructor of the managed object.
In existing implementations, the number of weak pointers is incremented [1] , [2] if there is a shared pointer to the same control block. Create account Log in.
0コメント