Reputation: 5175
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 example where smart pointers is actually useful?
Thank you!
Upvotes: 7
Views: 636
Reputation: 1
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
Upvotes: 0
Reputation: 74450
Primarily, smart pointers help you to:
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.
Upvotes: 6
Reputation: 67345
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.
Upvotes: 1
Reputation: 12713
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.
Upvotes: 1
Reputation: 28474
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.
Upvotes: 3
Reputation: 25828
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.
Upvotes: 0
Reputation: 15308
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).
Upvotes: 0