Q&A

What is the difference between auto_ptr and unique_ptr?

What is the difference between auto_ptr and unique_ptr?

unique_ptr is a new facility with a similar functionality, but with improved security. auto_ptr is a smart pointer that manages an object obtained via new expression and deletes that object when auto_ptr itself is destroyed.

What is smart pointer in C++ example?

As shown in the example, a smart pointer is a class template that you declare on the stack, and initialize by using a raw pointer that points to a heap-allocated object. After the smart pointer is initialized, it owns the raw pointer.

What is an auto pointer in C++?

auto_ptr is a class template that was available in previous versions of the C++ standard library (declared in the header file), which provides some basic RAII features for C++ raw pointers. It has been replaced by the unique_ptr class. For shared ownership, the shared_ptr template class can be used.

How do smart pointers differ from regular pointers?

How do smart pointers differ from regular pointers? Smart pointers keep track of the owners of a resource and automatically deallocate the resource when the last owner goes out of scope. Name the header file that needs to be included in a program that uses smart pointers.

Why is Auto_ptr bad?

More specifically, copying an auto_ptr causes one of the copies to let go of the pointer. Which of these remains in the container is not defined. Therefore, you can randomly lose access to pointers if you store auto_ptrs in the containers.

What is weak pointer in C++?

A weak pointer is a smart pointer that does not take ownership of an object but act as an observer. In other words, it does not participate in reference counting to delete an object or extend its lifetime. Weak pointers are mainly used to break the circular dependency that shared pointers create.

How do you set a unique pointer?

Given the common situation where the lifespan of an owned object is linked to its owner, I can use a unique pointer one of 2 ways . . It can be assigned: class owner { std::unique_ptr owned; public: owner() { owned=std::unique_ptr(new someObject()); } };

Do I need to delete shared pointer?

Thus for smart pointers you do not need to explicitly delete the pointer. No memory leak can be caused, because the shared_ptr will deallocate the allocated object when it goes out of scope.

When should you not use a smart pointer?

10 Answers

  1. When not to. There are two situations where you should not use smart pointers.
  2. Smart managers. Unless you are writing a smart manager class, if you use the keyword delete you are doing something wrong.
  3. Smart pointers.
  4. Smart containers.
  5. Rolling your own.
  6. Examples.

What is a Unique_ptr?

std::unique_ptr is a smart pointer that owns and manages another object through a pointer and disposes of that object when the unique_ptr goes out of scope. The object is disposed of, using the associated deleter when either of the following happens: the managing unique_ptr object is destroyed.

How does a weak pointer work?

shared_ptr uses an extra “counter” object (aka. “shared count” or “control block”) to store the reference count. (BTW: that “counter” object also stores the deleter.) Every shared_ptr and weak_ptr contains a pointer to the actual pointee, and a second pointer to the “counter” object.

How do you set a shared pointer to a weak pointer?

You can make a weak pointer from a shared pointer, just using assignment = e.g. bool release_channel(std::shared_ptr ch) { std::weak_ptr weak_ch = ch; //… }

What are the uses of smart pointers in C?

Smart pointers are also useful in management of resources, such as file handles or network sockets. C++ libraries provide implementations of smart pointers in the form of auto_ptr, unique_ptr, shared_ptr and weak_ptr.

Which is better shared pointers or auto pointers?

Shared pointers delete the pointee when the last shared pointer to it is destroyed. This is useful when you want a no-brainer, open-ended storage scheme where expected lifetime and ownership can vary widely depending on the situation. Due to the need to keep an (atomic) counter, they’re a bit slower than auto pointers.

How to declare a smart pointer as a local variable?

Declare the smart pointer as an automatic (local) variable. (Do not use the new or malloc expression on the smart pointer itself.) In the type parameter, specify the pointed-to type of the encapsulated pointer. Pass a raw pointer to a new-ed object in the smart pointer constructor.

When do smart pointers go out of scope?

Since the destructor is automatically called when an object goes out of scope, the dynamically allocated memory would automatically be deleted (or reference count can be decremented). Consider the following simple smart ptr class.