In general it's safe and can lead to more robust code. It should not lead to a memory leak since the memory pointed to is automatic deleted.
But there are some cases where you have to take care:
- Copies of
auto_ptr
are not equal! - Construction of one
auto_ptr
from another will release the object the first pointer was pointing to
Please see here:
- http://www.gotw.ca/publications/using_auto_ptr_effectively.htm
- http://www.cprogramming.com/tutorial/auto_ptr.html
The auto_ptr template class is designed to help manage memory in a semi-automatic way and prevent memory leaks when unexpected events such as exceptions would otherwise have caused the normal cleanup code to be skipped.
(quoted from (2))