开发者

Can someone explain smart pointers in plain English?

开发者 https://www.devze.com 2023-02-10 09:26 出处:网络
Today I was asked about smart pointers in C++, and I can\'t find anywhere useful information about it..

Today I was asked about smart pointers in C++, and I can't find anywhere useful information about it..

Please, can someone tell: What is smart pointers? When do you need it? Do you have any ex开发者_JS百科ample where smart pointers is actually useful?

Thank you!


Primarily, smart pointers help you to:

  • Avoid leaks when exceptions are thrown. When an exception is thrown, you don't want any objects that are allocated earlier in the try block to be leaked. By wrapping them in smart pointers, which will be destroyed when the try block is exited, those objects will get properly destroyed.
  • Manage lifetime by reference counting owners to objects (i.e., the last one to destroy its smart pointer referencing a particular object actually deallocates the object). This is especially helpful in loosely coupled scenarios where it is not clear at what time the object should be destroyed, because users of the object do not know about each other.

A good example of where smart pointers are useful:

A vector of pointers to objects. By making it a vector of shared pointers, for example, the objects will automatically be deallocated when the vector is destroyed and/or objects are removed. This automates object lifetime management and helps the user of the container avoid memory leaks.


Excerpt from Boost Smart Pointers (smart_ptr) lib:

Smart pointers are objects which store pointers to dynamically allocated (heap) objects. They behave much like built-in C++ pointers except that they automatically delete the object pointed to at the appropriate time. Smart pointers are particularly useful in the face of exceptions as they ensure proper destruction of dynamically allocated objects. They can also be used to keep track of dynamically allocated objects shared by multiple owners.

Conceptually, smart pointers are seen as owning the object pointed to, and thus responsible for deletion of the object when it is no longer needed.


Smart pointers handle their own memory management by keeping track of how many references point to the memory. Once there are 0 references, it deletes the memory for you. Makes memory management easier.


Smart pointer general refers to a class that behaves like a pointer. You can use the class to store a pointer to memory that you allocate, and access data through the pointer.

The advantage is that, when used inside functions and methods, the smart pointer can be made to automatically deallocate the memory once the variable goes out of scope. Otherwise, this is a prime opportunity for a memory leak when functions fail to free all allocated memory.

For an example, check out http://msdn.microsoft.com/en-us/library/txda4x5t(VS.80).aspx.


A smart pointer is an object that dynamically allocates memory for the thing that it points to, and when the smart pointer goes out of scope it automatically deallocates the memory for the thing that it points to. It's useful when you want something that's deallocated when it goes out of scope, but that's too big to put on the stack (or has other issues that prevent it from being able to be put on the stack).


A smart pointer essentially manages memory allocated on the heap with an object allocated on the stack.

Because objects allocated on the stack have a fixed lifetime (i.e. within the scope they are declared) deallocation of the heap memory is deterministic and guaranteed to happen.


Smart pointers are basically objects that perform functions similar to pointers they are used to lessen the allocation and deallocation time. For C++ one common example would be of auto_ptr

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号