What is the difference between *ptr and *ptr.get() when using auto_ptr?
Asked Answered
S

4

7

Why would I use get() with *, instead of just calling *?

Consider the following code:

auto_ptr<int> p (new int);
*p = 100;
cout << "p points to " << *p << '\n';           //100

auto_ptr<int> p (new int);
*p.get() = 100;
cout << "p points to " << *p.get() << '\n'; //100

Result is exactly the same. Is get() more secure?

Schonfeld answered 20/10, 2016 at 12:58 Comment(3)
practically no difference.Outman
As this is tagged C++11: std::auto_ptr is deprecated and will be removed in C++17. Use std::unique_ptr instead.Autogamy
"Is get() more secure?" Using auto_ptr makes your whole code insecure in my opinion. Don't use it.Brinn
O
8

Practically no difference.

In case of *p, the overloaded operator* (defined by auto_ptr) is invoked which returns the reference to the underlying object (after dereferencing it — which is done by the member function). In the latter case, however, p.get() returns the underlying pointer which you dereference yourself.

I hope that answers your question. Now I'd advise you to avoid using std::auto_ptr, as it is badly designed — it has even been deprecated, in preference to other smart pointers such as std::unique_ptr and std::shared_ptr (along with std::weak_ptr).

Outman answered 20/10, 2016 at 13:1 Comment(0)
H
6

*p calls auto_ptr::operator*, which dereferences the managed pointer.

*p.get first calls method auto_ptr::get, which returns the managed pointer, which is then dereferenced by operator *.

These will provide exactly the same result once executed: the managed pointer is dereferenced, and there will be no additional checking when using get.

Note that auto_ptr is deprecated since C++11. It is dangerous because ownership of the pointer is transfered when copying:

std::auto_ptr<int> p(new int(42));

{
    std::auto_ptr<int> copy_of_p(p); // ownership of *p is transfered here
} // copy_of_p is destroyed, and deletes its owned pointer

// p is now a dangling pointer

To avoid the problem, you had to "manage the managed pointers":

std::auto_ptr<int> p(new int(42));

{
    std::auto_ptr<int> copy_of_p(p); // ownership of *p is transfered here

    // ...

    p = copy_of_p; // p gets back ownership
} // copy_of_p is destroyed, but doesn't delete pointer owned by p

// p is still valid

Use unique_ptr or shared_ptr instead.

Harpsichord answered 20/10, 2016 at 13:2 Comment(0)
S
4

There is no effective difference. operator* is defined to return *get() (cppreference).

You should consider using unique_ptr in auto_ptr's stead. The latter has been removed from the current C++ standard and has very non-intuitive yank-on-copy behaviour.

Stoppage answered 20/10, 2016 at 13:0 Comment(0)
A
0

Don't use get() on a smart pointer (regardless of auto_, unique_, shared_, or otherwise). This returns a naked pointer that isn't under the control of an RAII class. This opens up various possibilities of giving the pointer to another RAII class (causing double-deletion later), or the caller doing something silly like deleting the pointer (again causing double-deletion). Just dereference the smart pointer.

PS: For experts only: Yes, there are other legitimate reasons for extracting the raw pointer from a smart pointer. But by not using get() in general, the times that it is used becomes a red flag for "there's something funky going on here, pay attention!".

Avid answered 20/10, 2016 at 22:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.