Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assigning existing values to smart-ptrs?

Tags:

I am just learning about smart pointers, and I am having trouble assigning a pre-existing location of a variable to the standard library's shared pointer.

For example, lets say you have an int x, which you do not know the value of. With normal pointers, I just did

int* ptr;
ptr = &x;

I tried both that with shared pointers, and

std::tr1::shared_ptr<int> ptr;
ptr = std::make_shared<int> (&x)

So i'm fairly lost as to how to do it.

like image 256
bluepanda Avatar asked Nov 25 '13 14:11

bluepanda


People also ask

How do you assign a value to a shared pointer?

You cannot assign the value of a pointer directly to a shared_ptr object. You may either use make_shared or member reset instead.

When should you use shared_ptr?

An object referenced by the contained raw pointer will not be destroyed until reference count is greater than zero i.e. until all copies of shared_ptr have been deleted. So, we should use shared_ptr when we want to assign one raw pointer to multiple owners. // referring to the same managed object.

Can shared_ptr be Nullptr?

A null shared_ptr does serve the same purpose as a raw null pointer. It might indicate the non-availability of data. However, for the most part, there is no reason for a null shared_ptr to possess a control block or a managed nullptr .

How does a shared pointer work?

"Shared pointer is a smart pointer (a C++ object wih overloaded operator*() and operator->()) that keeps a pointer to an object and a pointer to a shared reference count. Every time a copy of the smart pointer is made using the copy constructor, the reference count is incremented.


2 Answers

You wouldn't (usually) make a smart pointer point to an existing variable. A smart pointer manages the lifetime of a dynamically allocated object, deleting it after use; pointing it to something that wasn't dynamically allocated will cause an error if it tries to delete it.

You would usually use new or make_shared to create an object, and create or assign a smart pointer with the result of that:

std::shared_ptr<int> ptr(new int(42)); // Create a new pointer to manage an object
ptr.reset(new int(66));                // Reset to manage a different object
ptr = std::make_shared<int>(53);       // Use `make_shared` rather than `new`

make_shared is usually preferable to new, since it makes better use of memory and gives stronger exception-safety.

like image 66
Mike Seymour Avatar answered Sep 23 '22 18:09

Mike Seymour


Shared pointers are used to manage dynamically allocated memory and more precisely, they manage the ownership for this memory.

Basically, a smart pointer is a materialization of the Ressource Acquisition Is Initialization, or RAII. I strongly suggest you take a look at this principle, as it is extremely useful for managing resource ownership (basically, each time you need to acquire a resource, and release it, be it memory, a database connection, a file handler, a mutex, etc.).

What it does is basically guarantee that while someone points at the dynamically allocated memory it manages, then this memory will be available, and as soon as the last (smart) pointer to this memory goes out of scope, then delete is called.

Then, it makes no sense to use smart pointers with variable that have automatic storage duration (i.e. that are removed when they go out of scope or when the object they're member of goes itself out of scope or is deleted (if it was new'd).

like image 26
JBL Avatar answered Sep 20 '22 18:09

JBL