Can someone explain smart pointers in plain English?

Today I was asked about smart pointers in C ++, and I can not find useful information about this.

Please can someone say: What are smart pointers? When do you need it? Do you have any example where smart pointers are really useful?

Thanks!

+7
source share
7 answers

First of all, smart pointers help you:

  • Avoid leakage when throwing exceptions. When an exception is thrown, you do not want any objects that were highlighted earlier in the try block to be skipped. By wrapping them in smart pointers that will be destroyed when you exit the try block, these objects will be correctly destroyed.
  • Manage the lifetime by matching owners of object references (i.e., the latter destroys its smart pointer that refers to a specific object, actually frees the object). This is especially useful in loosely coupled scenarios where it is unclear at what time the object should be destroyed because the users of the object are not aware of each other.

A good example of using smart pointers:

Vector pointers to objects. From making it a vector of common pointers, for example, objects will be automatically freed when the vector is destroyed and / or the objects are deleted. This automates object lifecycle management and helps the container user avoid memory leaks.

+6
source

Excerpt from Boost Smart Pointers (smart_ptr) lib:

Smart pointers are objects that store pointers to the dynamic allocation (heaps) of objects. They behave like built-in pointers in C ++, except that they automatically delete the object indicated at the appropriate time. Smart pointers are especially useful in the face of exceptions, as they ensure proper destruction of dynamically allocated objects. They can also be used to track dynamically distributed shared objects by multiple owners.

Smart pointers are conceptually displayed as the owner of the object that they are pointing to and the person responsible for deleting the object when it is no longer needed.

+3
source

Smart pointers handle their own memory management by tracking how many references point to memory. When there are 0 links, it removes memory for you. Simplifies memory management.

+1
source

A smart pointer generally refers to a class that behaves like a pointer. You can use the class to store a pointer to the memory you allocate and access data through a pointer.

The advantage is that when used inside functions and methods, a smart pointer can be made to automatically free memory after a variable goes out of scope. Otherwise, this is a great opportunity for memory leaks when functions do not free up all the allocated memory.

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

+1
source

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 frees memory for what it points to. This is useful when you want something that is freed when it goes out of scope but is too large to be put on the stack (or there are other problems that prevent it from being pushed onto the stack).

0
source

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

Since the objects allocated on the stack have a fixed lifetime (i.e., within the area that they declare), freeing up heap memory is deterministic and guaranteed.

0
source

Smart pointers are basically objects that perform functions similar to pointers they are used to reduce the allocation and release times. For C ++, one common example: auto_ptr

0
source

All Articles